Tensorflow 实战google深度学习框架 01
2018-03-10 15:54
981 查看
定义两个不同的图
[0.]
[1.]
2.张量的概念
会话的使用
3.1 创建和关闭会话
[3. 5.]
3.2 使用with statement 来创建会话
[3. 5.]
3.3 指定默认会话
[3. 5.]
[3. 5.]
[3. 5.]
使用tf.InteractiveSession构建会话
[3. 5.]
[3. 5.]
通过ConfigProto配置会话
import tensorflow as tf g1 = tf.Graph() with g1.as_default(): v = tf.get_variable("v", [1], initializer = tf.zeros_initializer()) # 设置初始值为0 g2 = tf.Graph() with g2.as_default(): v = tf.get_variable("v", [1], initializer = tf.ones_initializer()) # 设置初始值为1 with tf.Session(graph = g1) as sess: tf.global_variables_initializer().run() with tf.variable_scope("", reuse=True): print(sess.run(tf.get_variable("v"))) with tf.Session(graph = g2) as sess: tf.global_variables_initializer().run() with tf.variable_scope("", reuse=True): print(sess.run(tf.get_variable("v")))
[0.]
[1.]
2.张量的概念
a = tf.constant([1.0, 2.0], name="a") b = tf.constant([2.0, 3.0], name="b") print(a.eval()) result = a + b print(result) # Tensor("add:0", shape=(2,), dtype=float32) sess = tf.InteractiveSession () print(result.eval()) # [3. 5.]
会话的使用
3.1 创建和关闭会话
# 创建一个会话。 sess = tf.Session() # 使用会话得到之前计算的结果。 print(sess.run(result)) # 关闭会话使得本次运行中使用到的资源可以被释放。 sess.close()
[3. 5.]
3.2 使用with statement 来创建会话
with tf.Session() as sess: print(sess.run(result))
[3. 5.]
3.3 指定默认会话
sess = tf.Session() with sess.as_default(): print(result.eval())
[3. 5.]
sess = tf.Session() # 下面的两个命令有相同的功能。 print(sess.run(result)) # [3. 5.] print(result.eval(session=sess)) # [3. 5.]
[3. 5.]
[3. 5.]
使用tf.InteractiveSession构建会话
sess = tf.InteractiveSession () print(result.eval()) # [3. 5.] sess.close() sess = tf.InteractiveSession() with sess.as_default(): print(sess.run(result)) # [3. 5.] sess.close()
[3. 5.]
[3. 5.]
通过ConfigProto配置会话
config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True) sess1 = tf.InteractiveSession(config=config) sess2 = tf.Session(config=config)
相关文章推荐
- 迁移学习源码全注释 - 《Tensorflow 实战 Google 深度学习框架》源码注释
- Tensorflow 实战Google深度学习框架(完整版) 百度网盘
- Tensorflow 实战google深度学习框架 02
- Tensorflow 实战google深度学习框架 09 计算图保存,与模型读取
- Tensorflow 实战 google 深度学习框架 笔记(一)滑动模型
- (Tensorflow之八)MNIST数字识别源码--实战Google深度学习框架5.2小节
- Tensorflow 实战google深度学习框架 06 正则化
- TensorFlow:实战Google深度学习框架(二)实现简单神经网络
- TensorFlow:实战Google深度学习框架(四)MINIST数据集识别问题
- Tensorflow 实战Google深度学习框架
- TensorFlow:实战Google深度学习框架(五)图像识别与卷积神经网络
- TensorFlow:实战Google深度学习框架,数据增强
- TensorFlow:实战Google深度学习框架(六)图像数据处理
- Tensorflow 实战google深度学习框架 07 滑动平均模型
- Tensorflow 实战google深度学习框架 08 变量域
- Tensorflow 实战google深度学习框架 05 学习率设置
- 免费教材丨第55期:Python机器学习实践指南、Tensorflow 实战Google深度学习框架
- TensorFlow:实战Google深度学习框架(一)计算、数据、运行模型
- Tensorflow 实战google深度学习框架 10 pb文件的保存&加载
- Tensorflow 实战google深度学习框架 03