TF.Variable
1.前言
在学习莫烦Tensorflow教学视频中有些东西不清楚,这篇是关于tf.Variable的拓展。
2.变量的创建
视频中用的tf.Variable(tf.zeros([1]))可以简写为tf.zeros([1])。
2.1生成Tensor
tf.zeros(shape, dtype=tf.float32, name=None)
tf.zeros_like(tensor, dtype=None, name=None)
tf.constant(value, dtype=None, shape=None, name='Const')
tf.fill(dims, value, name=None)
tf.ones_like(tensor, dtype=None, name=None)
tf.ones(shape, dtype=tf.float32, name=None)
2.2生成序列
类似于Matlab中的用法
tf.range(start, limit, delta=1, name='range')
tf.linspace(start, stop, num, name=None)
2.3生成随机矩阵
tf.random_normal(shape, mean=0.0, stddev=1.0,
dtype=tf.float32, seed=None, name=None)
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
tf.random_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None)
tf.random_shuffle(value, seed=None, name=None)
2.4.1例子
import tensorflow as tf
# 生成0和1矩阵
v1 = tf.Variable(tf.zeros([3, 3, 3]), name="v1")
v2 = tf.Variable(tf.ones([2, 3]), name="v2")
# 填充单值矩阵
v3 = tf.Variable(tf.fill([2, 3], 9))
# 常量矩阵
v4_1 = tf.constant([1, 2, 3, 4, 5, 6, 7])
v4_2 = tf.constant(-1.0, shape=[2, 3])
# 生成等差数列
v6_1 = tf.linspace(1.0, 10.0, 20, name="linspace") # float32 or float64
v7_1 = tf.range(10, 20, 3) # just int32
# 生成各种随机数据矩阵
# seed 为标准差
v8_1 = tf.random_uniform([2, 4], minval=0.0, maxval=2.0, dtype=tf.float32, seed=1234, name="v8_1")
v8_2 = tf.Variable(tf.random_normal([2, 3], mean=0.0, stddev=1.0, dtype=tf.float32, seed=1234, name="v8_2"))
v8_3 = tf.Variable(tf.truncated_normal([2, 3], mean=0.0, stddev=1.0, dtype=tf.float32, seed=1234, name="v8_3"))
v8_4 = tf.Variable(tf.random_uniform([2, 3], minval=0.0, maxval=1.0, dtype=tf.float32, seed=1234, name="v8_4"))
v8_5 = tf.random_shuffle([[1, 2, 3], [4, 5, 6], [6, 6, 6]], seed=134, name="v8_5")
# 初始化
init_op = tf.global_variables_initializer()
# 运行
with tf.Session() as sess:
sess.run(init_op)
print('v1')
print(v1)
print(tf.Variable.get_shape(v1))
print(sess.run(v1))
print('v2')
print(sess.run(v2))
print('v3')
print(sess.run(v3))
print('v4_1')
print(sess.run(v4_1))
print('v4_2')
print(sess.run(v4_2))
print('v6_1')
print(sess.run(v6_1))
print('v7_1')
print(sess.run(v7_1))
print('v8_1')
print(sess.run(v8_1))
print('v8_2')
print(sess.run(v8_2))
print('v8_3')
print(sess.run(v8_3))
print('v8_4')
print(sess.run(v8_4))
print('v8_5')
print(sess.run(v8_5))
2.4.2例子
import tensorflow as tf
state = tf.Variable(0, name='counter')
#print(state.name)
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value) # 赋值操作 将new_value赋值给state , 注意updata为操作。
init = tf.initialize_all_variables() # must have if define variable
with tf.Session() as sess:
sess.run(init)
for _ in range(3):
sess.run(update)
print(sess.run(state))
3.变量的初始化
变量的初始化必须在模型的其它操作运行之前先明确地完成。
3.1最简单的方法
添加一个给所有变量初始化的操作,并在使用模型之前首先运行那个操作。
# Create two variables.
weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),
name="weights")
biases = tf.Variable(tf.zeros([200]), name="biases")
...
# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()
# tf.initialize_all_variables()这是旧版本的用法
# Later, when launching the model
with tf.Session() as sess:
# Run the init operation.
sess.run(init_op)
...
# Use the model
...