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‘相关推荐

  1. 解决Python中sum函数出现的TypeError: unsupported operand type(s) for +: 'int' and 'list'错误问题

    当在Python中运行sum函数时,会出现"TypeError: unsupported operand type(s) for +: 'int' and 'list'"这样的问题 ...

  2. 解决pip安装时出现报错TypeError: unsupported operand type(s) for -=: ‘Retry‘ and ‘int‘

    我在Linux下使用pip安装时出现报错: Exception: Traceback (most recent call last):File "/usr/lib/python2.7/dis ...

  3. 成功解决TypeError: unsupported operand type(s) for +: 'dict_items' and 'list'

    成功解决TypeError: unsupported operand type(s) for +: 'dict_items' and 'list' 目录 解决问题 解决思路 解决方法 解决问题 Typ ...

  4. 成功解决TypeError: unsupported operand type(s) for %: 'NoneType' and 'dict'

    成功解决TypeError: unsupported operand type(s) for %: 'NoneType' and 'dict' 目录 解决问题 解决思路 解决方法 解决问题 TypeE ...

  5. 踩坑中:TypeError: unsupported operand type(s) for /: 'str' and 'int'

    在网上查看了很多博客,首先从报错提示来讲TypeError: unsupported operand type(s) for /: 'str' and 'int',明确了是类型错误:不支持操作类型为整 ...

  6. Django问题:TypeError: unsupported operand type(s) for -: ‘str‘ and ‘float‘

    Django问题:TypeError: unsupported operand type(s) for -: 'str' and 'float Traceback (most recent call ...

  7. python报错TypeError: unsupported operand type(s) for -: ‘decimal.Decimal‘ and ‘float‘的解决方法

    问题描述 在编写python代码时,进行小数位相减时出现 TypeError: unsupported operand type(s) for -: 'decimal.Decimal' and 'fl ...

  8. opencv小笔记(TypeError: unsupported operand type(s) for +: ‘NoneType‘ and ‘NoneType‘)

    今天在学习OpenCV的算数操作时,进行图像加法时,运行了下列程序 import numpy as np import cv2 as cv import matplotlib.pyplot as pl ...

  9. apex安装报错:TypeError: unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘

    参考解决方法:TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' · Issue #990 · NVIDIA/apex ...

最新文章

  1. SQL SERVER数据库文件收缩
  2. 再窥--单链表和顺序存储
  3. zookeeper理解
  4. python具体应用过程_python公开课|Python for循环的具体应用就是python流程控制的核心,想学会就来看看...
  5. spring整合atomikos实现分布式事务的方法示例_分布式事务中的XA和JTA
  6. 深度学习笔记(25) 池化层
  7. 辐射3计算机,辐射3与道德
  8. C# 杀掉指定进程
  9. java项目实现服务器监控,Java实现服务器状态监控
  10. 比较好的论坛(个人认为)
  11. aria2和motrix的使用
  12. 高德地图两点间距离计算函数
  13. 【2021最新】4篇图神经网络综述论文,建议收藏!
  14. java生成随机邮箱_Java随机密码生成并和邮箱、手机号匹配
  15. 图解联合概率密度、边缘概率密度、条件概率密度之间的关系
  16. **Javase 重要知识点总结 day08
  17. Neurosim的manual细读(四)
  18. 北京南天软件java工程师面试题
  19. 工程力学(10)—轴向拉伸与压缩二
  20. 什么是自动出价?它的优势是什么?

热门文章

  1. 产业园区如何解决“招商难”?明确招商思路
  2. 网友分享失联9天的经历,看得人直冒冷汗,真的不能再熬夜了!
  3. 【笔记篇】04电商供应链流程——之《实战供应链》
  4. 字符串旋转——C语言
  5. spring特殊字符转义和方法入参检测工具类
  6. 统计一篇英文文章单词个数
  7. 高数_证明_极限的唯一性
  8. guacamole 认识
  9. 用vs2013编写并调试erlang内建函数(NIF)
  10. 超详细的MES制造执行系统介绍——设备管理