1. 程式人生 > >TensorFlow 報錯 TypeError: The value of a feed cannot be a tf.Tensor object

TensorFlow 報錯 TypeError: The value of a feed cannot be a tf.Tensor object

報錯程式碼:

with tf.Session() as sess:
    sess.run(init_op)

    for i in range(self.epoch_num):
        batch_images, batch_labels = mnist.train.next_batch(self.batch_size)
        batch_images = tf.reshape(tensor=batch_images, shape=[self.batch_size, 28, 28, 1])
        batch_images = tf.image.resize_images(images=batch_images,size=(32,32))
        print("images shape:{}".format(batch_images.shape))
        print("labels shape:{}".format(batch_labels.shape))
        accuracy = sess.run(train_op, feed_dict={images_holder:batch_images, labels_holder:batch_labels})
        print(accuracy)

報錯資訊:

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("resize_images/ResizeBilinear:0", shape=(100, 32, 32, 1), dtype=float32) which was passed to the feed with key Tensor("x:0", shape=(100, 32, 32, 1), dtype=float32).

 

報錯提示已經很明顯了,就是餵給訓練操作的資料不能是張量,只能是 scalars, strings, lists, numpy ndarrays, or TensorHandles

 

解決方法:

在餵給訓練操作的張量後面加 .eval(),將張量操作算出來的結果餵給訓練操作就正常了

accuracy = sess.run(train_op, feed_dict={images_holder:batch_images.eval(), labels_holder:batch_labels})