Variables
tf.Variable
Properties:
Properties | Usage | Return | Example for Print |
---|---|---|---|
w.device | 返回 该variable 所在的 device | 非Tensor | – |
w.dtype | 返回 该variable 的 DType | 非Tensor | <dtype: 'float32_ref'> |
w.graph | 返回 该variable 的 Graph | 非Tensor | <tensorflow.python.framework.ops.Graph object at 0x7fb06cdd2210> |
w.initial_value | 返回 该variable 的 初始值 | Tensor | 10.0 |
w.initializer | 返回 该variable 的 初始op | 非Tensor | (太长了就不放进来了) |
w.name | 返回 该variable 的 名称 | 非Tensor | Variable:0 |
w.op | 返回 该variable 的 op | 非Tensor | (太长了就不放进来了) |
w.shape | 返回 该variable 的 shape | 非tensor | () |
Methods:
Methods | Usage | Return | Example for Print |
---|---|---|---|
w.assign_add(10) | 返回 该variable 加 10 之后 的 variable | Tensor | 20.0 |
w.assign_sub(10) | 返回 该variable 减 10 之后 的 variable | Tensor | 10.0 |
w.assign(100) | 返回 重新赋值为 100 之后 的 variable | Tensor | 100.0 |
w.get_shape() | 返回 该variable 的 shape | 非Tensor | () |
w.initialized_value() | 返回 该variable 的 初始化值 | Tensor | 100.0 |
w.load(200) | 将 新值 载入 此variable | 无 | 200.0 |
w.read_value() | 返回 该variable 的 值 | Tensor | 200.0 |
w.set_shape() | 重置 该variable 的 shape | 无 | () |
w.value() | 返回 该variable 的 最后一个 快照 | Tensor | 200.0 |
# coding=utf-8
import tensorflow as tf
# 新建 variable
w = tf.Variable(initial_value=10., dtype=tf.float32)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
# Properties
print '\nw.device :\n', w.device
print '\nw.dtype :\n', w.dtype
print '\nw.graph :\n', w.graph
print '\nw.initial_value :\n', w.initial_value.eval()
print '\nw.initializer :\n', w.initializer
print '\nw.name :\n', w.name
print '\nw.op :\n', w.op
print '\nw.shape :\n', w.shape
# method
print '\ntf.add(w, 5) :\n', tf.add(w, 5).eval()
print '\nw.assign_add(10) :\n', w.assign_add(10).eval()
print '\nw.assign_sub(10) :\n', w.assign_sub(10).eval()
print '\nw.assign(100) :\n', w.assign(100).eval()
print '\nw.get_shape() :\n', w.get_shape()
print '\nw.initialized_value() :\n', w.initialized_value().eval()
w.load(200)
print '\nw.load(200) :\n', w.eval()
print '\nw.read_value() :\n', w.read_value().eval()
w.set_shape(())
print '\nw.set_shape() :\n', w.shape
print '\nw.value() :\n', w.value().eval()
sess.close()
结果输出:
w.device :
w.dtype :
<dtype: 'float32_ref'>
w.graph :
<tensorflow.python.framework.ops.Graph object at 0x7fb06cdd2210>
w.initial_value :
10.0
w.initializer :
name: "Variable/Assign"
op: "Assign"
input: "Variable"
input: "Variable/initial_value"
(折叠。。。)
w.name :
Variable:0
w.op :
name: "Variable"
op: "VariableV2"
(折叠。。。)
w.shape :
()
tf.add(w, 5) :
15.0
w.assign_add(10) :
20.0
w.assign_sub(10) :
10.0
w.assign(100) :
100.0
w.get_shape() :
()
w.initialized_value() :
100.0
w.load(200) :
200.0
w.read_value() :
200.0
w.set_shape() :
()
w.value() :
200.0
Variable helper functions
tf.global_variables
tf.local_variables
tf.model_variables
tf.trainable_variables
tf.moving_average_variables
tf.global_variables_initializer
tf.local_variables_initializer
tf.variables_initializer
tf.is_variable_initialized
tf.report_uninitialized_variables
tf.assert_variables_initialized
tf.assign
tf.assign_add
tf.assign_sub
Saving and Restoring Variables
tf.train.Saver
tf.train.latest_checkpoint
tf.train.get_checkpoint_state
tf.train.update_checkpoint_state
Sharing Variables
tf.get_variable
tf.get_local_variable
tf.VariableScope
tf.variable_scope
tf.variable_op_scope
tf.get_variable_scope
tf.make_template
tf.no_regularizer
tf.constant_initializer
tf.random_normal_initializer
tf.truncated_normal_initializer
tf.random_uniform_initializer
tf.uniform_unit_scaling_initializer
tf.zeros_initializer
tf.ones_initializer
tf.orthogonal_initializer