tensorflow编程: Variables

Variables

tf.Variable

Properties:

PropertiesUsageReturnExample 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 的 初始值Tensor10.0
w.initializer返回 该variable 的 初始op非Tensor(太长了就不放进来了)
w.name返回 该variable 的 名称非TensorVariable:0
w.op返回 该variable 的 op非Tensor(太长了就不放进来了)
w.shape返回 该variable 的 shape非tensor()


Methods:

MethodsUsageReturnExample for Print
w.assign_add(10)返回 该variable 加 10 之后 的 variableTensor20.0
w.assign_sub(10)返回 该variable 减 10 之后 的 variableTensor10.0
w.assign(100)返回 重新赋值为 100 之后 的 variableTensor100.0
w.get_shape()返回 该variable 的 shape非Tensor()
w.initialized_value()返回 该variable 的 初始化值Tensor100.0
w.load(200)将 新值 载入 此variable200.0
w.read_value()返回 该variable 的 值Tensor200.0
w.set_shape()重置 该variable 的 shape()
w.value()返回 该variable 的 最后一个 快照Tensor200.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


Variable Partitioners for Sharding


Sparse Variable Updates


Exporting and Importing Meta Graphs



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值