TensorFlow1.x入门(3)——Feed与Fetch

系列文章

本教程有同步的github地址

0. 统领篇

1. 计算图的创建与启动

2. 变量的定义及其操作

3. Feed与Fetch

4. 线性回归

5. 构建非线性回归模型

6. 简单分类问题

7. Dropout与优化器

8. 手动调整学习率与TensorBoard

9. 卷积神经网络(CNN)

10. 循环神经网络(RNN)

11. 模型的保存与恢复

知识点

Feed:中文翻译“喂食”。在TensorFlow中用于表示,将数据喂到(输入到)计算图中进行计算的操作。
Fetch:中文翻译“拿得”。在TensorFlow中用于表示,从计算图中取数字的操作。
tf.multiply(x, y)是乘法操作,返回xy的乘积。即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

猜你喜欢

转载自blog.csdn.net/qq_19672707/article/details/105235728
今日推荐