TypeError: Fetch argument has invalid type class ‘numpy.float32‘, must be a string or Tensor
项目github地址:bitcarmanlee easy-algorithm-interview-and-practice
欢迎大家star,留言,一起学习进步
1.问题
tensorflow代码在运行的时候,出现如下错误
Epoch: 0010 cost= 0.080483146 W= 0.23614137 b= 0.97142047
...
TypeError: Fetch argument 0.080483146 has invalid type <class 'numpy.float32'>, must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.)
对应部分的代码如下
...loss = tf.reduce_sum(tf.pow(pred-Y, 2) / (2*n_samples))# optmizeoptimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)init = tf.global_variables_initializer()with tf.Session() as sess:sess.run(init)for epoch in range(training_epochs):for (x, y) in zip(train_X, train_Y):sess.run(optimizer, feed_dict={X: x, Y: y})if (epoch+1) % display_step == 0:loss = sess.run(loss, feed_dict={X: train_X, Y:train_Y})print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(loss), "W=", sess.run(W), "b=", sess.run(b))...
2.原因分析
很明显是loss函数那边出了问题。第一次loss有正常输出,而后面loss则有异常,原因是Fetch需要的参数为一个Tensor或者String,但是现在传过来的确实个float32。
再查看一下
loss = tf.reduce_sum(tf.pow(pred-Y, 2) / (2*n_samples))
第一轮迭代的时候,这个loss是个Tensor。但是后面 run方法返回的,确实个float,所以会出现异常!
3.解决方法
因为是loss的异常引起的,所以我们将run方法返回的loss重新命令,给个新变量即可。
loss = tf.reduce_sum(tf.pow(pred-Y, 2) / (2*n_samples))# optmizeoptimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)init = tf.global_variables_initializer()with tf.Session() as sess:sess.run(init)for epoch in range(training_epochs):for (x, y) in zip(train_X, train_Y):sess.run(optimizer, feed_dict={X: x, Y: y})if (epoch+1) % display_step == 0:newloss = sess.run(loss, feed_dict={X: train_X, Y:train_Y})print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(newloss), "W=", sess.run(W), "b=", sess.run(b))
像上面这样,将run方法返回的值重新命名为newloss即可解决上述问题!
TypeError: Fetch argument has invalid type class ‘numpy.float32‘, must be a string or Tensor相关推荐
- has invalid type class 'numpy.ndarray', must be a string or Tensor
has invalid type <class 'numpy.ndarray'>, must be a string or Tensor. (Can not convert a ndarr ...
- gee错误:Image.multiply, argument ‘image2‘: Invalid type. Expected type: Image<unknown bands>.
我们在计算reduceRegion进行相应的线性统计,比如说 Image (Error) Image.multiply, argument 'image2': Invalid type. Expect ...
- 成功解决TypeError: ‘encoding’ is an invalid keyword argument for this function
成功解决TypeError: 'encoding' is an invalid keyword argument for this function 目录 解决问题 解决思路 解决方法 解决问题 Ty ...
- Numpy出现TypeError: integer argument expected, got float的分析与解决
问题 今天在使用Numpy的时候,打印array的时候突然出现了TypeError: integer argument expected, got float 这个报错,部分代码如下: import ...
- TypeError: first argument must be an iterable of pandas objects, you passed an object of type “DataF
使用concat()函数拼接两个表格,出现以下错误: TypeError: first argument must be an iterable of pandas objects, you pass ...
- 讲解为什么会提示“invalid type argument of ‘->‘ (have ‘Data_Ao‘)”错误信息
今天分享一个之前遇到的错误提示,其实就是语法的错误.可能如果对"->"或者"."的用法不了解,就会容易造成这样的错误,加上如果你的编译器不会提示一下基础的 ...
- TypeError: mean() received an invalid combination of arguments - got (list, dim=int), but expected o
TypeError: mean() received an invalid combination of arguments - got (list, dim=int), but expected o ...
- TypeError: Cannot handle this data type: (1, 1, 28), |u1
PIL和numpy相互转换方式如下: from PIL import Image#PIL image转成numpy np_img = np.asarray(PIL_img) #参数时PIL类型的图片 ...
- {TypeError}clamp(): argument 'min' must be Number, not Tensor
{TypeError}clamp(): argument 'min' must be Number, not Tensor x1是二维数组, xx1 = x1[order[1:]].clamp(min ...
- 报错解决——ctypes.ArgumentError: argument 1:……….. : wrong type
运行 python darknet.py 结果报错如下: Traceback (most recent call last): File "darknet.py", line 13 ...
最新文章
- openstack-5:安装rabbitmq
- 空间数据库Spatial Tools的使用
- proxy aspectj_使用AspectJ,Javassist和Java Proxy进行代码注入的实用介绍
- 用JavaFX编写图块引擎
- 精确覆盖DLX算法模板
- 【CSU - 1023】【HRBUST - 1039】修路(二分,枚举)
- 棋盘问题 POJ - 1321
- 学习笔记(2):uni-app实战社区交友类app开发-引入css动画库
- 东欧视频游戏市场概况
- nodejs异步流程控制
- HDOJ 5184 Brackets 卡特兰数扩展
- 使用Liquid实现简单的数据交换
- zedgraph显示最小刻度_关于ZedGraph几个难点
- 单片机技术应用实训装置
- 服务器无线路由器桥接,三个无线路由器怎么设置桥接
- SpringCloud Day05---服务网关(Gateway)
- 二值图像的Euclidean distance map(EDM)特征图计算及其优化
- 直播 | SDCC 2017 人工智能技术实战线上峰会
- 2.数据的结构 (学校考试必考概念)-数据结构入门(c语言实现)
- 从抓取豆瓣电影聊高性能爬虫思路