1

我将 Tensorflow(版本 1.7.0 和 Python 3.5)用于神经网络,但在使用该tf.train.batch()函数时遇到问题。见这里

我的数据集的维度是:

测试图像 (100000, 900) 测试标签 (100000, 10)

所以我有 100000 张大小为 30 x 30 像素的测试图像。标签是大小为 100000 x 10 的 one-hot 矩阵。

import numpy as np
train_images = np.load("train_images.npy")
train_labels = np.load("train_labels.npy")

现在我想得到一个大小为 100 的随机批次并想使用该功能tf.train.batch()

我在我的代码中使用如下函数:

# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    num_examples=100000
    batch_size = 100

    # Training cycle
    for epoch in range(training_epochs):
        total_batch = int(num_examples/batch_size)
        # Loop over all batches
        for i in range(total_batch):
            batch_x, batch_y = tf.train.batch(
                [train_images, train_labels],
                batch_size=batch_size,
                allow_smaller_final_batch=True,
                )

            _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y:batch_y})

这样做我得到以下错误:

    Traceback (most recent call last):
    File "my_network.py", line 124, in <module>
    _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y:batch_y})
    File "/home/samuel/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 905, in run
    run_metadata_ptr)
    File "/home/samuel/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1091, in _run
    'feed with key ' + str(feed) + '.')
    TypeError: The value of a feed cannot be a tf.Tensor object. 
    Acceptable feed values include Python scalars, strings, lists, 
    numpy ndarrays, or TensorHandles.For reference, the tensor object was
    Tensor("batch:0", shape=(?, 900), dtype=uint8) which was passed to the
feed with key Tensor("Placeholder:0", shape=(?, 900), dtype=float32).

我该怎么做才能tf.train.batch()使我的网络正常工作?我是否需要使用另一种方法来创建小批量?

4

1 回答 1

0

tf.train.batch 已弃用,您应该改用 tf.data 进行批处理。

那就是说...

为了清楚起见,您应该将图形构建和图形运行步骤分开。

然后,在使用 tf.train.batch 时,在构建图形之后但在运行任何操作之前,您需要运行 tf.queue_runner.start_queue_runners() 来启动将数据预取到批处理中的线程。

于 2018-04-18T20:26:55.483 回答