Next up, we want to now evaluate an expression from given input values. Let us construct a function (graph)
f(x,y) = x + y
where x,y are input values to fed into the graph f. To do this, we need to use tf.placeholder methods to define input nodes, and supply feed_dict parameter to run method as shown below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import numpy as np | |
# input nodes | |
x = tf.placeholder(tf.int32) | |
y = tf.placeholder(tf.int32) | |
# operation | |
f = tf.add(x, y) | |
with tf.Session() as sess: | |
inputs = np.random.randint(0, 10, size=(10,2)) | |
for _x, _y in inputs: | |
print _x, '+', _y, '=', sess.run(f, feed_dict={x:_x, y:_y}) |
The code is easy enough to be self-explanatory. The output of the code shall look similar to
$ python tf_computation_graph_p3.py 2>/dev/null
8 + -6 = 2
-6 + 4 = -2
-10 + -1 = -11
-6 + 0 = -6
5 + 9 = 14
7 + 6 = 13
3 + 8 = 11
3 + 6 = 9
5 + -4 = 1
0 + -3 = -3
8 + -6 = 2
-6 + 4 = -2
-10 + -1 = -11
-6 + 0 = -6
5 + 9 = 14
7 + 6 = 13
3 + 8 = 11
3 + 6 = 9
5 + -4 = 1
0 + -3 = -3
No comments:
Post a Comment