报错代码:
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))
sess.run(train_op, feed_dict={images_holder:batch_images.eval(), labels_holder:batch_labels})
accuracy = sess.run(fetches=accuracy, feed_dict={images_holder: batch_images.eval(), labels_holder: batch_labels})
print(accuracy)
报错信息:
TypeError: Fetch argument 0.16 has invalid type <class 'numpy.float32'>, must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.)
仔细检查代码后,问题主要出在这一句上:accuracy = sess.run(accuracy, feed_dict={images_holder: batch_images.eval(), labels_holder: batch_labels})
解决方法:将接受返回值的变量 accuracy 换一个名字,与 sess.run() 中参数 fetches 接收的 accuracy 不同的名字即可,如下:
accuracy_result = sess.run(accuracy, feed_dict={images_holder: batch_images.eval(), labels_holder: batch_labels}
本文详细解析了在使用TensorFlow进行模型训练时遇到的TypeError问题,错误源于sess.run()函数中fetches参数的不当使用。通过调整变量名,成功避免了类型冲突,实现了模型的正确训练。
31万+

被折叠的 条评论
为什么被折叠?



