Tensorflow:有效地将数据移动/放入GPU

时间:2017-08-05 00:20:38

标签: python tensorflow tensorflow-gpu

所以我正在阅读有关从CPU移动数据的更多信息 - > Tensorflow中的GPU,我发现feed_dict仍然很慢: https://github.com/tensorflow/tensorflow/issues/2919

我看到将Python变量“移动”到GPU上的直接选项是:

#1. Tensorflow constant
a = tf.constant(data, name='a')

#2. Tensorflow Variable
b = tf.Variable(data, name='b')

#3. Tensorflow placeholder
c = tf.placeholder(dtype=dtype, shape=[x,y,z ...], name='c')

选项#1#2对于非常大的数据集变量不实用(因为您实际上是将数据预加载到内存中),因为我们将快速超过2GB图形限制。这使得#3成为将大型 Python vars变为Tensorflow的更好选择,但之后您被迫使用feed_dict

除了#1#2#3之外,还有其他选项可以将 Python变量移动到GPU吗?我指的是......

with tf.device('/gpu:0'):
    # create tensorflow object(s), whether it's tf.Variable, tf.constant, etc

如果我理解正确,我们可以使用输入管道功能来解决这个问题吗?我在这里提到这两个:

  1. https://datascience.stackexchange.com/questions/17559/input-pipeline-for-tensorflow-on-gpu

  2. https://stackoverflow.com/a/38956678/7093236

  3. 我能做些什么来进一步提高将所有内容放在Tensorflow端的速度吗?

2 个答案:

答案 0 :(得分:1)

最好的方法是使用张量流Queue来加速数据传输。

即使您没有标签文件,也可以执行以下步骤

# data_files and labels_files are list, this may be some data files path, and labels values.
filename_queue = tf.train.slice_input_producer([data_files, label_files], shuffle=True)   
# filename_queue = tf.train.slice_input_producer(data_files, shuffle=True)       
# Some steps to decode the files and process
 ......
data, label = some_function(filename_queue)

# Define batch size and get batch for processing
image_batch, label_batch = tf.train.shuffle_batch([data, label], batch_size=batch_size, num_threads=num_threads)                                                                                                       

答案 1 :(得分:1)

Dataset API是将数据移动到GPU的面向未来的方法。所有合理的优化,如tensorflow performance guide中解释的优化,最终将在那里得到。

相关问题