TensorFlow Learning Process Record (2) -- Basic Use (4) -- Fetch and Feed

5. Fetch

In order to retrieve the output of the operation, you can use the run() of the Session object to call the execution graph, passing in some tensors, which will help you retrieve the results. In the previous example, we only retrieved the state of a single node , but you can also retrieve multiple tensors:

input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermed = tf.add(input2, input3)
mul = tf.multiply(input1, intermed)

with tf.Session() as sess:
    result = sess.run([mul, intermed])
    print(result)  # ==> [21.0, 7.0]

You need to get the values ​​of multiple tensors together in one run of op (instead of getting the tensors one by one).

The mul method in the original document is the same as the sub that appeared earlier, it is an expired method, which has been corrected in the above code.
Get the running result: [21.0, 7.0] This is not the same as [array([ 21.], dtype=float32), array([ 7.], dtype=float32)] given in the document. Like the sub() and mul() methods, they should all be version reasons.

6. Feed

The above example introduces tensors in the computation graph, which are stored in the form of constants or variables. TensorFlow also provides a feed mechanism, which can temporarily replace tensors in any operation in the graph. Patches can be submitted for any operation in the graph, and one can be inserted directly. tensor.feed
Temporarily replaces the output of an operation with a tensor value. You can provide feed data as an argument to the run() call. The feed is only valid within the method that called it, and when the method ends, the feed disappears. Most common use case is to designate some special operations as "feed" operations, and the way to mark them is to use tf.placeholder() to create placeholders for these operations.

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)

with tf.Session() as sess:
    print(sess.run([output], feed_dict={input1: [7.], input2: [2.]}))  # ==>[array([14.], dtype=float32)]

The placeholder() operation will generate an error if the feed is not provided correctly.

Guess you like

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