简体   繁体   English

为什么我们需要在T​​ensorFlow中“分配”?

[英]Why do we need “assign” in TensorFlow?

I am going through an example here . 我在这里举个例子。 I see there the following sequence of operations: 我看到以下一系列操作:

import tensorflow as tf

v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
inc_v1 = v1.assign(v1+1)
init_op = tf.global_variables_initializer()
s = tf.Session()
s.run(init_op)
s.run(inc_v1)

As a result we get: 结果我们得到:

array([1., 1., 1.], dtype=float32)

I do not understand the logic behind "assign" operation. 我不明白“分配”操作背后的逻辑。 In particular I have replaced it by something that looks much simpler to me: 特别是我用一些看起来更简单的东西取而代之:

import tensorflow as tf

v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
inc_v1 = v1 + 1
init_op = tf.global_variables_initializer()
s = tf.Session()
s.run(init_op)
s.run(inc_v1)

And I got exactly the same output. 而且我得到了完全相同的输出。 So, why do we need assign? 那么,为什么我们需要分配?

It takes your original tensor and a new tensor, updates original value of your tensor with a new value and returns the reference of your original tensor. 它采用原始张量和新张量,用新值更新张量的原始值并返回原始张量的参考。 Take a look at the graphs generated on Tensorboard: 看一下Tensorboard上生成的图表:

The operation assign returns a reference to the original Tensor: 操作assign返回对原始Tensor的引用:

分配

Without assign , just creates another tensor to add a constant value: 如果没有assign ,只需创建另一个张量来添加常量值:

没有分配

If you print the evaluation of the tensor v1 (after running inc_v1 ) it outputs [1. 1. 1.] 如果打印张量v1的评估(运行inc_v1 ),则输出[1. 1. 1.] [1. 1. 1.] as the result of the operation its been reassigned to the original tensor. [1. 1. 1.]作为操作的结果,它被重新分配给原始张量。 In the second case, it will remain as [0. 0. 0.] 在第二种情况下,它将保持为[0. 0. 0.] [0. 0. 0.] . [0. 0. 0.]

That example is indeed not very illustrative. 这个例子确实不是很具说明性。 The important part is that assign saves the given value to the variable within the session, so you can use it later in the next call to run . 重要的部分是assign将给定值保存到会话中的变量,因此您可以在稍后的调用中使用它来run See here: 看这里:

import tensorflow as tf

v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
inc_v1 = v1.assign(v1+1)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    sess.run(inc_v1)
    print(sess.run(v1))
    # [1. 1. 1.]
    sess.run(inc_v1)
    print(sess.run(v1))
    # [2. 2. 2.]

Note v1 saves the assigned value, so in further calls to run it can be used. v1保存分配的值,所以在还呼吁run可以使用。 Compare now to: 现在比较:

import tensorflow as tf

v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
inc_v1 = v1+1
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    sess.run(inc_v1)
    print(sess.run(v1))
    # [0. 0. 0.]
    sess.run(inc_v1)
    print(sess.run(v1))
    # [0. 0. 0.]

Here the increment happens within one call to run , but its result is never saved anywhere, and v1 keeps having the value [0. 0. 0.] 这里增量发生在一次run调用中,但其结果从未保存在任何地方,并且v1保持值[0. 0. 0.] [0. 0. 0.] . [0. 0. 0.]

Variables are important because most things in TensorFlow are done on several steps, eg each batch in a neural network. 变量很重要,因为TensorFlow中的大多数内容都是在几个步骤中完成的,例如神经网络中的每个批次。 Each step is a call to run , and it is important that the changes to a model in a step (eg the updates to the weights in a neural network) are saved for the next step - otherwise you would be running in place, never leaving the starting point! 每个步骤都是一个run调用,重要的是在一个步骤中对模型的更改(例如神经网络中权重的更新)将被保存用于下一步 - 否则您将在适当的位置运行,永远不会离开出发点!

如果要分配不是来自图表的值,则需要指定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM