TensorFlow learning process record (2) -- basic use (3) -- variables

Before introducing variables, let's explain the word 'tensor':

3. tensor

The documentation says this:

TensorFlow programs use the tensor data structure to represent all data. In the computation graph, the data passed between operations are all tensors.
You can think of a TensorFlow tensor as an n-dimensional array or list. A tensor contains a static type rank, and a shape.

4. Variables

Variables maintain state information during the execution of the graph.
Code given in the documentation:

# coding=utf-8
# 使用变量实现一个简单的计数器

import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# 创建一个变量, 初始化为标量 0.
state = tf.Variable(0, name="counter")

# 创建一个 op, 其作用是使 state 增加 1

one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)

# 启动图后, 变量必须先经过`初始化` (init) op 初始化,
# 首先必须增加一个`初始化` op 到图中.
init_op = tf.initialize_all_variables()

# 启动图, 运行 op
with tf.Session() as sess:
  # 运行 'init' op
  sess.run(init_op)
  # 打印 'state' 的初始值
  print(sess.run(state))
  # 运行 op, 更新 'state', 并打印 'state'
  for _ in range(3):
    sess.run(update)
    print(sess.run(state))

get the result of the operation
write picture description here

Note that the code is used state = tf.Variable(0, name="counter")to create a variable initialized to 0, and the signature of the Variable is:

 def __init__(self,
               initial_value=None,
               trainable=True,
               collections=None,
               validate_shape=True,
               caching_device=None,
               name=None,
               variable_def=None,
               dtype=None,
               expected_shape=None,
               import_scope=None,
               constraint=None):

Used to create a initial_valuevariable with a value.
Adds a new variable to the graph collections listed in "collections", with a default value of [GracKy.GualAlxValue].
If trainable is True the variable is also added to the graph collection "GrimeKest.Trimable Variables".
The constructor creates an variableop and an assignop to set the variable to its initial value.
parameter:

initial_value trainable
A , or a Python object Tensorconverted to , which is the initial value of the variable. Unless set to False, the initial value must specify a shape. It can also be called without parameters, and returns the initial value when called. In this case, it must be specified . Note that the initialization function from init_ops.py must be bound to a shape before it can be used.Tensorvalidate_shape“dType” If True, also adds the variable to the chart collection GraphKeys.TRAINABLE_VARIABLES, which is used as the default list of variables, Optimizerused through the class.
collections validate_shape
A list of keys for several charts, new variables are added to the set. default:[GraphKeys.GLOBAL_VARIABLES] If FALSE, allows the variable to be initialized with an unknown shape. Defaults to True, indicating that the shape of the variable must be explicit.

There are a few more properties, which will be explained later when they are encountered.
The code behind also uses an update = tf.assign(state, new_value)op.
The signature of assign is:

def assign(ref, value, validate_shape=None, use_locking=None, name=None):

Used to assign value to ref.

Then an initialization op is required:

init_op = tf.initialize_all_variables()

For a problem with this method, the next section will explain it
returns an op that initializes global variables

The assign() operation in the code is part of the expression depicted in the graph, just like the add() operation. So it doesn't actually perform the assignment until run() is called to execute the expression.
Usually a statistical model The parameters of is represented as a set of variables. For example, you can store the weights of a neural network as some variable in a tensor. During training, this tensor is updated by repeatedly running the training graph.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763444&siteId=291194637