[AI combat] Quickly master TensorFlow (2): Computational graph, conversation


In the previous articles, we have completed the construction of the AI ​​basic environment (see the article: Ubuntu + Anaconda + TensorFlow + GPU + PyCharm to build the AI ​​basic environment ), and have a preliminary understanding of the characteristics and basic operations of TensorFlow (see the article: Quick Mastery TensorFlow (1) ), and then continue to learn to master TensorFlow.

This article mainly learns to master the computational graph and session operations of TensorFlow.

 

 
Computational graph is the core concept of TensorFlow, which uses graph (Graph) to represent computing tasks and consists of nodes and edges. In TensorFlow, the front-end is responsible for building the computational graph, and the back-end is responsible for executing the computational graph.
In order to perform graph computation, the graph must be started in a session, which distributes graph operations to CPU, GPU and other devices for execution.
The following will introduce how to create sessions, graphs, and basic operations in TensorFlow.

1. Graph
The TensorFlow Python library already has a default graph. If no new computational graph is created, nodes and edges are created in this default graph by default.
Adding nodes to a graph is very convenient. For example, to create such a calculation graph, add two tensors, as shown below: The
 
code is as follows:

import tensorflow as tf 
a=tf.constant([1.0,2.0], name='a') 
b=tf.constant([3.0,4.0], name='b') 
result = tf.add(a,b)

The default graph now has three nodes, two constant(), and one add().
In order to actually add two tensors and get the result, the graph must be started in the session.

2. Session
To start the computational graph, first create a Session object.
Use tf.Session() to create a session and call the run() function to execute the computation graph. If no creation parameters are passed in, the session constructor will start the default graph. If you want to specify a calculation graph, pass in the calculation graph parameters (such as g1), then create a session as tf.Session(graph=g1) There are three main ways to create a session (Session):
(1) Create a session

#启动默认图
sess=tf.Session()
result_value = sess.run(result)
print(result_value)
# ==> [4.0 6.0]

# 任务完成, 关闭会话.
sess.close()

(2) Create a session
Session needs to be closed to release resources after use. In addition to explicitly calling close, you can also use the "with" code block to automatically complete the closing action. code show as below:

with tf.Session() as sess:
    result_value = sess.run(result)
    print(result_value)
    # ==> [4.0 6.0]

(3) Create a default session

sess=tf.Session()
with sess.as_default():
    result_value = result.eval()
    print(result_value)

When the default session is specified, the value of a tensor can be calculated by the tf.Tensor.eval function.

(4) Create an interactive session
In an interactive environment (such as IPython), it is more convenient to use the method of setting a default session to obtain the value of a tensor. TensorFlow provides a function to directly build a default session in an interactive environment : tf.InteractiveSession, this function will automatically register the generated session as the default session, use tf.Tensor.eval() instead of Session.run(), the code is as follows:

sess= tf.InteractiveSession()
result_value = result.eval()
print(result_value)
sess.close()

3. Build multiple computational graphs
In TensorFlow, you can build multiple computational graphs. The tensors and operations between computational graphs will not be shared. In this way, multiple network models can be built in the same project, while will not affect each other.
Use the tf.Graph() function to build a graph. The way to build multiple computational graphs is as follows:

# 构建计算图g1
g1=tf.Graph()
with g1.as_default():
    # 在计算图g1中定义变量'v',并设置初始值为0。
    v=tf.get_variable('v',initializer=tf.zeros_initializer()(shape = [1]))
    
# 构建计算图g2
g2=tf.Graph()
with g2.as_default():
    # 在计算图g2中定义变量'v',并设置初始值微1。
    v=tf.get_variable('v',initializer=tf.ones_initializer()(shape = [1]))

# 在计算图g1中读取变量'v'的取值
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')))
        # 输出结果[0.]

# 在计算图g2中读取变量'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')))
        # 输出结果[1.]。

4. Specify the running device
If the computer has multiple GPUs, you can specify the running device in the graph and session
(1) Specify the running device in the graph

g=tf.Graph()
# 指定计算运行的设备。
with g.device('/gpu:0'):
    result=tf.add(a,b)

(2) Specify the running device in the session

with tf.Session() as sess:
  with tf.device("/gpu:0"):
    result=tf.add(a,b)

The running device is identified by a string, currently supported devices include:

  • "/cpu:0": the CPU of the machine
  • "/gpu:0": the machine's first GPU, if any
  • "/gpu:1": the second GPU of the machine, and so on

Through the above introduction, you have understood the basic operations of graphs and sessions, using graphs to represent computing tasks, and using sessions to execute graphs.

Next, we will have more exciting content explaining TensorFlow, so stay tuned!

 

Welcome to follow my WeChat public account "Big Data and Artificial Intelligence Lab" (BigdataAILab) for more information

 

Recommended related reading

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324119649&siteId=291194637