官术网_书友最值得收藏!

Computation graph

A computation graph is the basic unit of computation in TensorFlow. A computation graph consists of nodes and edges. Each node represents an instance of tf.Operation, while each edge represents an instance of tf.Tensor that gets transferred between the nodes.

A model in TensorFlow contains a computation graph. First, you must create the graph with the nodes representing variables, constants, placeholders, and operations, and then provide the graph to the TensorFlow execution engine. The TensorFlow execution engine finds the first set of nodes that it can execute. The execution of these nodes starts the execution of the nodes that follow the sequence of the computation graph.

Thus, TensorFlow-based programs are made up of performing two types of activities on computation graphs:

  • Defining the computation graph
  • Executing the computation graph

A TensorFlow program starts execution with a default graph. Unless another graph is explicitly specified, a new node gets implicitly added to the default graph. Explicit access to the default graph can be obtained using the following command:

graph = tf.get_default_graph()

For example, the following computation graph represents the addition of three inputs to produce the output, that is, :

In TensorFlow, the add operation node in the preceding diagram would correspond to the code y = tf.add( x1 + x2 + x3 ).

The variables, constants, and placeholders get added to the graph as and when they are created. After defining the computation graph, a session object is instantiated that executes the operation objects and evaluates the tensor objects.

Let's define and execute a computation graph to calculate , just like we saw in the preceding example: 

# Linear Model y = w * x + b
# Define the model parameters
w = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
# Define model input and output
x = tf.placeholder(tf.float32)
y = w * x + b
output = 0

with tf.Session() as tfs:
# initialize and print the variable y
tf.global_variables_initializer().run()
output = tfs.run(y,{x:[1,2,3,4]})
print('output : ',output)

Creating and using a session in the with block ensures that the session is automatically closed when the block is finished. Otherwise, the session has to be explicitly closed with the tfs.close() command, where tfs is the session name.

主站蜘蛛池模板: 张家港市| 许昌县| 晴隆县| 社旗县| 汨罗市| 晋宁县| 沅江市| 隆化县| 西乡县| 津市市| 安顺市| 荃湾区| 沙田区| 确山县| 开江县| 武功县| 西畴县| 苏尼特左旗| 富源县| 禄丰县| 肥城市| 当阳市| 龙门县| 红安县| 滨州市| 宁德市| 信丰县| 繁昌县| 江都市| 霍林郭勒市| 依安县| 肇东市| 涞源县| 从江县| 蓬莱市| 盐边县| 南漳县| 平陆县| 平果县| 苏尼特左旗| 云龙县|