Python 实际需要 global_variables_initializer() 时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44299666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:53:15  来源:igfitidea点击:

When global_variables_initializer() is actually required

pythonpython-3.xtensorflowinitializer

提问by Vinay

import tensorflow as tf
x = tf.constant(35, name='x')
y = tf.Variable(x + 5, name='y')
# model = tf.global_variables_initializer()
with tf.Session() as session:
        print("x = ", session.run(x)) 
        # session.run(model)
        print("y = ", session.run(y))

I was not able to understand when global_variables_initializer()is actually required. In the above code, if we uncomment lines 4 & 7, I can execute the code and see the values. If I run as-is, I see a crash.

我无法理解什么时候global_variables_initializer()真正需要。在上面的代码中,如果我们取消第 4 行和第 7 行的注释,我可以执行代码并查看值。如果我按原样运行,我会看到崩溃。

My question is which variables it is initializing. xis a constant which does not need initialization and yis variable which is not being initialized but is used as an arithmetic operation.

我的问题是它正在初始化哪些变量。x是一个不需要初始化的常量,是一个y没有被初始化但用作算术运算的变量。

采纳答案by Salvador Dali

tf.global_variables_initializeris a shortcut to initialize all global variables. It is not required, and you can use other ways to initialize your variables or in case of easy scripts sometimes you do not need to initialize them at all.

tf.global_variables_initializer是初始化所有全局变量的快捷方式。它不是必需的,您可以使用其他方法来初始化变量,或者在简单脚本的情况下,有时您根本不需要初始化它们。

Everything except of variables do not require initialization (constants and placeholders). But every usedvariable (even if it is a constant) should be initialized. This will give you an error, although zis just 0-d tensor with only one number.

除变量外的所有内容都不需要初始化(常量和占位符)。但是每个使用的变量(即使它是一个常量)都应该被初始化。这会给你一个错误,尽管z它只是一个只有一个数字的 0-d 张量。

import tensorflow as tf
z = tf.Variable(4)
with tf.Session() as session:
        print(session.run(z)) 

I highlighted the word used, because if you just have variables which are not run (or non of the runs depends on them) you do not need to initialize them.

我突出显示了使用的词,因为如果您只有未运行的变量(或非运行依赖于它们),则不需要初始化它们。



For example this code will execute without any problems, nonetheless it has 2 variables and one operation which depends on them. But the run does not require them.

例如,此代码将毫无问题地执行,但它有 2 个变量和一个依赖于它们的操作。但是运行不需要它们。

import tensorflow as tf
x = tf.constant(35, name='x')
y = tf.Variable(x + 5, name='y')
z = tf.Variable(4)
a = y + z
with tf.Session() as session:
        print("x = ", session.run(x)) 

回答by P-Gn

From the docs(emphasis mine):

文档(强调我的):

Calling tf.Variable() adds several ops to the graph:

  • A variable op that holds the variable value.
  • An initializer opthat sets the variable to its initial value. This is actually a tf.assign op.
  • The ops for the initial value, such as the zeros op for the biases variable in the example are also added to the graph.

调用 tf.Variable() 会向图中添加几个操作:

  • 保存变量值的变量 op。
  • 将变量设置为其初始值的初始化操作。这实际上是一个 tf.assign 操作。
  • 初始值的操作,例如示例中偏差变量的零操作,也会添加到图中。

Later,

之后,

Variable initializers must be run explicitly before other opsin your model can be run. The easiest way to do that is to add an op that runs all the variable initializers, and run that op before using the model.

变量初始值设定项必须显式运行,然后才能运行模型中的其他操作。最简单的方法是添加一个运行所有变量初始化器的操作,并在使用模型之前运行该操作。

In short, global_variables_initializeris never required, Variableinitialization is. Whenever you have Variablesin your code, you must initialize them first. The global_variables_initializerhelper initializes all Variablesthat have been previously declared, and is therefore just a very convenient way to do it.

简而言之,global_variables_initializer从不需要,Variable初始化是。每当您Variables的代码中存在时,您必须先初始化它们。该global_variables_initializer助手初始化所有Variables先前已声明,因此仅仅是一个非常方便的方式来做到这一点。

回答by kmario23

The tf.global_variables_initializerjust initializes all variables that tf.global_variables()would list. This actually makes much sense in a distributed environment where the graph might be located in different computing nodes in a cluster.

tf.global_variables_initializer刚刚初始化所有的变量tf.global_variables()将列出。这实际上在分布式环境中很有意义,其中图可能位于集群中的不同计算节点中。

In such a case, tf.global_variables_initializer()which is just an alias for tf.variables_initializer(tf.global_variables())would initialize all the variables in all the computing nodes, where the graph is placed.

在这种情况下,tf.global_variables_initializer()这只是一个别名,它tf.variables_initializer(tf.global_variables())会初始化放置图的所有计算节点中的所有变量。

回答by Calvin Alvin

It's never a requirement unless you are using a declared tf.Variableor tf.placeholderfrom within your tensorflow session run. Personally, I always make it a habit of running tf.global_variables_initializer(). It almost becomes part of the boiler plate code when running tensorflow models:

除非您在 tensorflow 会话运行中使用已声明的tf.Variabletf.placeholder来自 tensorflow 会话运行,否则这从来都不是必需的。就个人而言,我总是把跑步变成一种习惯tf.global_variables_initializer()。在运行 tensorflow 模型时,它几乎成为样板代码的一部分:

with tf.Session(graph=graph) as sess:
    sess.run(tf.global_variables_initializer())

    # run model etc...