当我尝试使用assign_add 或assign_sub 函数时,我遇到了错误“Tensor”对象没有属性“assign_add”。代码如下所示:
我定义了两个张量 t1 和 t2,它们具有相同的形状和相同的数据类型。
>>> t1 = tf.Variable(tf.ones([2,3,4],tf.int32))
>>> t2 = tf.Variable(tf.zeros([2,3,4],tf.int32))
>>> t1
<tf.Variable 'Variable_4:0' shape=(2, 3, 4) dtype=int32_ref>
>>> t2
<tf.Variable 'Variable_5:0' shape=(2, 3, 4) dtype=int32_ref>
Run Code Online (Sandbox Code Playgroud)
然后我在 t1 和 t2 上使用 assign_add 来创建 t3
>>> t3 = tf.assign_add(t1,t2)
>>> t3
<tf.Tensor 'AssignAdd_4:0' shape=(2, 3, 4) dtype=int32_ref>
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用 t1[1] 和 t2[1] 创建一个新的张量 t4,它们是具有相同形状和相同数据类型的张量。
>>> t1[1]
<tf.Tensor 'strided_slice_23:0' shape=(3, 4) dtype=int32>
>>> t2[1]
<tf.Tensor 'strided_slice_24:0' shape=(3, 4) dtype=int32>
>>> t4 = tf.assign_add(t1[1],t2[1])
Run Code Online (Sandbox Code Playgroud)
但有错误,
Traceback (most recent call …Run Code Online (Sandbox Code Playgroud)