tensorflow概述
一、基本知识
- graph 表示计算任务,op是graph中的结点;
- 会话Session执行计算;
- tensor表示数据;
- Variable表示变量。
二、构建和执行
1.构造图:
matrix = tf.contant([[3.,3.]]) # 创建一个op节点,产生1x2矩阵。
2.在会话中启动图:
sess = tf.Session()
result = sess.run(product) #product为一个图节点
print(result)
sess.close() #关闭会话
# 用with完成关闭
with tf.Session() as sess:
result = sess.run(product)
print(result)
二、Tensor
1.变量
tf.Variable(0,name='counter') #创建变量,初始化为0
2. Fetch
result = sess.run([mul,intermed]) #可以取回mul和intermed的值
3.Feed
input1 = tf.placeholder(tf.types.float32) #占位
input2 = tf.placeholder(tf.types.float32)
output = tf.mul(input1,input2)
with tf.Session() as sess:
print(sess.run([output],feed_dict={input1:[7.],input2:[2.]}))
4.随机数
tf.random_normal #正态分布
tf.truncted_normal #正态分布,平均值超过2个标准差,重新随机
tf.random_uniform #均匀分布
tf.random_gamma #Gamma分布