当前位置:网站首页>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() 操作将会产生错误。
边栏推荐
- L2-016 愿天下有情人都是失散多年的兄妹 (25 分)
- iNFTnews | 元宇宙为企业发展带来新思路
- 基于华为云弹性云服务器ECS(搭载openEuler的鲲鹏通用计算增强型)完成鲲鹏代码迁移工具实践【华为云至简致远】
- H. Huge Boxes of Animal Toys
- mysql进阶(二十九)常用函数汇总
- bzoj1097 [POI2007]旅游景点atr
- 敏捷开发项目管理的一些心得
- 3 个开源项目,让你感受程序员的浪漫!
- [In-depth study of 4G/5G/6G topic-54]: L3 signaling control-3-segmentation of software functions and processes-signaling of CU-UP network elements
- Camera calibration toobox for Matlab(一)—— 工具包的基本使用
猜你喜欢
随机推荐
APICloud AVM wraps date and time selection components
MySQL 数据库
IDEA2020安装教程
【LeetCode】试题总结:深度优先搜索 (DFS)
WPF StepBar
D. Non-zero Segments
浅学软件逆向笔记(2)
leetcode:294.翻转游戏
【数学模型】TOPSIS
mysql进阶(二十九)常用函数汇总
bzoj1269 [AHOI2006]文本编辑器editor
leetcode:296.最佳的碰头地点
基于华为云ModelArts的水表读数识别开发实践【华为云至简致远】
linux安装部署redis&配置远程连接
L2-023 图着色问题 (25 分)
英特尔两大 FPGA 产品已部署至中国创新中心:性能提高 45%,功耗降低 40%
【数学模型】灰色关联分析
JVM内存Dump原理与在线分析实战
Appium 自动化测试环境搭建
差分约束做法