Tensorflow学习笔记(二)简单使用,Graph构建一个简单的计算图、as_default指定计算图

一、简单的向量相加

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
#动态图机制,disable处于不可使用状态,enable可使用状态
config = tf.compat.v1.ConfigProto(allow_soft_placement=True)
#对session会话进行参数控制,allow_soft_placement=True为允许TF自动分配设备,log_device_placement=True 为打印设备分配日志
config.gpu_options.per_process_gpu_memory_fraction = 0.9
#控制GPU利用率,GPU利用率100%会出现以下 warning:failed to allocate 10.91G (11715084288 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY
a = tf.constant([1.0,2.0],name = "a")
#定义第一个常向量
b = tf.constant([3.0,4.0],name = "b")
#定义第二个常向量
result = a + b
#向量求和
sess = tf.compat.v1.Session(config=config)
#Session为调用(定义)会话,调用这种方式时,要明确调用Session.close(),以释放资源。这样后面每调用一次sess.run(XXX)就会运行括号中的XXX
print(sess.run(result))

本段程序的运行结果如下:

2020-06-09 16:25:42.518535: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-06-09 16:25:42.520144: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-06-09 16:25:53.288731: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-06-09 16:25:53.586469: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x23c2ca5b490 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-06-09 16:25:53.586771: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-06-09 16:25:53.624048: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-06-09 16:25:53.796584: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce GTX 1080 Ti computeCapability: 6.1
coreClock: 1.62GHz coreCount: 28 deviceMemorySize: 11.00GiB deviceMemoryBandwidth: 451.17GiB/s
2020-06-09 16:25:53.799106: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-06-09 16:25:53.801374: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found
2020-06-09 16:25:53.929269: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-06-09 16:25:53.983001: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-06-09 16:25:54.150514: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-06-09 16:25:54.152444: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found
2020-06-09 16:25:54.154285: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found
2020-06-09 16:25:54.154501: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1598] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2020-06-09 16:25:54.405653: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-06-09 16:25:54.405913: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]      0 
2020-06-09 16:25:54.406067: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0:   N 
2020-06-09 16:25:54.432168: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x23c2d31ace0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-06-09 16:25:54.432508: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): GeForce GTX 1080 Ti, Compute Capability 6.1
[4. 6.]

Process finished with exit code 0

Tips:

①、为确保高版本的TF支持低版本的TF代码,升级脚本加入了 compat.v1 模块。此模块将以等效的 tf.compat.v1.foo 引用代替表单 tf.foo 的调用。不过,建议手动检查此类替代方案,并尽快将其迁移至 tf.* 命名空间(代替 tf.compat.v1.* 命名空间)中的新 API。

②、Session会话是tensorflow里面的重要机制,tensorflow构建的计算图必须通过Session会话才能执行,如果只是在计算图中定义了图的节点但没有使用Session会话的话,就不能运行该节点。比如在tensorflow中定义了两个矩阵a和b,和一个计算a和b乘积的c节点,如果想要得到a和b的乘积(也就是c节点的运算结果)的话,必须要建立Session会话,并调用Session中的run方法运行c节点才行

    1). session拥有管理CPU、GPU、网络连接的功能。
    2). session的主要参数有三个:
    target:用来控制session使用的硬件设备,如果为空值则调用本地设备;若取grpc://URL则调用该服务器控制的所有设备。
    config:用来指定一个configProto配置参数,
    graph:用来控制session运行哪个计算图,如果为空值则运行当前默认graph。

二、指定默认的计算图

使用Graph()函数创建一个默认的计算图,以及计算图初始化操作

import tensorflow  as tf
g1 = tf.Graph()
#Graph函数用于声明一个张量图方便后期调用
with g1.as_default():
#系统会自动维护一个默认的计算图,可以通过tf.get_default_graph()函数获取
#使用with g1.as_default():则在g1张量图中定义其中的变量a,b
    a = tf.compat.v1.get_variable("a", [2], initializer=tf.ones_initializer())
    b = tf.compat.v1.get_variable("b", [2], initializer=tf.zeros_initializer())
    #get_variable获取一个已经存在的变量或者创建一个新的变量,get_variable(name,shape=None,dtype=None,initializer=None,regularizer=None,trainable=True,collections=None,caching_device=None,partitioner=None,validate_shape=True,use_resource=None,custom_getter=None,constraint=None
g2 = tf.Graph()
with g2.as_default():
    a = tf.compat.v1.get_variable("a", [2], initializer=tf.zeros_initializer())
    b = tf.compat.v1.get_variable("b", [2], initializer=tf.ones_initializer())
with tf.compat.v1.Session(graph=g1) as sess:
    tf.compat.v1.global_variables_initializer ().run()
    #全局变量
    # 必须要使用global_variables_initializer的场合
	# 含有tf.Variable的环境下,因为tf中建立的变量是没有初始化的,也就是在debug时还不是一个tensor量,而是一个Variable变量类型
    with tf.compat.v1.variable_scope("",reuse = True):
    #用于定义创建变量(层)的操作的上下文管理器。
    #使用reuse=True共享变量
    #reuse:可以是True、None或tf.AUTO_REUSE;如果是True,则我们进入此范围的重用模式以及所有子范围;如果是AUTO_REUSE,则我们创建变量(如果它们不存在),否则返回它们;如果是None,则我们继承父范围的重用标志。当启用紧急执行时,该参数总是被强制为tf.AUTO_REUSE。
        print(sess.run(tf.compat.v1.get_variable("a")))
        print(sess.run(tf.compat.v1.get_variable("b")))
with tf.compat.v1.Session(graph=g2) as sess:
    tf.compat.v1.global_variables_initializer ().run()
    with tf.compat.v1.variable_scope("",reuse = True):
        print(sess.run(tf.compat.v1.get_variable("a")))
        print(sess.run(tf.compat.v1.get_variable("b")))

运行结果如下:

2020-06-10 14:29:40.579102: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-06-10 14:29:40.579695: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
WARNING:tensorflow:From D:\Program Files\anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py:1666: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
2020-06-10 14:29:43.403811: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-06-10 14:29:43.433739: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce GTX 1080 Ti computeCapability: 6.1
coreClock: 1.62GHz coreCount: 28 deviceMemorySize: 11.00GiB deviceMemoryBandwidth: 451.17GiB/s
2020-06-10 14:29:43.436801: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-06-10 14:29:43.439566: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found
2020-06-10 14:29:43.445581: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-06-10 14:29:43.447931: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-06-10 14:29:43.460084: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-06-10 14:29:43.463610: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found
2020-06-10 14:29:43.467073: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found
2020-06-10 14:29:43.467478: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1598] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2020-06-10 14:29:43.468698: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-06-10 14:29:43.480369: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x2541d4e90e0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-06-10 14:29:43.480884: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-06-10 14:29:43.481280: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-06-10 14:29:43.481635: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]      
[1. 1.]
[0. 0.]
2020-06-10 14:29:43.819395: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-06-10 14:29:43.819648: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]      
[0. 0.]
[1. 1.]

值得注意的是:
当with tf.compat.v1.Session(graph=g2) as sess:部分的程序改成:

......
......
......
with tf.compat.v1.Session(graph=g2) as sess:
    tf.compat.v1.global_variables_initializer ().run()
    with tf.compat.v1.variable_scope("",reuse = True):
        print(sess.run(a))
        print(sess.run(b))

得到的结果和上面的结果一样。但是并不代表二者相同,具体不同在后期学习中在查证。

你可能感兴趣的:(Tensorflow学习笔记(二)简单使用,Graph构建一个简单的计算图、as_default指定计算图)