tensorflow编码错误:TypeError: unsupported operand type(s) for *: ‘float‘ and ‘NoneType‘
tensorflow 编码出现错误:TypeError: unsupported operand type(s) for *: ‘float’ and ‘NoneType’
原码如下:
import tensorflow as tf
w=tf.Variable(tf.constant(5,dtype=float)) #给w赋随机值,初始值为float 5
lr=0.2 #学习率设为0.2
epoch=20 #设置循环迭代次数
for i in range(epoch): #for循环20次对loss函数求导with tf.GradientTape() as DS: #导入求导模块并命名为DSloss=tf.square(w+1) #定义loss函数tidu=DS.gradient(loss,w) #调用求导模块的求导函数对w求偏导w=w-lr*tidu #更新w值
print("after %s epoch,w is %f,loss is %f"%(epoch,w.numpy(),loss))
运行出现错误:
line 9, in
w=w-lr*grand
TypeError: unsupported operand type(s) for *: ‘float’ and ‘NoneType’
原因是:参数类型错误。要将浮点数与None相乘,所以报错,在with tf.GradientTape() as tape里,参数系统默认跟踪类型为tf.Variable数据类型的数据,将参数类型改成:
w=tf.Variable(w-lr * tidu)
或自减:
w.assign_sub(lr*tidu)
正确代码如下:
import tensorflow as tf
w=tf.Variable(tf.constant(5,dtype=float)) #给w赋随机值,初始值为float 5
lr=0.2 #学习率设为0.2
epoch=20 #设置循环迭代次数
for i in range(epoch): #for循环20次对loss函数求导with tf.GradientTape() as DS: #导入求导模块并命名为DSloss=tf.square(w+1) #定义loss函数tidu=DS.gradient(loss,w) #调用求导模块的求导函数对w求偏导w=tf.Variable(w-lr*tidu) #更新w值 也可以写成w.assign_sub(lr*tidu)
print("after %s epoch,w is %f,loss is %f"%(epoch,w.numpy(),loss))
再看一下运行结果
after 20 epoch,w is -0.999781,loss is 0.000000
Process finished with exit code 0
经过20个周期迭代,成功算得损失函数为0
参考博客原文链接:https://blog.csdn.net/weixin_43788143/article/details/107024185
tensorflow编码错误:TypeError: unsupported operand type(s) for *: ‘float‘ and ‘NoneType‘相关推荐
- 解决Python中sum函数出现的TypeError: unsupported operand type(s) for +: 'int' and 'list'错误问题
当在Python中运行sum函数时,会出现"TypeError: unsupported operand type(s) for +: 'int' and 'list'"这样的问题 ...
- 解决pip安装时出现报错TypeError: unsupported operand type(s) for -=: ‘Retry‘ and ‘int‘
我在Linux下使用pip安装时出现报错: Exception: Traceback (most recent call last):File "/usr/lib/python2.7/dis ...
- 成功解决TypeError: unsupported operand type(s) for +: 'dict_items' and 'list'
成功解决TypeError: unsupported operand type(s) for +: 'dict_items' and 'list' 目录 解决问题 解决思路 解决方法 解决问题 Typ ...
- 成功解决TypeError: unsupported operand type(s) for %: 'NoneType' and 'dict'
成功解决TypeError: unsupported operand type(s) for %: 'NoneType' and 'dict' 目录 解决问题 解决思路 解决方法 解决问题 TypeE ...
- 踩坑中:TypeError: unsupported operand type(s) for /: 'str' and 'int'
在网上查看了很多博客,首先从报错提示来讲TypeError: unsupported operand type(s) for /: 'str' and 'int',明确了是类型错误:不支持操作类型为整 ...
- Django问题:TypeError: unsupported operand type(s) for -: ‘str‘ and ‘float‘
Django问题:TypeError: unsupported operand type(s) for -: 'str' and 'float Traceback (most recent call ...
- python报错TypeError: unsupported operand type(s) for -: ‘decimal.Decimal‘ and ‘float‘的解决方法
问题描述 在编写python代码时,进行小数位相减时出现 TypeError: unsupported operand type(s) for -: 'decimal.Decimal' and 'fl ...
- opencv小笔记(TypeError: unsupported operand type(s) for +: ‘NoneType‘ and ‘NoneType‘)
今天在学习OpenCV的算数操作时,进行图像加法时,运行了下列程序 import numpy as np import cv2 as cv import matplotlib.pyplot as pl ...
- apex安装报错:TypeError: unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘
参考解决方法:TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' · Issue #990 · NVIDIA/apex ...
最新文章
- SQL SERVER数据库文件收缩
- 再窥--单链表和顺序存储
- zookeeper理解
- python具体应用过程_python公开课|Python for循环的具体应用就是python流程控制的核心,想学会就来看看...
- spring整合atomikos实现分布式事务的方法示例_分布式事务中的XA和JTA
- 深度学习笔记(25) 池化层
- 辐射3计算机,辐射3与道德
- C# 杀掉指定进程
- java项目实现服务器监控,Java实现服务器状态监控
- 比较好的论坛(个人认为)
- aria2和motrix的使用
- 高德地图两点间距离计算函数
- 【2021最新】4篇图神经网络综述论文,建议收藏!
- java生成随机邮箱_Java随机密码生成并和邮箱、手机号匹配
- 图解联合概率密度、边缘概率密度、条件概率密度之间的关系
- **Javase 重要知识点总结 day08
- Neurosim的manual细读(四)
- 北京南天软件java工程师面试题
- 工程力学(10)—轴向拉伸与压缩二
- 什么是自动出价?它的优势是什么?