tensorflow中的Fetch和Feed

其实吧 记住这两个名字没啥用,知道他们之间的区别就行了

首先说一说Fetch ,就是在使用变量之前就已经初始化了的(我是这么理解的)上图

import tensorflow as tf

a = tf.constant(2.0)
b = tf.constant(3.0)
c = tf.constant(5.0)

add = tf.add(a, c)
mul = tf.multiply(b, add)

with tf.Session() as sess:
	result = sess.run([mul, add])
	print(result)

# 下面是运行结果
[21.0, 7.0]

再来说一说Feed,就是在使用它的时候在给它传入数值, 上图

import tensorflow as tf

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
res = tf.multiply(a, b)

with tf.Session() as sess:
	print(sess.run(res, feed_dict={a: 7.0, b: 2.0}))
# 这里是要注意的,赋值的时候使用的是python自带的字典这种数据结构,冒号后面的数值,加不加中括号
# 只是对结果产生了结果那个值有没有中括号
# 下面是运行结果
14.0

# 下面是加上中括号的

import tensorflow as tf

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
res = tf.multiply(a, b)

with tf.Session() as sess:
	print(sess.run(res, feed_dict={a: [7.0], b: [2.0]}))

# 运行结果如下
[14.]

猜你喜欢

转载自blog.csdn.net/liyu_123_321/article/details/81186644