简体   繁体   English

为什么我需要在 TensorFlow 中初始化变量?

[英]Why do I need to initialize variables in TensorFlow?

I primarily develop my models in R and I am currently learning TensorFlow.我主要在 R 中开发我的模型,我目前正在学习 TensorFlow。 I'm going through a tutorial with the following code我正在使用以下代码进行教程

raw_data = [1., 2., 8., -1., 0., 5.5, 6., 13] 
spike = tf.Variable(False)
spike.initializer.run()

for i in range(1, len(raw_data)): 
    if raw_data[i] - raw_data[i-1] > 5:
        updater = tf.assign(spike, True)
        updater.eval()
    else:
        tf.assign(spike, False).eval()
    print("Spike", spike.eval())
sess.close()

From a layman's perspective, why do I need to initialize and Variabalize in TensorFlow?从外行的角度来看,为什么我需要在 TensorFlow 中进行初始化和 Variabalize? I know this may be a basic question but it's something not dealt with in R.我知道这可能是一个基本问题,但它在 R 中没有处理。

Let's have a look at what the script actually does:让我们来看看脚本实际做了什么:

spike = tf.Variable(False)

This line creates a symbolic variable or a node in the computational graph, with a constant initializer.该行在计算图中创建了一个符号变量或节点,并带有一个常量初始化器。 At this point, nothing's been allocated for this variable.在这一点上,没有为这个变量分配任何东西 On top of that, it's not even known yet on which device (CPU or GPU) it's going to be placed.最重要的是,它甚至还不知道它将被放置在哪个设备(CPU 或 GPU)上。

Next,接下来,

spike.initializer.run()

This line runs the spike initializer in the default session, that you've already started.此行在您已经启动的默认会话中运行spike初始值设定项。

Note that, first of all, although the code is perfectly valid, it's not typical in real application.请注意,首先,虽然代码完全有效,但在实际应用中并不典型。 More commonly, there's separation of responsibilities: the model is defined in one or more source files and gets executed in another file or files.更常见的是,职责分离:模型在一个或多个源文件中定义,并在另一个文件或多个文件中执行。 The initialization logically belongs to the latter, because only when the session starts, the memory gets allocated.初始化逻辑上属于后者,因为只有在会话开始时,才会分配内存。

Secondly, const is not the only option to initialize the variable.其次,const 不是初始化变量的唯一选项。 Eg, Xavier initializer needs to have the whole graph structure to compute the number of incoming and outcoming connections, and deduce the standard deviation from them.例如, Xavier 初始化器需要具有整个图结构来计算传入和传出连接的数量,并从中推导出标准偏差。 It simply won't work if we tried to initialize the variable during the definition.如果我们试图在定义期间初始化变量,它根本无法工作。

I hope now tensorflow design gets clearer: initializer is a dedicated op.我希望现在 tensorflow 设计变得更清晰:初始化程序一个专用操作。 Specifically for your use-case, tensorflow has released eager mode , an imperative, define-by-run interface where operations are executed immediately as they are called from Python.专门针对您的用例,tensorflow 发布了eager mode ,这是一个命令式的、按运行定义的接口,其中操作在从 Python 调用时立即执行。

You can start it like this:你可以这样开始:

import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution()

... and it will save you from the boilerplate, like above. ...它会像上面一样将您从样板中拯救出来。

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

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