系列文章
本教程有同步的github地址
知识点
Feed:中文翻译“喂食”。在TensorFlow中用于表示,将数据喂到(输入到)计算图中进行计算的操作。
Fetch:中文翻译“拿得”。在TensorFlow中用于表示,从计算图中取数字的操作。
tf.multiply(x, y)
是乘法操作,返回x
与y
的乘积。即x*y
。该乘法为数值之间相乘,并不是矩阵相乘。
tf.placeholder()
表示占位符,可以理解为计算图中喂数据的入口,利用Feed操作将数据输入到占位符,从而进行数据的训练。
示例
#%% md
# Feed与Fetch
#%%
import tensorflow as tf
#%% md
## Fetch
#%% md
Fetch是获取计算图中的数据的操作
#%%
input1 = tf.constant(1.0)
input2 = tf.constant(2.0)
input3 = tf.constant(3.0)
#%%
input1,input2,input3
#%% md
创建加法和乘法操作
#%%
add = tf.add(input1, input3)
#%%
add
#%%
mul = tf.multiply(input2, add)
#%%
mul
#%% md
创建会话Session
#%%
with tf.Session() as sess:
multiply_res = sess.run(mul)
add_res = sess.run(add)
#%%
multiply_res, add_res
#%% md
## Feed
#%% md
Feed是赋值操作,是将数据赋值到占位符操作
#%% md
创建两个占位符placeholder
#%%
num1 = tf.placeholder(tf.float32)
num2 = tf.placeholder(tf.float32)
#%%
num1
#%% md
创建乘法操作
#%%
output = tf.multiply(num1, num2)
#%%
output
#%% md
创建会话
#%%
with tf.Session() as sess:
res = sess.run(output, feed_dict={num1: 0.2, num2: 8.0})
#%%
res
#%%
with tf.Session() as sess:
res = sess.run(output, feed_dict={num1: [0.2], num2: [8.0]})
#%%
res