gpt4 book ai didi

Tensorflow,为变量赋值

转载 作者:行者123 更新时间:2023-12-04 18:01:59 25 4
gpt4 key购买 nike

我是 tensorflow 的新手,我正在学习

https://www.tensorflow.org/get_started/get_started

fixW = tf.assign(W, [-1.])

工作正常,但
fixb = tf.assign(b, [1.])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/milenko/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py", line 272, in assign
return ref.assign(value)
AttributeError: 'Tensor' object has no attribute 'assign'

另一个例子
zero_tsr = tf.zeros([1.,2.])
zero_tsr
<tf.Tensor 'zeros:0' shape=(1, 2) dtype=float32>

如果我尝试更改 zero_tsr
fixz = tf.assign(zero_tsr, [2.,2.])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/milenko/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py", line 272, in assign
return ref.assign(value)
AttributeError: 'Tensor' object has no attribute 'assign'

同样的问题。

我没有换过shell,一切都一样。为什么我这里有问题?

最佳答案

在您发布的示例中:

zero_tsr = tf.zeros([1.,2.])
zero_tsr
<tf.Tensor 'zeros:0' shape=(1, 2) dtype=float32>
zero_tsr是一个常数而不是一个变量,所以你不能给它赋值。

来自 the documentation :

assign( ref, value, validate_shape=None, use_locking=None, name=None )

ref: A mutable Tensor. Should be from a Variable node. May be uninitialized.



例如,这将正常工作:
import tensorflow as tf
zero_tsr = tf.Variable([0,0])
tf.assign(zero_tsr,[4,5])

虽然此代码会引发错误
import tensorflow as tf
zero_tsr = tf.zeros([1,2])
tf.assign(zero_tsr,[4,5])

引发的错误正是您发布的错误:

AttributeError: 'Tensor' object has no attribute 'assign'

关于Tensorflow,为变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44755413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com