> For the complete documentation index, see [llms.txt](https://minghuiwu.gitbook.io/tfbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://minghuiwu.gitbook.io/tfbook/di-san-zhang-mo-dao-bu-wu-kan-chai-gong-tensorflow-ji-chu/di-san-zhang-tensorflow-ji-chu/3.10feed-ti-jiao-shu-ju-he-fetch-ti-qu-shu-ju.md).

# 3.10    Feed提交数据和Fetch提取数据

**3.10.1 Feed提交数据**

在TensorFlow中如果构建了一个包含placeholder操作的计算图，在程序执行当在session中调用run方法时，placeholder占用的变量必须通过**feed\_dict**参数传递进去，否则报错。图12提供了一个Feed的样例。

![图3-12 Feed提交数据](/files/-L_XerLt4UEoi1KEsbLJ)

**注**：多个操作可以通过一次Feed完成执行

**3.10.2 Fetch提取数据**

会话运行完成之后，如果我们想查看会话运行的结果，就需要使用fetch来实现，feed、fetch一般搭配起来使用，图13提供了一个样例。

![图3-13 Fetch提取数据](/files/-L_Xf4TGcRvlq6FytmuA)

## > 示例代码 <a href="#shi-li-dai-ma" id="shi-li-dai-ma"></a>

{% code title="1.py" %}

```python
import tensorflow as tf

a = tf.placeholder(tf.float32, name='a')
b = tf.placeholder(tf.float32, name='b')
c = tf.multiply(a, b, name='c')

init = tf.global_variables_initializer()

with tf.Session() as sess:
#    sess.run(init)
    # 通过feed_dict的参数传值，按字典格式
    result = sess.run(c, feed_dict={a:10.0, b:3.5})
    
print(result)
```

{% endcode %}

{% code title="2.py" %}

```python
import tensorflow as tf
a = tf.placeholder(tf.float32, name='a')
b = tf.placeholder(tf.float32, name='b')
c = tf.multiply(a, b, name='c')
d = tf.subtract(a, b, name='d')
with tf.Session() as sess:
    
    #返回的两个值分别赋给两个变量
    rc,rd = sess.run([c,d], feed_dict={a:[8.0,2.0,3.5], b:[1.5,2.0,4.]})
    
    print("value of c=",rc,"value of d=",rd)
```

{% endcode %}
