tensorflow2.0 - Tensorflow 2.0: AttributeError: Tensor.name 在启用 Eager Execution 时毫无意义
问题描述
使用 tensorflow 2.0 不断出现这些错误。这应该工作吗?
import tensorflow as tf
import numpy as np
x = tf.constant(3.0)
with tf.GradientTape() as t:
t.watch(x)
y = (x - 10) ** 2
opt = tf.optimizers.Adam()
opt.minimize(lambda: y, var_list=[x])
解决方案
在磁带中,您只需要计算优化器的正向传递,并且最小化定义不是正向传递的一部分,因此您必须远程处理它们。
而且,如果你想使用minimize
优化器的方法,你不必使用tf.GradienTape
对象,而只需将前向传递(损失计算)定义为一个函数,那么优化器将为你创建磁带+最小化函数.
但是,由于您想使用常量而不是变量,因此您必须使用 atf.GradientTape
并手动计算损失值。
import tensorflow as tf
x = tf.constant(3.0)
with tf.GradientTape() as t:
t.watch(x)
y = (x - 10) ** 2
grads = t.gradient(y, [x])
当然你不能应用渐变
opt = tf.optimizers.Adam()
opt.apply_gradients(zip([y], [x]))
since x
is not a trainable variable, but a constant (the apply_gradients
call will raise an exception)
推荐阅读
- javascript - 根据条件禁用按钮
- javascript - 使用 Mocha 和 JavaScript 语法测试 Angular 9 (TypeScript)
- python - Python BeautifulSoup 表不解析
- google-cloud-platform - Google Cloud Source Repository:远程:PERMISSION_DENIED:调用者没有远程权限
- c++ - 在复杂的类中使用 boost::numeric::odeint 步进器
- django - 创建了双重用户
- c# - 显示添加的项目总数
- reactjs - styled-jsx 动态样式未应用
- tcl - 如何在 TCL 中获取匹配行的第一个元素
- javascript - 如何在 Javascript 中验证日期?