Tensorflow中的run()函数

1 run()函数存在的意义

run()函数可以让代码变得更加简洁,在搭建神经网络(一)中,经历了数据集准备、前向传播过程设计、损失函数及反向传播过程设计等三个过程,形成计算网络,再通过会话tf.Session().run()进行循环优化网络参数。这样可以使得代码变得更加简洁,可以集中处理多个图和会话,明确调用tf.Session().run()可能是一种更加直观的方法。

总而言之,我们先规划好计算图,再编写代码,之后调用tf.Session.run()。简洁高效。

 

在实际代码中,一般写成下种形式(会话Session模式)

with tf.Session() as sess:
    sess.run( )

2 run() 语法

run(fetches, feed_dict=None, options=None, run_metadata=None)

tensorflow.python.client.session.Session实例中的方法对 ‘fetches’ 中的张量tensors进行评估和计算

该方法进行Tensorflow计算的第一个步骤,是将 ‘ feed_dict ’ 中的值替换为相应的输入值,通过运行必要的图形片段(necessary graph fragment)来执行每一个 ‘ Operation ’ 并评估 ‘ fetches ’ 中的每一个张量(evaluate every `Tensor` in `fetches`)

参数

`fetches`参数可以是单个图元素(single graph element),也可以是任意嵌套的列表list,元组tuple,名称元组namedtuple,字典dict或包含图元素的OrderedDict。

入列是列表,可以写成如下形式 

sess.run([train_step, loss_mse], feed_dict = ...)

 

图元素可以是以下类型之一:

* An @{tf.Operation}.
  The corresponding fetched value will be `None`.
# 相应的取值将会是None

* A @{tf.Tensor}.
  The corresponding fetched value will be a numpy ndarray containing the value of that tensor.
#  相应的获取值将是包含该张量值的numpy ndarray。

* A @{tf.SparseTensor}.
  The corresponding fetched value will be a
  @{tf.SparseTensorValue}
  containing the value of that sparse tensor.

# 相应的获取值将是包含该稀疏张量值的@{tf.SparseTensorValue}。
* A `get_tensor_handle` op.  The corresponding fetched value will be a
  numpy ndarray containing the handle of that tensor.
# `get_tensor_handle`操作。 相应的获取值将是包含该张量的句柄的numpy ndarray。

* A `string` which is the name of a tensor or operation in the graph.
# `string`,它是图中张量或操作的名称。

 

返回值:

函数返回值与 ‘ fetches ' 参数具有相同的形状,其中 the leaves 被Tensorflow返回的相应值替换。

原文: the leaves are replaced by the corresponding values returned by TensorFlow.

关于the leaves 具体表达的什么意思,自己还没有完全搞清楚,只是模模糊糊地有点意会。

feed_dict

可选参数 ’ feed_dict ’ 允许调用者覆盖图中张量的值( the value of tensors in the graph ), `feed_dict`中的每个键可以是以下类型之一:

  • @{tf.Tensor} - 则值value可能是Python标量scalar、字符串string、列表list、numpy ndarray,可以将其转化为‘dtype’相同的张量
  • @{tf.placeholder} - 则将检查值的形状是否与占位符兼容。
  • @{tf.SparseTensor} - 则该值应为{tf.SparseTensorValue}。
  • @{tf.SparseTensorValue}. - 则该值应为{tf.SparseTensorValue}。
  • nested tuple of `Tensor`s or `SparseTensor`s - 则该值应该是嵌套元组nested tuple,其结构与上面相应的值相同。

`feed_dict`中的每个值必须可以转换为相应键的dtype的numpy数组。

options

 可选的`options`参数需要一个[`RunOptions`]原型。 选项允许控制该特定步骤的行为(例如,打开跟踪)。

run_metadata

可选的`run_metadata`参数需要一个[`RunMetadata`]原型。 适当时,将在那里收集此步骤的非Tensor输出。 例如,当用户在`options`中打开跟踪时,配置信息将被收集到此参数中并传回。

args:
       fetches:单个图元素,图元素列表或其值为图元素或图元素列表的字典(如上所述)。
       feed_dict:将图元素映射到值的字典(如上所述)。
       options:一个[`RunOptions`]协议缓冲区
       run_metadata:一个[`RunMetadata`]协议缓冲区

备注

run的参数是字典形式,如果按照顺序传参,可以不写成字典形式

Tensorflow中的run()函数_第1张图片

 bottleneck_values = sess.run(bottleneck_tensor, {image_data_tensor: image_data})

参考链接:Mingxuan Yi的GitHub

3 会话Session相关内容

会话是否有生命周期?中间张量是否有生命周期?

会话可以拥有资源,比如 tf.Variable、tf.QueueBase 、 tf.ReaderBase ;这些资料可能会占用大量内存。

通过调用 tf.Session.close  关闭会话后,这些资源(及其关联的内存)将被释放;同时在调用 Session.run()  过程中创建的中间张量会在调用结束时或结束前释放。

此段内容见Tensorflow官网中常见问题解答 --> 运行 TensorFlow 计算

4 相关参考及其他


tensorflow学习笔记(十):sess.run()

tensorflow-tutorial/Deep_Learning_with_TensorFlow/1.0.0/Chapter06/2. 迁移学习.ipynb

Tensorflow中的Tensor和Variable之间有什么区别

在TensorFlow中,Session.run()和Tensor.eval()有什么区别?(中文)

In TensorFlow, what is the difference between Session.run() and Tensor.eval()?(英文)

 

你可能感兴趣的:(Tensorflow中的run()函数)