一.变量声明
#Declare variables
#create variable a with scalar value
a = tf.Variable(2, name="scalar")
#create variable b as a vector
b = tf.Variable([2, 3], name="vector)
#create variable c as a 2x2 matrix
c = tf.Variable([[0, 1], [2, 3]], name="matrix")
# create variable W as 784 x 10 tensor, filled with zeros
W = tf.Variable(tf.zeros([784,10]))
二.变量初始化
#一次性初始化所有变量
init = tf.global_variables_initializer()
with tf.Session() as sess:
tf.run(init)
#初始化变量的子集:tf.variables_initializer()
init_ab=tf.variables_initializer([a,b],name="init_ab")#[a,b]为想初始化的变量列表
with tf.Session() as sess:
tf.run(init_ab)
#分开初始化每一个变量:tf.Variable.initializer
# create variable W as 784 x 10 tensor, filled with zeros
W = tf.Variable(tf.zeros([784,10]))
with tf.Session() as sess:
tf.run(W.initializer)
三.评估变量的值
# W is a random 700 x 100 variable object
W = tf.Variable(tf.truncated_normal([700, 10]))
with tf.Session() as sess:
sess.run(W.initializer)
print(W)
>> Tensor("Variable/read:0", shape=(700, 10), dtype=float32)
#To get the value of a variable, we need to evaluate it using eval()
# W is a random 700 x 100 variable object
W = tf.Variable(tf.truncated_normal([700, 10]))
with tf.Session() as sess:
sess.run(W.initializer)
print(W.eval())
四.赋值
#赋值采用:tf.Variable.assign()
W=tf.Variable(10)
W.assign(100)
with tf.Session() as sess:
sess.run(W.initializer)
print(W.eval())#>>10
#assign()会构造一个对象
W = tf.Variable(10)
assign_op = W.assign(100)
with tf.Session() as sess:
sess.run(assign_op)
print(W.eval())# >> 100
五.数学运算
W = tf.Variable(10)
with tf.Session() as sess:
sess.run(W.initializer)
print(sess.run(W.assign_add(10)))# >> 20
print(sess.run(W.assign_sub(2)))# >> 18
六.补充说明
#Tensorflow的每个session独立维护相应的值,每一个session都可以有自己独立的当前值
W = tf.Variable(10)
sess1 = tf.Session()
sess2 = tf.Session()
sess1.run(W.initializer)
sess2.run(W.initializer)
print(sess1.run(W.assign_add(10))) # >> 20
print(sess2.run(W.assign_sub(2))) # >> 8
print(sess1.run(W.assign_add(100))) # >> 120
print(sess2.run(W.assign_sub(50))) # >> -42
sess1.close()
sess2.close()
#声明一个依赖于其他变量的变量
# W is a random 700 x 100 tensor
W = tf.Variable(tf.truncated_normal([700, 10]))
U = tf.Variable(W * 2)
U = tf.Variable(W.intialized_value() * 2)#确保W在给U赋值前已经初始化完毕
#定义一个placeholder:tf.placeholder(dtype, shape=None, name=None)
# create a placeholder of type float 32-bit, shape is a vector of 3 elements
a = tf.placeholder(tf.float32, shape=[3])
# create a constant of type float 32-bit, shape is a vector of 3 elements
b = tf.constant([5, 5, 5], tf.float32)
# use the placeholder as you would a constant or a variable
c = a + b # Short for tf.add(a, b)
#If we try to fetch c, we will run into error.
with tf.Session() as sess:
print(sess.run(c))
>> NameError
#This runs into an error because to compute c, we need the value of a, but a is just a #placeholder
#without actual value. We have to first feed actual value into a.
with tf.Session() as sess:
# feed [1, 2, 3] to placeholder a via the dict {a: [1, 2, 3]}
# fetch value of c
print(sess.run(c, {a: [1, 2, 3]}))
>> [6. 7. 8.]
with tf.Session() as sess:
for a_value in list_of_a_values:
print(sess.run(c, {a: a_value}))