当前位置: 代码迷 >> 综合 >> TensorFlow 报错 TypeError Fetch argument 0.16 has invalid type class 'numpy.float32', must be a string
  详细解决方案

TensorFlow 报错 TypeError Fetch argument 0.16 has invalid type class 'numpy.float32', must be a string

热度:68   发布时间:2024-01-14 07:04:08.0

报错代码:

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}

  相关解决方案