python-tensorflow Session.run-参数理解
发布日期:2021-11-05 07:52:15
浏览次数:34
分类:技术文章
本文共 5500 字,大约阅读时间需要 18 分钟。
def run(self, fetches, feed_dict=None, options=None, run_metadata=None): """Runs operations and evaluates tensors in `fetches`. 功能:This method runs one "step" of TensorFlow computation, by running the necessary graph fragment(碎片) to execute every `Operation` and evaluate every `Tensor` in `fetches`, substituting(取代) the values in `feed_dict` for the corresponding input values. fetches数据类型:The `fetches` argument may be a single graph element, or an arbitrarily nested list, tuple, namedtuple, dict, or OrderedDict containing graph elements at its leaves. A graph element can be one of the following types:
什么时候需要使用fetches? 当有feed_dict需求时
fetches格式是什么,输出格式就是什么1、fetches是字典,值为z,tf会获取z的值,返回字典
import tensorflow as tfx_input = tf.placeholder(dtype=tf.float32,name='x',shape=[None,])y_input = tf.placeholder(dtype=tf.float32,name='y',shape=[None,])z = x_input+y_inputwith tf.Session() as sess: print(sess.run(fetches={'a':z},feed_dict={'x:0':[1],'y:0':[2]})) print(sess.run(fetches={'a':z},feed_dict={x_input:[1],y_input:[2]}))
{'a': array([3.], dtype=float32)} {'a': array([3.], dtype=float32)}2、fetches是列表,值为z,tf会获取z的值,返回列表
x_input = tf.placeholder(dtype=tf.float32,name='x',shape=[None,])y_input = tf.placeholder(dtype=tf.float32,name='y',shape=[None,])z = x_input+y_inputwith tf.Session() as sess: print(sess.run(fetches=[z,tf.constant([1,2,3])],feed_dict={'x:0':[1],'y:0':[2]})) print(sess.run(fetches=[tf.constant('afaf'),z],feed_dict={x_input:[1],y_input:[2]}))
[array([3.], dtype=float32), array([1, 2, 3])][b'afaf', array([3.], dtype=float32)] * An @{tf.Operation}. The corresponding fetched value will be `None`. * A @{tf.Tensor}. The corresponding fetched value will be a numpy ndarray containing the value of that tensor. * A @{tf.SparseTensor}. The corresponding fetched value will be a @{tf.SparseTensorValue} containing the value of that sparse tensor. * A `get_tensor_handle` op. The corresponding fetched value will be a numpy ndarray containing the handle of that tensor. * A `string` which is the name of a tensor or operation in the graph. The value returned by `run()` has the same shape as the `fetches` argument,where the leaves are replaced by the corresponding values returned by TensorFlow. Example: ```python a = tf.constant([10, 20]) b = tf.constant([1.0, 2.0]) # 'fetches' can be a singleton v = session.run(a) # v is the numpy array [10, 20] # 'fetches' can be a list. v = session.run([a, b]) # v is a Python list with 2 numpy arrays: the 1-D array [10, 20] and the # 1-D array [1.0, 2.0] # 'fetches' can be arbitrary lists, tuples, namedtuple, dicts: MyData = collections.namedtuple('MyData', ['a', 'b']) v = session.run({'k1': MyData(a, b), 'k2': [b, a]}) # v is a dict with # v['k1'] is a MyData namedtuple with 'a' (the numpy array [10, 20]) and # 'b' (the numpy array [1.0, 2.0]) # v['k2'] is a list with the numpy array [1.0, 2.0] and the numpy array # [10, 20]. ``` feed_dict数据类型:The optional `feed_dict` argument allows the caller to override the value of tensors in the graph. Each key in `feed_dict` can be one of the following types: * If the key is a @{tf.Tensor}, the value may be a Python scalar, string, list, or numpy ndarray that can be converted to the same `dtype` as that tensor. Additionally, if the key is a @{tf.placeholder}, the shape of the value will be checked for compatibility with the placeholder.1、tf.Tensor,字典键可以是变量名,也可以是变量属性
import tensorflow as tfx_input = tf.placeholder(dtype=tf.float32,name='x',shape=[None,])y_input = tf.placeholder(dtype=tf.float32,name='y',shape=[None,])z = x_input+y_inputwith tf.Session() as sess: print(sess.run(fetches={'a':z},feed_dict={'x:0':[1],'y:0':[2]})) print(sess.run(fetches={'a':z},feed_dict={x_input:[1],y_input:[2]}))
* If the key is a @{tf.SparseTensor},the value should be a @{tf.SparseTensorValue}. * If the key is a nested tuple of `Tensor`s or `SparseTensor`s, the value should be a nested tuple with the same structure that maps to their corresponding values as above. Each value in `feed_dict` must be convertible to a numpy array of the dtype of the corresponding key. The optional `options` argument expects a [`RunOptions`] proto. The options allow controlling the behavior of this particular step (e.g. turning tracing on). The optional `run_metadata` argument expects a [`RunMetadata`] proto. When appropriate, the non-Tensor output of this step will be collected there. For example, when users turn on tracing in `options`, the profiled info will be collected into this argument and passed back. Args: fetches: A single graph element, a list of graph elements, or a dictionary whose values are graph elements or lists of graph elements (described above). feed_dict: A dictionary that maps graph elements to values (described above). options: A [`RunOptions`] protocol buffer run_metadata: A [`RunMetadata`] protocol buffer Returns: Either a single value if `fetches` is a single graph element, or a list of values if `fetches` is a list, or a dictionary with the same keys as `fetches` if that is a dictionary (described above). Raises: RuntimeError: If this `Session` is in an invalid state (e.g. has been closed). TypeError: If `fetches` or `feed_dict` keys are of an inappropriate type. ValueError: If `fetches` or `feed_dict` keys are invalid or refer to a `Tensor` that doesn't exist. """
转载地址:https://blog.csdn.net/xingghaoyuxitong/article/details/114629557 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月11日 18时07分30秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
学习,一定是脱“贫”致富的捷径
2019-04-27
人应该活成什么样子?该以什么方式活着?
2019-04-27
块设备驱动初探
2019-04-27
MIPI白皮书
2019-04-27
去华为吗?
2019-04-27
Linux 块设备,Block Layer层架构演变
2019-04-27
我在MTK平台下调试音频ALSA
2019-04-27
被该死的Openssl编译嘲讽了一个下午
2019-04-27
暴力修改SElinux权限
2019-04-27
camera-open-log
2019-04-27
让这个该死的服务跑起来了~
2019-04-27
LinuxGPIO操作和MTK平台GPIO
2019-04-27
Linux cpu亲和力
2019-04-27
C语言指定初始化器解析及其应用
2019-04-27
安卓9.0添加服务修改SELinux
2019-04-27
深入理解Linux内核链表
2019-04-27
Linux一定需要文件系统吗?
2019-04-27
手机是怎么确定位置信息的?
2019-04-27
使用Adobe Audition生成基本音频
2019-04-27
思必驰算法库录音日志~
2019-04-27