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

报错代码:

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}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 这是一个类型错误,意味着您正在尝试对一个类型为“numpy.float32”的对象执行“len”操作,但是“numpy.float32”对象不支持“len”操作。您可能需要检查代码以查找可能导致此错误的位置,并确保您正在对支持“len”操作的实际对象执行该操作。 ### 回答2: 这个错误一般是在使用Python中的len()函数时出现的。我们可以通过简单的例子来说明这个错误。 假设我们有一个numpy数组arr: import numpy as np arr = np.array([1,2,3]) 如果我们尝试使用len()函数来检查数组的长度: print(len(arr)) 我们会得到以下错误信息: TypeError: object of type 'numpy.ndarray' has no len() 这是因为numpy数组不支持len()函数。在上面的例子中,我们将类型更改为numpy.float32,错误信息就变成了“TypeError: object of type'numpy.float32' has no len()”。 为了检查numpy数组的长度,我们应该使用numpy的shape属性: print(arr.shape[0]) 这会输出数组的第一个维度的大小,即数组的长度。在上面的例子中,输出将为3。 因此,当你遇到“typeerror: object of type 'numpy.float32' has no len()”这种错误时,你应该检查你是否在使用len()函数来确定numpy数组的长度。如果是的话,你应该使用numpy的shape属性来获取数组的大小。 ### 回答3: 这个错误信息出现在Python的程序中,提示了一个类型错误(TypeError)。具体的错误信息为"object of type 'numpy.float32' has no len()"。这种错误通常出现在使用len()函数去获取一个不支持长度(即没有长度属性)的对象时。 这个错误信息中出现了"numpy.float32",说明在程序中使用了numpy库中的float32数据类型,而该数据类型并不具备长度属性。因此,当使用len()函数来获取numpy.float32类型的长度时,就会产生这个错误信息。 要解决这个错误,首先需要确认程序中使用了numpy.float32类型,并且在对其使用len()函数时出现了错误。可尝试使用其他的属性或方法来替代len()函数,如shape属性等。或者也可以将numpy.float32类型转换为其他支持长度属性的类型(如列表,元组等)来解决问题。 总的来说, TypeError: object of type 'numpy.float32' has no len()是一个比较常见的错误信息,解决方法也相对简单,只需要确认出错的源头,以及针对该类型找到其支持的方法,就可以解决这个问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn-WJW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值