#定义两个不同的图
import tensorflow as tf
g1 = tf.Graph()#创建图g1
with g1.as_default():
a = tf.get_variable("v",shape=(1),dtype=tf.float32,initializer=tf.zeros_initializer())
g2 = tf.Graph()
with g2.as_default():#创建图g2
b = tf.get_variable("v",shape=(1),dtype=tf.float32,initializer=tf.ones_initializer())#shape=[1]或者shape=(1)都可以
with tf.Session(graph=g1) as sess:
sess.run(tf.initialize_all_variables())#现在变量初始化都用tf.global_variables_initializer(),tf.initialize_all_variables()将被弃用
with tf.variable_scope("",reuse=True):
v=tf.get_variable("v")
print(sess.run(v))
with tf.Session(graph = g2) as sess:
sess.run(tf.global_variables_initializer())
with tf.variable_scope("",reuse=True):#reuse=True表示可以获取定义的变量,reuse=False表示定义变量
v = tf.get_variable("v")#获取的上面tf.get_variable()参数中定义的变量名
print(sess.run(v))
结果如下:
WARNING:tensorflow:From F:\Anaconda3\lib\site-packages\tensorflow\python\util\tf_should_use.py:118: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
[ 0.]
[ 1.]
#关于张量的使用
import tensorflow as tf
a = tf.constant([1.0,2.0],name = "a")#定义常量为一维列表
b = tf.constant([3.0,4.0],name = 'b')
result = a + b
print(result)#输出的是一个张量而不是运行结果
sess = tf.InteractiveSession()#建立会话并自动加入默认会话不需要使用with sess.as_default():语句了
print(result.eval())#tf.InteractiveSession()和eval()函数配套使用
sess.close()#记得关闭会话,要不然会占用资源
结果如下:
Tensor("add_1:0", shape=(2,), dtype=float32)
[ 4. 6.]
#会话的使用(4种个方式)
import tensorflow as tf
a = tf.constant([1.0,2.0],name = 'a')
b = tf.constant([3.0,4.0],name = 'b')
result = a + b
#方式一
sess = tf.Session()
print(sess.run(result))
sess.close()
#方式二
sess = tf.InteractiveSession()#创建会话并加入默认会话,和方式三效果一样
#以下两个具有相同的功能
print(result.eval())
print(sess.run(result))
sess.close()
#方式三
sess = tf.Session()
with sess.as_default():#加入默认会话
print(sess.run(result))
sess.close()
#方式四
with tf.Session() as sess:#使用上下文管理器创建会话,好处是不用使用sess.close()来关闭会话,使用完管理器会自动关闭会话
print(sess.run(result))
结果如下:
[ 4. 6.]
[ 4. 6.]
[ 4. 6.]
[ 4. 6.]
[ 4. 6.]
#通过ConfigProto配置会话
import tensorflow as tf
a = tf.constant([1.0,2.0],name = 'a')
b = tf.constant([3.0,4.0],name = 'b')
result = a + b
#allow_soft_placement=True表示下列三种情况下用cpu
#1.无指定gpu资源可用(指定两台gpu,只有一个gpu)2.计算中包含有cpu计算结果的引用3.运算无法在gpu上运行
#log_device_placement=True表示在控制台中输入运行日志,也就是运算是在什么设备上运行的
config = tf.ConfigProto(allow_soft_placement = True,log_device_placement = True)
with tf.Session(config = config) as sess:#四种方式都可以在session配置
sess.run(result)
结果:
add: (Add): /job:localhost/replica:0/task:0/device:GPU:0
2018-05-14 11:13:30.388069: I C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:875] add: (Add)/job:localhost/replica:0/task:0/device:GPU:0
b: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2018-05-14 11:13:30.393693: I C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:875] b: (Const)/job:localhost/replica:0/task:0/device:GPU:0
a: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2018-05-14 11:13:30.398606: I C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:875] a: (Const)/job:localhost/replica:0/task:0/device:GPU:0