我正在尝试在 GPU 上运行一些回归模型。虽然我的 GPU 利用率非常低,高达 20%。看完代码后,
for i in range(epochs):
rand_index = np.random.choice(args.train_pr,
size=args.batch_size)
rand_x = X_train[rand_index]
rand_y = Y_train[rand_index]
我使用这三行代码为每次迭代选择一个随机批处理。所以,我想问一下什么时候开始训练,我可以为下一次迭代多准备一批吗?
我正在研究回归问题而不是分类问题。我已经在 Tensorflow 中看到过线程,但只找到了图像示例,没有用于训练的大小为 100000X1000 的大矩阵的示例。
请您参考如下方法:
您有一个位于主机内存中的大型 numpy 数组。您希望能够在 CPU 上并行处理它并将批处理发送到设备。
从 TF 1.4 开始,最好的方法是使用 tf.data.Dataset,尤其是 tf.data.Dataset.from_tensor_slices。然而,作为the documentation指出,您可能不提供您的 numpy 数组作为此函数的参数,因为它最终会被复制到设备内存中。你应该做的是使用占位符。文档中给出的示例非常不言自明:
features_placeholder = tf.placeholder(features.dtype, features.shape)
labels_placeholder = tf.placeholder(labels.dtype, labels.shape)
dataset = tf.data.Dataset.from_tensor_slices((features_placeholder, labels_placeholder))
# [Other transformations on `dataset`...]
iterator = dataset.make_initializable_iterator()
sess.run(iterator.initializer, feed_dict={features_placeholder: features,
labels_placeholder: labels})
可以使用 .map 方法对切片应用进一步的预处理或数据扩充。为确保这些操作同时发生,请确保仅使用 tensorflow 操作并避免使用 tf.py_func 包装 python 操作。
