【深度学习】note01:tensorflow的Variable

The `Variable()` constructor requires an initial value for the variable,
which can be a `Tensor` of any type and shape. The initial value defines the
type and shape of the variable. After construction, the type and shape of
the variable are fixed. The value can be changed using one of the assign
methods.
# Create a variable.
w = tf.Variable(<initial-value>, name=<optional-name>)

创建了Variable变量后,在运行graph之前,需要对所有变量进行初始化。

When you launch the graph, variables have to be explicitly initialized before
you can run Ops that use their value. You can initialize a variable by
running its *initializer op*, restoring the variable from a save file, or
simply running an `assign` Op that assigns a value to the variable. In fact,
the variable *initializer op* is just an `assign` Op that assigns the
variable's initial value to the variable itself.

更简单的做法是执行tf.global_variables_initializer(),这个是初始化模型的所有参数。

代码如下:

import tensorflow as tf

#创建变量w
w = tf.Variable(tf.zeros([10]), name='weights')

#创建会话
sess = tf.Session()

#初始话变量
init_op = tf.global_variables_initializer()

sess.run(init_op)
print(sess.run(w))            

从网站https://blog.csdn.net/u012436149/article/details/78291545得知:global_variables_initializer()通过源码可以做了以下工作:

一步步看源代码:(代码在后面)

  • global_variables_initializer 返回一个用来初始化 计算图中 所有global variable的 op。 
    • 这个op 到底是啥,还不清楚。
    • 函数中调用了 variable_initializer() 和 global_variables()
  • global_variables() 返回一个 Variable list ,里面保存的是 gloabal variables
  • variable_initializer() 将 Variable list 中的所有 Variable 取出来,将其 variable.initializer 属性做成一个 op group
  • 然后看 Variable 类的源码可以发现, variable.initializer 就是一个 assign op

所以: sess.run(tf.global_variables_initializer()) 就是 run了 所有global Variable 的 assign op,这就是初始化参数的本来面目。

def global_variables_initializer():
  """Returns an Op that initializes global variables.
  Returns:
    An Op that initializes global variables in the graph.
  """
  return variables_initializer(global_variables())

def global_variables():
  """Returns global variables.
  Returns:
    A list of `Variable` objects.
  """
  return ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)

def variables_initializer(var_list, name="init"):
  """Returns an Op that initializes a list of variables.
  Args:
    var_list: List of `Variable` objects to initialize.
    name: Optional name for the returned operation.

  Returns:
    An Op that run the initializers of all the specified variables.
  """
  if var_list:
    return control_flow_ops.group(*[v.initializer for v in var_list], name=name)
  return control_flow_ops.no_op(name=name)


class Variable(object):
    def _init_from_args(self, ...):
        self._initializer_op = state_ops.assign(
            self._variable, self._initial_value,
            validate_shape=validate_shape).op
    @property
    def initializer(self):
        """The initializer operation for this variable."""
        return self._initializer_op

猜你喜欢

转载自blog.csdn.net/ghalcyon/article/details/82153531