【问题标题】:Difference between v.assign(v + 1) and v = v + 1 in TensorflowTensorflow 中 v.assign(v + 1) 和 v = v + 1 的区别
【发布时间】:2018-07-04 05:12:31
【问题描述】:

以下 TensorFlow 代码运行良好,v1 变为 [1., 1., 1.]

v1 = tf.get_variable('v1', shape=[3], initializer=tf.zeros_initializer)
v1 = v1 + 1 

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print (v1.eval())

下面的代码段也给了我们与上面完全相同的结果。如果我们运行sess.run(inc_v1)v1 将变为 [1., 1., 1.]。

v1 = tf.get_variable('v1', shape=[3], initializer=tf.zeros_initializer)
inc_v1 = v1.assign(v1 + 1)


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(inc_v1)
    print (v1.eval())

但是,以下代码会导致错误。

v1 = tf.get_variable('v1', shape=[3], initializer=tf.zeros_initializer)
v1 = v1 + 1 
inc_v1 = v1.assign(v1 + 1)


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(inc_v1)
    print (v1.eval())

错误如下:

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

您能告诉我为什么会导致错误吗?

【问题讨论】:

    标签: tensorflow assign arithmetic-expressions


    【解决方案1】:

    张量和变量在 TensorFlow 中是不同的对象

    import tensorflow as tf
    
    
    def inspect(t):
        print('\n %s\n-------' % t.name)
        print(type(t))
        print(t.op.outputs)
        print('has assign method' if 'assign' in dir(t) else 'has no assign method')
    
    
    v1 = tf.get_variable('v1', shape=[3], initializer=tf.zeros_initializer)
    inspect(v1)
    v2 = v1 + 1
    inspect(v2)
    

    给予

     v1:0
    -------
    <class 'tensorflow.python.ops.variables.Variable'>
    [<tf.Tensor 'v1:0' shape=(3,) dtype=float32_ref>]
    has assign method
    
     add:0
    -------
    <class 'tensorflow.python.framework.ops.Tensor'>
    [<tf.Tensor 'add:0' shape=(3,) dtype=float32>]
    has no assign method
    

    因此,v1:0 确实是变量本身,v1 具有方法 assign。这是有道理的,因为它只是对浮点值的引用。 另一方面,v2 = v1 + 1 导致add 操作的输出。所以v2 不再是一个变量,你不能为v2 分配一个新值。在这种情况下,您希望更新add 的哪个操作数? 每当您使用 v1 时,请考虑使用来自 v1read_value() 操作:

    v1 = tf.get_variable('v1', shape=[3], initializer=tf.zeros_initializer)
    inspect(v1)
    w = v1.read_value()
    inspect(w)
    v2 = v1.read_value() + 1
    inspect(v2)
    

    给予

     v1:0
    -------
    <class 'tensorflow.python.ops.variables.Variable'>
    [<tf.Tensor 'v1:0' shape=(3,) dtype=float32_ref>]
    has assign method
    
     read:0
    -------
    <class 'tensorflow.python.framework.ops.Tensor'>
    [<tf.Tensor 'read:0' shape=(3,) dtype=float32>]
    has no assign method
    
     add:0
    -------
    <class 'tensorflow.python.framework.ops.Tensor'>
    [<tf.Tensor 'add:0' shape=(3,) dtype=float32>]
    has no assign method
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-04
      • 2018-10-24
      • 2019-04-18
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多