上QQ阅读APP看书,第一时间看更新
How to do it...
We proceed with the recipe as follows:
- We consider a simple example of adding two vectors, we have two inputs vectors v_1 and v_2 they are to be fed as input to the Add operation. The graph we want to build is as follows:
- The corresponding code to define the computation graph is as follows:
v_1 = tf.constant([1,2,3,4])
v_2 = tf.constant([2,1,5,3])
v_add = tf.add(v_1,v_2) # You can also write v_1 + v_2 instead
- Next, we execute the graph in the session:
with tf.Session() as sess:
prin(sess.run(v_add))
The above two commands are equivalent to the following code. The advantage of using with block is that one need not close the session explicitly.
sess = tf.Session()
print(ses.run(tv_add))
sess.close()
- This results in printing the sum of two vectors:
[3 3 8 7]
Remember that each Session needs to be explicitly closed using the close() method, with block implicitly closes the session when it ends.