TensorFlow Ops
这是阅读 http://web.stanford.edu/class/cs20si/syllabus.html slides_02.pdf 的笔记.
- 基本操作
- Tensor 类型
- Project speed dating
- Placeholders and feeding inputs
- Lazy loading
第一个 TensorFlow 程序
import tensorflow as tf
a = tf.constant(2)
b = tf.constant(3)
x = tf.add(a,b)
with tf.Session() as sess:
print(sess.run(x))
5
使用 TensorBoard 可视化
TensorFlow 是一个可视化的工具, 可以展示运算过程中的计算图等信息.
import tensorflow as tf
a = tf.constant(2)
b = tf.constant(3)
x = tf.add(a,b)
with tf.Session() as sess:
# add this line to use TensorBoard.
writer = tf.summary.FileWriter("./graphs",sess.graph)
print(sess.run(x))
writer.close() # close the writer when you're done using it
5
在本地文件,可以看到 graphs 目录中有一个文件.
启动 TensorBoard
打开 Anaconda Prompt, 激活到安装有 TensorFlow 的环境. 然后 cd 到 graphs 上层目录.
运行 tensorboard --logdir = graphs
, 然后在浏览器中输入 http://localhost:6006/ 即可进入 TensorBoard.
在网页中点击 Graph 就可以看到一些东西了.
注意到这里两个是显示 Const, Const_1 , 那么如何给它们改名字呢?
import tensorflow as tf
a = tf.constant(2,name = "a")
b = tf.constant(3,name="b")
x = tf.add(a,b,name="add")
with tf.Session() as sess:
writer = tf.summary.FileWriter("./graphs",sess.graph)
print(sess.run(x))
5
再通过 tensorboard 可以看到 graph 对应节点有名字了
constant 的更多例子
tf.constant(value, dtype = None, shape = None, name = "Const", verify_shape = Flase)
import tensorflow as tf
# 注意 a,b 维度不同, 在运算时发生了 boardcasting
# 更多关于 https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html
# https://www.tensorflow.org/api_guides/python/math_ops#Arithmetic_Operators
a = tf.constant([2,2],name="a")
b = tf.constant([[0,1],[2,3]],name="b")
x = tf.add(a,b,name="add")
y = tf.multiply(a,b,name="mul")
with tf.Session() as sess:
x = sess.run(x)
print(x)
print('-----------------')
y = sess.run(y)
print(y)
[[2 3]
[4 5]]
-----------------
[[0 2]
[4 6]]
import tensorflow as tf
# 生成指定维度的零 tensor
# 一般定义: tf.zeros(shape, dtype=tf.float32, name=None)
v1 = tf.zeros([3,4],tf.int32)
# 值填充
# tf.fill(dims, value, name = none)
v2 = tf.fill([2,3],4)
with tf.Session() as sess:
print(sess.run(v1))
print(sess.run(v2))
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
[[4 4 4]
[4 4 4]]
Variables
# 官方文档: https://www.tensorflow.org/programmers_guide/variables
## 创建变量
import tensorflow as tf
a = tf.Variable(2, name = "scalar")
b = tf.Variable(tf.truncated_normal([2,3]))
# 初始化变量
# 一次初始化所有变量
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(a))
print(sess.run(b))
# provide name and shape
#v1 = tf.get_variable("v1",[1,2,3])
#print(v1)
2
[[-0.89090335 -0.62117159 0.95811218]
[ 1.0819943 0.74583757 -0.73409617]]
import tensorflow as tf
# 变量初始值为 10
w = tf.Variable(10)
# 赋值为 100
w.assign(100)
with tf.Session() as sess:
sess.run(w.initializer)
print(w.eval()) # 但是结果仍然是 10, 为什么
10
import tensorflow as tf
# 变量初始值为 10
w = tf.Variable(10)
# 赋值为 100
assign_op = w.assign(100)
with tf.Session() as sess:
sess.run(assign_op)
print(w.eval()) # 这时的结果就正确了
100
Placeholders (占位符)
TF 程序通常有两个阶段:
- assemble a graph
- use a session to execute operations in the graph.
import tensorflow as tf
# 创建一个 placeholder
a = tf.placeholder(tf.float32,shape=[3])
b = tf.constant([5,5,5],tf.float32)
c = a + b
with tf.Session() as sess:
# 用字典来初始化 a
print(sess.run(c,{a:[1,2,3]}))
[ 6. 7. 8.]