1 TensorFlow的变量使用要注意两点
1.1 变量初始化、执行
变量要先初始化、再执行,这样变量才能起作用。
变量 tf.Variable() 中“V”是大写。
node1 = tf.Variable(3.0,tf.float32,name="node1")
node2 = tf.Variable(4.0,tf.float32,name="node2")
result = tf.add(node1,node2,name="add")
sess = tf.Session()
#所有变量初始化
init = tf.global_variables_initializer()
#执行
sess.run(init)
print(sess.run(result))
sess.close()
1.2 运行
使用了Variable变量类型,不进行初始化数值会出现运行错误。
如果把 上边 的代码的运行行 sess.run(init) 注释掉,出现一大堆错误。如下所示:
2 占位符placeholder
placeholder占位符,是TensorFlow中特有的一种数据结构,类似动态变量,函数的参数、或者C语言或者Python语言中格式化输出时的“%”占位符。
- 举例1
x = tf.placeholder(tf.float32,[2,3],name="tx")
#此代码生成一个2x3的二维数组,
#矩阵中每个元素的类型都是tf.float32,内部对应的符号是tx
3 变量赋值
3.1(输出1~10):
#变量赋值案例(输出1~10)
import tensorflow as tf
value = tf.Variable(0,name="value")
one = tf.constant(1)
new_value = tf.add(value,one)
update_value = tf.assign(value,new_value) #变量赋值语句
init = tf.global_variables_initializer()
#创建一个会话,并通过Python的上下文管理器来管理这个会话
with tf.Session() as sess:
#使用这创建好的会话来计算关心的结果
sess.run(init)
for _ in range(10):
sess.run(update_value)
print(sess.run(value))
#不需要再调用Session.close()函数来关闭会话
#当上下文退出时会话关闭和资源释放也自动完成了
3.2 (通过TensorFlow的变量赋值计算:1+2+3+…+10=?)
#通过TensorFlow的变量赋值计算:1+2+3+...+10=?
import tensorflow as tf
value = tf.Variable(0,name="value")
sum = tf.Variable(0,name="sum")
one = tf.constant(1)
new_value = tf.add(value,one)
update_value = tf.assign(value,new_value) #变量更新
new_sum = tf.add(sum,value)
update_sum = tf.assign(sum,new_sum) #变量更新
init = tf.global_variables_initializer()
#创建一个会话,并通过Python的上下文管理器来管理这个会话
with tf.Session() as sess:
#使用这创建好的会话来计算关心的结果
sess.run(init)
for _ in range(10):
sess.run(update_value)
sess.run(update_sum)
print("1+2+3+4+5+6+7+8+9+10=",end="")
print(sess.run(sum))
3.3 (通过TensorFlow的变量赋值计算:1+2+3+4+5+6+7+8+9+…+n=?)
#通过变量赋值输出1+2+3+4+5+6+7+8+9+...+n
import tensorflow as tf
value = tf.Variable(0,name="value")
sum = tf.Variable(0,name="sum")
one = tf.constant(1)
n = tf.placeholder(tf.int32,name='n') #占位符placeholder
new_value = tf.add(value,one)
update_value = tf.assign(value,new_value)
new_sum = tf.add(sum,value)
update_sum = tf.assign(sum,new_sum)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
number = int(input("请输入数字: "))
for i in range(number):
sess.run(update_value)
sess.run(update_sum)
result = sess.run(sum,feed_dict={n:number})
print(result)
3.4 feed 提交数据
#Feed提交数据
#如果构建了一个包含placeholder操作的计算图,当在session中调用run方法时,
#placeholder占用的变量必须通过 feed_dict参数传递进去,否则报错
import tensorflow as tf
a = tf.placeholder(tf.float32,name="a")
b = tf.placeholder(tf.float32,name="b")
c = tf.multiply(a,b,name="c")
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
#通过feed_dict的参数传值,按字典格式
result = sess.run(c,feed_dict={a:8.0,b:3.5})
print(result)
3.5 Fetch 提取数据
#多次操作可以通过一次feed完成执行
import tensorflow as tf
a = tf.placeholder(tf.float32,name="a")
b = tf.placeholder(tf.float32,name="b")
c = tf.multiply(a,b,name="c")
d = tf.subtract(a,b,name="d")
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
result = sess.run([c,d],feed_dict={a:[8.0,2.,3.5],b:[1.5,2.0,4.0]})
print(result)
print(result[0])
rc,rd = sess.run([c,d],feed_dict={a:[8.0,2.,3.5],b:[1.5,2.0,4.0]})
print(rc)
print(rd)