当前位置:网站首页>Tensorflow教程(三)——获取数据 feed 和 fetchn
Tensorflow教程(三)——获取数据 feed 和 fetchn
2022-08-08 16:54:00 【泉伟】
名称 | Fetch | Feed |
---|---|---|
特点 | 直接确认数值,直接进行调用 | 需要使用tf.placeholder() 占位符,数值需要后期输入 |
使用方法 | print( sess.run([mul, intermed])) | print(sess.run([output], feed_dict={input1:[7.], input2:[2.]})) |
Fetch
为了取回操作的输出内容, 可以在使用 Session 对象的 run() 调用 执行图时, 传入一些 tensor, 这些 tensor 会帮助你取回结果。在之前的例子里, 我们只取回了单个节点 state, 但是你也可以取回多个 tensor:
import tensorflow as tf
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)
# 输出:
# [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]
需要获取的多个 tensor 值,在 op 的一次运行中一起获得(而不是逐个去获取 tensor)。在上边的例子中我们就希望获取中间变量mul, intermed
的数值,这个方便我们操作进行。
Feed
上述示例在计算图中引入了 tensor, 以常量或变量的形式存储。 TensorFlow 还提供了 feed 机制, 该机制 可以临时替代图中的任意操作中的 tensor 可以对图中任何操作提交补丁, 直接插入一个 tensor。
feed 使用一个 tensor 值临时替换一个操作的输出结果,你可以提供 feed 数据作为 run() 调用的参数, feed 只在调用它的方法内有效, 方法结束, feed 就会消失,最常见的用例是将某些特殊的操作指定为 “feed” 操作, 标记的方法是使用 tf.placeholder() 为这些操作创建占位符。
import tensorflow as tf
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)]
for a larger-scale example of feeds. 如果没有正确提供 feed, placeholder() 操作将会产生错误。
边栏推荐
猜你喜欢
随机推荐
信号生成和可视化
Using PyGame's Bubble Sort Visualizer
题目:有序队列
mysql 索引和 pgsql 索引 命名区别
QCon 回顾 | Data Fabric:逻辑统一、物理分散
3531. 哈夫曼树
[机缘参悟-64]:《兵者,诡道也》-5-孙子兵法解读-混战计
文件操作和IO
The difference between B+ tree and B- tree
爬百度图片
LeetCode_二叉树_中等_515.在每个树行中找最大值
4. S32K14X study notes: S32 Design Studio new and imported projects
L2-015 互评成绩 (25 分)
APICloud AVM wraps date and time selection components
L2-009 抢红包 (25 分)(结构体+自定义排序)
D. Non-zero Segments
3dsmax2021软件安装教程
APICloud AVM 封装日期和时间选择组件
MySQL 数据库
LeetCode_Backtrack_Medium_491. Incrementing Subsequence