运行backpropagation.py,寻找 最小loss的代码

```python
import tensorflow as tf
w = tf.Variable(tf.constant(5, dtype=tf.float32))
lr = 0.2
epoch = 40
for epoch in range(epoch):  # for epoch 定义顶层循环,表示对数据集循环epoch次,此例数据集数据仅有1个w,初始化时候constant赋值为5,循环40次迭代。
    with tf.GradientTape() as tape:  # with结构到grads框起了梯度的计算过程。
        loss = tf.square(w + 1)
    grads = tape.gradient(loss, w)  # .gradient函数告知谁对谁求导

    w.assign_sub(lr * grads)  # .assign_sub 对变量做自减 即:w -= lr*grads 即 w = w - lr*grads
    print("After %s epoch,w is %f,loss is %f" % (epoch, w.numpy(), loss))

# lr初始值:0.2   请自改学习率  0.001  0.999 看收敛过程
# 最终目的:找到 loss 最小 即 w = -1 的最优参数w

```

出现问题:

AttributeError: 'RefVariable' object has no attribute '_id'

解决 AttributeError: RefVariable object has no attribute _id(tf 版本低)

问题分析:


怀疑 是 tensorflow(=1.12.0,python 3.6.5)版本太低


解决 AttributeError: RefVariable object has no attribute _id(tf 版本低)

解决办法:


在Anaconda 中 创建一个 虚拟 环境 tf2.0(创建方法)。在tf2.0 中安装 python 3.7 和tensorflow 2.0.0

解决 AttributeError: RefVariable object has no attribute _id(tf 版本低)
tf2.0环境中运行:

解决 AttributeError: RefVariable object has no attribute _id(tf 版本低)

解决 AttributeError: RefVariable object has no attribute _id(tf 版本低)

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-15
  • 2021-12-07
  • 2021-12-14
  • 2021-06-06
  • 2021-07-27
猜你喜欢
  • 2021-10-22
  • 2022-12-23
  • 2021-06-26
  • 2021-06-28
  • 2021-11-22
  • 2022-12-23
  • 2021-08-16
相关资源
相似解决方案