TensorFlow介绍与安装

0.77字数 7096阅读 6441

1. 介绍

``````import tensorflow as tf
import numpy as np

# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3

# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b

# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
train = optimizer.minimize(loss)

# Before starting, initialize the variables.  We will 'run' this first.
init = tf.global_variables_initializer()

# Launch the graph.
sess = tf.Session()
sess.run(init)

# Fit the line.
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(W), sess.run(b))

# Learns best fit is W: [0.1], b: [0.3]

# Close the Session when we're done.
sess.close()
``````

2. 下载与安装

要求

TensorFlow支持Python 2.7和Python 3.3+。

GPU版本最好使用Cuda Toolkit 8.0和cuDNN v5。只有从源代码安装时，才支持其他版本（Cuda工具包> = 7.0和cuDNN> = v3）。有关详细信息，请参阅Cuda安装。对于Mac OS X，请参阅安装GPU for Mac

基于Pip安装

Pip是用于安装和管理用Python编写的软件包的软件包管理系统。我们在Linux，Mac OS X和Windows上为TensorFlow提供pip软件包。有关Windows的说明，请参阅Windows上的Pip安装

``````# Ubuntu/Linux 64-bit
\$ sudo apt-get install python-pip python-dev

# Mac OS X
\$ sudo easy_install pip
``````

``````\$ pip install tensorflow
``````

``````# Ubuntu/Linux 64-bit, CPU only, Python 2.7

# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.

# Mac OS X, CPU only, Python 2.7:

# Mac OS X, GPU enabled, Python 2.7:

# Ubuntu/Linux 64-bit, CPU only, Python 3.4

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.

# Ubuntu/Linux 64-bit, CPU only, Python 3.5

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.5
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.

# Mac OS X, CPU only, Python 3.4 or 3.5:

# Mac OS X, GPU enabled, Python 3.4 or 3.5:
``````

``````# Python 2
\$ sudo pip install --upgrade \$TF_BINARY_URL

# Python 3
\$ sudo pip3 install --upgrade \$TF_BINARY_URL
``````

在Windows上Pip安装

TensorFlow在Windows上仅支持64位Python 3.5。我们已经使用以下Python版本测试了pip软件包：

基于Virtualenv安装

Virtualenv是为不同Python项目创建一个隔离的容器的工具。利用Virtualenv安装TensorFlow不会影响原先存在的的Python环境。

• 安装pip和Virtualenv。
• 创建一个Virtualenv环境。
• 激活Virtualenv环境并在其中安装TensorFlow。
• 安装完成后，您将在每次要使用TensorFlow时激活Virtualenv环境。

``````# Ubuntu/Linux 64-bit
\$ sudo apt-get install python-pip python-dev python-virtualenv

# Mac OS X
\$ sudo easy_install pip
\$ sudo pip install --upgrade virtualenv
``````

``````\$ virtualenv --system-site-packages ~/tensorflow
``````

``````\$ source ~/tensorflow/bin/activate  # If using bash
\$ source ~/tensorflow/bin/activate.csh  # If using csh
(tensorflow)\$  # Your prompt should change
``````

``````# Ubuntu/Linux 64-bit, CPU only, Python 2.7

# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.

# Mac OS X, CPU only, Python 2.7:

# Mac OS X, GPU enabled, Python 2.7:

# Ubuntu/Linux 64-bit, CPU only, Python 3.4

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.

# Ubuntu/Linux 64-bit, CPU only, Python 3.5

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.5
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.

# Mac OS X, CPU only, Python 3.4 or 3.5:

# Mac OS X, GPU enabled, Python 3.4 or 3.5:
``````

``````# Python 2

# Python 3
``````

``````(tensorflow)\$ deactivate

\$  # Your prompt should change back
``````

``````\$ source ~/tensorflow/bin/activate  # If using bash.
\$ source ~/tensorflow/bin/activate.csh  # If using csh.
(tensorflow)\$  # Your prompt should change.
# Run Python programs that use TensorFlow.
...
# When you are done using TensorFlow, deactivate the environment.
(tensorflow)\$ deactivate
``````

基于Anaconda安装

Anaconda是一个集成许多第三方科学计算库的 Python 科学计算环境。Anaconda使用一个名为“conda”的包管理器，它具有与Virtualenv相似的环境系统

• 安装Anaconda。
• 创建一个conda环境。
• 激活conda环境并在其中安装TensorFlow。
• 安装完成后，每次要使用TensorFlow时，都需要激活环境。
• 可选地将ipython和其他软件包安装到conda环境中。

Python 2

``````\$ pip uninstall tensorflow
``````

Python 3

``````\$ pip3 uninstall tensorflow
``````

``````# Python 2.7
\$ conda create -n tensorflow python=2.7

# Python 3.4
\$ conda create -n tensorflow python=3.4

# Python 3.5
\$ conda create -n tensorflow python=3.5
``````

使用conda

``````\$ source activate tensorflow
(tensorflow)\$  # Your prompt should change

# Linux/Mac OS X, Python 2.7/3.4/3.5, CPU only:
(tensorflow)\$ conda install -c conda-forge tensorflow
``````

使用pip

``````\$ source activate tensorflow
(tensorflow)\$  # Your prompt should change
``````

``````# Ubuntu/Linux 64-bit, CPU only, Python 2.7

# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.

# Mac OS X, CPU only, Python 2.7:

# Mac OS X, GPU enabled, Python 2.7:

# Ubuntu/Linux 64-bit, CPU only, Python 3.4

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.

# Ubuntu/Linux 64-bit, CPU only, Python 3.5

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.5
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.

# Mac OS X, CPU only, Python 3.4 or 3.5:

# Mac OS X, GPU enabled, Python 3.4 or 3.5:
``````

``````# Python 2
(tensorflow)\$ pip install --ignore-installed --upgrade \$TF_BINARY_URL

# Python 3
(tensorflow)\$ pip3 install --ignore-installed --upgrade \$TF_BINARY_URL
``````

使用方法

``````(tensorflow)\$ source deactivate

\$  # Your prompt should change back
``````

``````\$ source activate tensorflow
(tensorflow)\$  # Your prompt should change.
# Run Python programs that use TensorFlow.
...
# When you are done using TensorFlow, deactivate the environment.
(tensorflow)\$ source deactivate
``````

安装IPython

``````\$ source activate tensorflow
(tensorflow)\$ conda install ipython
``````

Docker安装

Docker是一种在Linux操作系统上运行的自建版本的虚拟机。当您通过Docker安装和运行TensorFlow时，将与机器上预先安装存在的包完全隔离。

• gcr.io/tensorflow/tensorflow：TensorFlow CPU二进制镜像。
• gcr.io/tensorflow/tensorflow:latest-develCPU二进制镜像加源码。
• gcr.io/tensorflow/tensorflow:latest-gpu：TensorFlow GPU二进制镜像。
• gcr.io/tensorflow/tensorflow:latest-devel-gpu：GPU二进制镜像加源代码。

Docker的安装如下：

• 在您的机器上安装Docker。
• 创建一个Docker group以允许启动容器。
• 启动包含TensorFlow映像的Docker容器。首次启动时，镜像会自动下载。

Docker安装完成后，启动包含TensorFlow映像的Docker容器。

``````\$ docker run -it -p 8888:8888 gcr.io/tensorflow/tensorflow
``````

NVidia GPU支持安装最新的NVidia驱动程序和nvidia-docker

``````\$ nvidia-docker run -it -p 8888:8888 gcr.io/tensorflow/tensorflow:latest-gpu
``````

``````\$ path/to/repo/tensorflow/tools/docker/docker_run_gpu.sh -p 8888:8888 gcr.io/tensorflow/tensorflow:latest-gpu
``````

测试TensorFlow的安装

``````export LD_LIBRARY_PATH="\$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda
``````

从命令行运行TensorFlow

``````\$ python
...
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
>>>
``````

从源代码安装

Windows上从源代码安装TensorFlow，您可以在Windows上使用BazelTensorFlow CMake build的支持。

克隆TensorFlow仓库

``````\$ git clone https://github.com/tensorflow/tensorflow
``````

Linux环境准备

安装Bazel

``````\$ chmod +x PATH_TO_INSTALL.SH
\$ ./PATH_TO_INSTALL.SH --user
``````

安装其他依赖

``````# For Python 2.7:
\$ sudo apt-get install python-numpy python-dev python-wheel python-mock
# For Python 3.x:
\$ sudo apt-get install python3-numpy python3-dev python3-wheel python3-mock
``````

可选：安装CUDA（在Linux上启用GPU支持）

TensorFlow GPU支持需要具有NVIDIA计算能力（> = 3.0）的GPU卡。支持的显卡包括：

• NVidia Titan

• NVidia Titan X

• NVidia K20

• NVidia K40

...

``````tar xvzf cudnn-8.0-linux-x64-v5.1-ga.tgz
sudo cp -P cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
``````

可选：安装OpenCL（仅限实验和Linux系统）

TensorFlow只能利用支持OpenCL 1.2的加速器。支持的加速器包括：

• AMD Fiji

• AMD Hawaii

...

下载并安装OpenCL驱动程序

``````sudo apt-get install ocl-icd-opencl-dev opencl-headers
``````

``````sudo apt-get install fglrx-core fglrx-dev
``````
下载并安装ComputeCpp编译器

Codeplay的网站下载编译器，解压缩并将文件复制到如下的路径/usr/local/computecpp：

``````tar -xvzf ComputeCpp-CE-0.1.1-Ubuntu.14.04-64bit.tar.gz
sudo mkdir /usr/local/computecpp
sudo cp -R ComputeCpp-CE-0.1.1-Linux /usr/local/computecpp
sudo chmod -R a+r /usr/local/computecpp/
sudo chmod -R a+x /usr/local/computecpp/bin
``````

Mac环境准备

依赖

``````\$ brew install bazel
``````

``````\$ sudo easy_install -U six
\$ sudo easy_install -U numpy
\$ sudo easy_install wheel
``````

``````\$ sudo easy_install ipython
``````

可选：为Mac设置GPU支持

``````\$ brew install coreutils
``````

``````\$ brew tap caskroom/cask
``````

``````export CUDA_HOME=/usr/local/cuda
export DYLD_LIBRARY_PATH="\$DYLD_LIBRARY_PATH:\$CUDA_HOME/lib"
export PATH="\$CUDA_HOME/bin:\$PATH"
``````

``````\$ sudo mv include/cudnn.h /Developer/NVIDIA/CUDA-8.0/include/
\$ sudo mv lib/libcudnn* /Developer/NVIDIA/CUDA-8.0/lib
\$ sudo ln -s /Developer/NVIDIA/CUDA-8.0/lib/libcudnn* /usr/local/cuda/lib/
``````

``````\$ cp -r /usr/local/cuda/samples ~/cuda-samples
\$ pushd ~/cuda-samples
\$ make
\$ popd
\$ ~/cuda-samples/bin/x86_64/darwin/release/deviceQuery
``````

``````\$ sudo xcode-select -s /Application/Xcode-7.2/Xcode.app
``````

配置安装

``````\$ ./configure
Please specify the location of python. [Default is /usr/bin/python]:
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] N
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with GPU support? [y/N] y
Do you wish to build TensorFlow with OpenCL support? [y/N] N
GPU support will be enabled for TensorFlow
Please specify which gcc nvcc should use as the host compiler. [Default is /usr/bin/gcc]:
Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: 8.0
Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify the cuDNN version you want to use. [Leave empty to use system default]: 5
Please specify the location where cuDNN 5 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.

Setting up Cuda include
Setting up Cuda lib
Setting up Cuda bin
Setting up Cuda nvvm
Setting up CUPTI include
Setting up CUPTI lib64
Configuration finished
``````

已知的问题

• 尽管可以在同一个源码树下编译开启 Cuda 支持和禁用 Cuda 支持的版本, 我们还是推荐在 在切换这两种不同的编译配置时, 使用 "bazel clean" 清理环境.。

• 在执行 bazel 编译前必须先运行 configure, 否则编译会失败并提示错误信息. 未来, 我们可能考虑将 configure 步骤包含在编译过程中, 以简化整个过程, 前提是 bazel 能够提供新的特性支持这样。

创建pip包并安装

``````\$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

# To build with GPU support:
\$ bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

\$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

# The name of the .whl file will depend on your platform.
\$ sudo pip install /tmp/tensorflow_pkg/tensorflow-0.12.0rc1-py2-none-any.whl
``````

优化CPU性能

``````\$ bazel build --copt=-march=native -c opt //tensorflow/tools/pip_package:build_pip_package
``````

设置TensorFlow的发展

``````bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

# To build with GPU support:
bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

mkdir _python_build
cd _python_build
ln -s ../bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/org_tensorflow/* .
ln -s ../tensorflow/tools/pip_package/* .
python setup.py develop
``````

训练您的第一个TensorFlow神经网络模型

``````\$ cd models/tutorials/image/mnist
\$ python convolutional.py
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
Initialized!
Epoch 0.00
Minibatch loss: 12.054, learning rate: 0.010000
Minibatch error: 90.6%
Validation error: 84.6%
Epoch 0.12
Minibatch loss: 3.285, learning rate: 0.010000
Minibatch error: 6.2%
Validation error: 7.0%
...
...
``````

常见问题

GPU 相关问题

``````ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory
``````

Protobuf库相关问题

TensorFlow的pip包依赖于protobuf 3.1.0版本的pip软件包。可以从PyPI（运行时pip install protobuf）下载的Protobuf的pip软件包，Python能利用它实现原型序列化/反序列化的，它比C ++实现慢10倍到50倍。Protobuf还支持包含基于快速C ++的原语解析的Python包的二进制扩展。此扩展在只有Python标准的PIP包中不可用。我们为protobuf创建了一个包含二进制扩展名的自定义二进制pip包。按照以下说明安装自定义二进制protobuf pip软件包：

``````# Ubuntu/Linux 64-bit:

# Mac OS X:
``````

``````# Ubuntu/Linux 64-bit:

# Mac OS X:
``````

``````\$ git clone https://github.com/google/protobuf.git
\$ cd protobuf
\$ ./autogen.sh
\$ CXXFLAGS="-fPIC -g -O2" ./configure
\$ make -j12
\$ export PROTOC=\$PWD/src/protoc
\$ cd python
\$ python setup.py bdist_wheel --cpp_implementation --compile_static_extension
\$ pip uninstall protobuf
\$ pip install dist/<wheel file name>
``````

``````[libprotobuf ERROR google/protobuf/src/google/protobuf/io/coded_stream.cc:207] A
protocol message was rejected because it was too big (more than 67108864 bytes).
To increase the limit (or to disable these warnings), see
``````

Pip安装问题

Cannot import name 'descriptor'

``````ImportError: Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/tensorflow/core/framework/graph_pb2.py", line 6, in <module>
from google.protobuf import descriptor as _descriptor
ImportError: cannot import name 'descriptor'
``````

找不到setup.py

``````...
IOError: [Errno 2] No such file or directory: '/tmp/pip-o6Tpui-build/setup.py'
``````

``````pip install --upgrade pip
``````

SSLError：SSL_VERIFY_FAILED

``````...
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
``````

Operation not permitted

``````...
Installing collected packages: setuptools, protobuf, wheel, numpy, tensorflow
Found existing installation: setuptools 1.1.6
Uninstalling setuptools-1.1.6:
Exception:
...
[Errno 1] Operation not permitted: '/tmp/pip-a1DXRT-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib'
``````

Cannot remove entries from nonexistent file: easy-install.pth

``````Cannot remove entries from nonexistent file <path-to-anaconda-instalation>/anaconda[version]/lib/site-packages/easy-install.pth
``````
1. 升级安装工具：`pip install --upgrade -I setuptools`

2. 安装TensorFlow再添加--ignore-installed选项：`pip install --ignore-installed --upgrade <tensorflow_url>`

在 Linux 上

``````...
^
SyntaxError: invalid syntax
``````

在 Mac OS X 上

``````import six.moves.copyreg as copyreg
ImportError: No module named copyreg
``````

• 升级全系统的 six:
``````\$ sudo easy_install -U six
``````
• 通过隔离环境安装TensorFlow:

• 通过Homebrew或 MacPorts安装隔离的Python环境，并在该版本的Python中重新安装TensorFlow。

3. 基本用法

• 使用图(graph)来表示计算任务

• 在被称之为会话(Session)的上下文(context)中执行图

• 使用 tensor 表示数据

• 通过变量(Variable)维护状态

• 使用feed和fetch可以为任意的操作(arbitrary operation)赋值或者从其中获取数据

概述

TensorFlow是一个编程系统，使用图(graph)来表示计算任务。图中的节点称为op（operation）。一个op取零个或多个Tensors来执行计算，并产生零个或多个Tensors。在TensorFlow术语中，一个Tensor是一个类型化的多维数组。例如，您可以将一小组的图像表示为一个4维的浮点数数组，这4个维度分别是[batch, height, width, channels]。

计算图(graph)

TensorFlow 程序通常被组织成一个构建阶段和一个执行阶段。 在构建阶段，op的执行步骤被描述成一个图(graph)，在执行阶段, 使用会话执行执行图中的op。

TensorFlow支持C，C++，Python编程语言。目前，TensorFlow的Python 库更加易用，它提供了大量的辅助函数来简化构建图的工作，但这些函数尚未被C和C++库支持。

构建图(graph)

Python库中的op构造函数返回表示构造的ops的输出的对象。您可以将这些作为输入传递给其他op构造函数。

Python的TensorFlow库有一个默认图(default graph)，op构造器可以为其增加节点。这个默认图对许多程序来说已经足够用了。阅读Graph类文档来了解如何管理多个图。

``````import tensorflow as tf

# Create a constant op that produces a 1x2 matrix.  The op is
# added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the constant op.
matrix1 = tf.constant([[3., 3.]])

# Create another constant that produces a 2x1 matrix.
matrix2 = tf.constant([[2.],[2.]])

# Create a matmul op that takes 'matrix1' and 'matrix2' as inputs.
# The returned value, 'product', represents the result of the matrix
# multiplication.
product = tf.matmul(matrix1, matrix2)
``````

在一个会话(Session)中启动图(graph)

``````# Launch the default graph.
sess = tf.Session()

# To run the matmul op we call the session 'run()' method, passing 'product'
# which represents the output of the matmul op.  This indicates to the call
# that we want to get the output of the matmul op back.
#
# All inputs needed by the op are run automatically by the session.  They
# typically are run in parallel.
#
# The call 'run(product)' thus causes the execution of three ops in the
# graph: the two constants and matmul.
#
# The output of the matmul is returned in 'result' as a numpy `ndarray` object.
result = sess.run(product)
print(result)
# ==> [[ 12.]]

# Close the Session when we're done.
sess.close()
``````

Session对象在使用完后需要关闭以释放资源。除了显式调用close外，也可以使用with语句来自动完成关闭动作。

``````with tf.Session() as sess:
result = sess.run([product])
print(result)
``````

``````with tf.Session() as sess:
with tf.device("/gpu:1"):
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
...
``````

• "/cpu:0"：机器的CPU
• "/gpu:0"：机器的第一个 GPU，如果有的话
• "/gpu:1"：机器的第二个 GPU，以此类推

在分布式会话(Session)中启动图(graph)

``````with tf.Session("grpc://example.org:2222") as sess:
# Calls to sess.run(...) will be executed on the cluster.
...
``````

``````with tf.device("/job:ps/task:0"):
weights = tf.Variable(...)
biases = tf.Variable(...)
``````

交互式使用

``````# Enter an interactive TensorFlow Session.
import tensorflow as tf
sess = tf.InteractiveSession()

x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])

# Initialize 'x' using the run() method of its initializer op.
x.initializer.run()

# Add an op to subtract 'a' from 'x'.  Run it and print the result
sub = tf.sub(x, a)
print(sub.eval())
# ==> [-2. -1.]

# Close the Session when we're done.
sess.close()
``````

Tensor

TensorFlow程序使用tensor数据结构来代表所有的数据，计算图(graph)的时候，操作间传递的数据都是tensor。你可以把TensorFlow的tensor 看作是一个n维的数组或列表。一个tensor包含一个静态类型rank和一个shape。想了解TensorFlow关于这些概念的信息，参见Rank，Shape和Type

变量(Variables)

``````# Create a Variable, that will be initialized to the scalar value 0.
state = tf.Variable(0, name="counter")

# Create an Op to add one to `state`.

one = tf.constant(1)
update = tf.assign(state, new_value)

# Variables must be initialized by running an `init` Op after having
# launched the graph.  We first have to add the `init` Op to the graph.
init_op = tf.global_variables_initializer()

# Launch the graph and run the ops.
with tf.Session() as sess:
# Run the 'init' op
sess.run(init_op)
# Print the initial value of 'state'
print(sess.run(state))
# Run the op that updates 'state' and print 'state'.
for _ in range(3):
sess.run(update)
print(sess.run(state))

# output:

# 0
# 1
# 2
# 3
``````

Fetch

``````input1 = tf.constant([3.0])
input2 = tf.constant([2.0])
input3 = tf.constant([5.0])
mul = tf.mul(input1, intermed)

with tf.Session() as sess:
result = sess.run([mul, intermed])
print(result)

# output:
# [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]
``````

Feed

feed使用一个tensor值临时替换一个操作的输出结果。你可以提供feed数据作为run()调用的参数。feed只在调用它的方法内有效。方法结束，feed就会消失。最常见的用例是将某些特殊的操作指定为feed操作，标记方法是使用tf.placeholder()为这些操作创建占位符。

``````input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)

with tf.Session() as sess:
print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))

# output:
# [array([ 14.], dtype=float32)]
``````