您的位置:首页 > 编程语言 > Go语言

Tensorflow 实战google深度学习框架 01

2018-03-10 15:54 981 查看
定义两个不同的图

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: