Tensorflow预取到设备

2024-09-28 22:12:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试新的张量流函数tf.contrib.data公司。预回迁到设备。在

我的简单代码示例

model = build_network()

N=1000

def gen():
    while True:
        batch = np.random.rand(N, 48, 48, 3)
        # Do some heavy calculation
        yield batch

dataset = tf.data.Dataset.from_generator(gen, tf.float32)
dataset = dataset.apply(tf.contrib.data.prefetch_to_device('/gpu:0'))

iterator = dataset.make_one_shot_iterator()
x = iterator.get_next()

output = model(x)

g = gen()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(100):
        if i == 50:
            start = time.time()
        result = sess.run(output)
        #result = model.predict(next(g))
    end = time.time()
print('\nAverage time of one forward pass: {}\n'.format((end-start)/50))
print('Done')

这会产生错误:

InvalidArgumentError (see above for traceback): Cannot assign a device for operation 'IteratorGetDevice': Could not satisfy explicit device specification '/device:GPU:0' because no supported kernel for GPU devices is available. Colocation Debug Info: Colocation group had the following types and devices: IteratorToStringHandle: CPU IteratorGetDevice: CPU OneShotIterator: CPU

Colocation members and user-requested devices: OneShotIterator (OneShotIterator) IteratorGetDevice (IteratorGetDevice) /device:GPU:0 IteratorToStringHandle (IteratorToStringHandle)

Registered kernels: device='CPU'

[[Node: IteratorGetDevice = IteratorGetDevice_device="/device:GPU:0"]]

这个新功能是不能与from峎u生成器结合使用,还是其他功能?在


Tags: fordatamodelgputimedevicetfcpu
1条回答
网友
1楼 · 发布于 2024-09-28 22:12:28

这是TensorFlow 1.8rc0候选版本中的一个bug。谢谢你引起我们的注意!在

它现在已在master branch中修复,并将在下一个夜间构建中获取。我还提交了一个cherry-pick to the 1.8 release branch,它应该包含在tensorflow1.8的下一个候选版本(和最终版本)中。在

相关问题 更多 >