TensorFlow(三)Fetch与Feed操作

2017-10-10  本文已影响501人  Oo晨晨oO

Fetch

Fetch操作是指TensorFlow的session可以一次run多个op
语法: 将多个op放入数组中然后传给run方法

import tensorflow as tf

#Fetch
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)

#定义两个op
add = tf.add(input2, input3)
mu1 = tf.multiply(input1, add)

with tf.Session() as sess:
    #一次操作两个op, 按顺序返回结果
    result = sess.run([mu1, add])
    print(result)

执行结果:

[21.0, 7.0]

Feed

Feed操作是指首先建立占位符, 然后把占位符放入op中.
在run op的时候, 再把要op的值传进去, 以达到使用时再传参数的目的

语法: 首先创建placeholder 然后再在run的时候把值以字典的方式传入run

#Feed
#创建占位符
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
#使用placeholder定义op
output = tf.multiply(input1, input2)

with tf.Session() as sess:
    #feed数据以字典的方式传入
    print(sess.run(output, feed_dict={input1: [7.], input2: [2.]}))

输出结果:

[ 14.]
上一篇 下一篇

猜你喜欢

热点阅读