第二天
今天主要学习tensorflow的计算模型,数据模型和运行模型
计算图
tensorflow中每一个计算都是计算图上的一个节点,而节点之间的边描述了计算之间的依赖关系。
import tensorflow as tf
g1=tf.Graph()
with g1.as_default():
v=tf.get_variable("v",shape=[1],initializer=tf.ones_initializer())
with tf.Session(graph=g1) as sess:
tf.initialize_all_variables().run()
with tf.variable_scope("",reuse=True):
print(sess.run(tf.get_variable("v")))
tf.Graph.device函数可以运行在制定的设备上(这可以为tensorflow使用了GPU机制)
数据模型-张量
张量可以简单理解为多维数组
一个张量主要保存了3个属性:名字(name),维度(shape),类型(type)
a=tf.constant([1.0,2.0],name="a",dtype=tf.float32)
运行模型-会话
会话拥有并管理tensorflow程序运行的所有资源
#创建一个会话,并通过python中上下文管理这个会话
with tf.Session() as sess:
sess.run(......)
#不需要再调用“Session.close()”函数来关闭会话