Tensorflow多个样品,无需分批

2024-06-02 11:23:32 发布

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

我有一个图,不幸的是,有些节点不支持批处理(自定义操作尚未充实)我已经能够让多个线程调用sess.运行然后通过feed-dict将数据放入。我现在已经将数据转换为tfrecords,以正确利用队列,但是仍然无法找到一种方法来告诉它在没有多个线程调用的情况下并行运行图的多个实例sess.运行(). 我假设tensorflow开发人员已经在某个地方创建了一个更“python”的方法来实现这一点,但是我还没有找到它。如何在tensorflow中执行此操作?在

编辑:即使是批处理的数据,前面的问题仍然存在,因为我的计算一半时间花在cpu上,一半时间花在gpu上,因此不管批处理,一个都会等待另一个的一半时间。我想让图形异步训练多个样本来填充这个空间。在

编辑2:我想我必须把伪代码放在这里,让那些不想读上面的文字的人使用。在

import tensorflow as tf

resultOfCPUCalculation = someCPUOnlyOP(inputData)\\does not support batching
gpuResults = aBunchOfGPUOps(resultOfCPUCalculation)
with tf.Session() as sess:
    sess.run([gpuResults])
    //only uses 1 cpu core, and the gpu is idle while it's doing it's thing.

我想用一种“流水线”的方式来完成这项工作,只要cpu操作完成,就从另一个样本开始。在


Tags: 数据方法编辑gputftensorflowas时间
1条回答
网友
1楼 · 发布于 2024-06-02 11:23:32
import tensorflow as tf

input_example = get_input_example()
cpu_output = some_cpu_only_op(input_example)

cpu_output_batch = tf.train.batch(input_example, batch_size, num_threads)
gpu_output_batch = a_bunch_of_gpu_ops(cpu_output_batch)

with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    for i in range(num_train_steps):
        output_values = sess.run(gpu_output_batch)
        do_stuff_with(output_values)

    coord.request_stop()
    coord.join(threads)

input_example将必须由队列馈送。在

相关问题 更多 >