TensorFlow学习笔记1:入门

TensorFlow 简介

TensorFlow是Google在2015年11月份开源的人工智能系统(Github项目地址),是以前所开发的深度学习基础架构DistBelief的改进版本,该系统能够被用于语音识别、图片识别等多个领域。html

官网上对TensorFlow的介绍是,一个使用数据流图(data flow graphs)技术来进行数值计算的开源软件库。数据流图中的节点,表明数值运算;节点节点之间的边,表明多维数据(tensors)之间的某种联系。你能够在多种设备(含有CPU或GPU)上经过简单的API调用来使用该系统的功能。TensorFlow是由Google Brain团队的研发人员负责的项目。node

什么是数据流图(Data Flow Graph)

数据流图是描述有向图中的数值计算过程。有向图中的节点一般表明数学运算,但也能够表示数据的输入、输出和读写等操做;有向图中的边表示节点之间的某种联系,它负责传输多维数据(Tensors)。图中这些tensorsflow也就是TensorFlow的命名来源。python

节点能够被分配到多个计算设备上,能够异步和并行地执行操做。由于是有向图,因此只有等到以前的入度节点们的计算状态完成后,当前节点才能执行操做。git

TensorFlow的特性

1 灵活性github

TensorFlow不是一个严格的神经网络工具包,只要你可使用数据流图来描述你的计算过程,你可使用TensorFlow作任何事情。你还能够方便地根据须要来构建数据流图,用简单的Python语言来实现高层次的功能。编程

2 可移植性api

TensorFlow能够在任意具有CPU或者GPU的设备上运行,你能够专一于实现你的想法,而不用去考虑硬件环境问题,你甚至能够利用Docker技术来实现相关的云服务。数组

3 提升开发效率网络

TensorFlow能够提高你所研究的东西产品化的效率,而且能够方便与同行们共享代码。session

4 支持语言选项

目前TensorFlow支持Python和C++语言。(可是你能够本身编写喜好语言的SWIG接口)

5 充分利用硬件资源,最大化计算性能

基本使用

你须要理解在TensorFlow中,是如何:

  • 将计算流程表示成图;
  • 经过Sessions来执行图计算;
  • 将数据表示为tensors
  • 使用Variables来保持状态信息;
  • 分别使用feedsfetches来填充数据和抓取任意的操做结果;

概览

TensorFlow是一种将计算表示为图的编程系统。图中的节点称为ops(operation的简称)。一个ops使用0个或以上的Tensors,经过执行某些运算,产生0个或以上的Tensors一个Tensor是一个多维数组,例如,你能够将一批图像表示为一个四维的数组[batch, height, width, channels],数组中的值均为浮点数。

TensorFlow中的图描述了计算过程,图经过Session的运行而执行计算。Session将图的节点们(即ops)放置到计算设备(如CPUs和GPUs)上,而后经过方法执行它们;这些方法执行完成后,将返回tensors。在Python中的tensor的形式是numpy ndarray对象,而在C/C++中则是tensorflow::Tensor.

图计算

TensorFlow程序中图的建立相似于一个 [施工阶段],而在 [执行阶段] 则利用一个session来执行图中的节点。很常见的状况是,在 [施工阶段] 建立一个图来表示和训练神经网络,而在 [执行阶段] 在图中重复执行一系列的训练操做。

建立图

在TensorFlow中,Constant是一种没有输入的ops,可是你能够将它做为其余ops的输入。Python库中的ops构造器将返回构造器的输出。TensorFlow的Python库中有一个默认的图,将ops构造器做为节点,更多可了解Graph Class文档

见下面的示例代码:

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)

默认的图(Default Graph)如今有了三个节点:两个 Constant()ops和一个matmul()op。为了获得这两个矩阵的乘积结果,还须要在一个session中启动图计算。

在Session中执行图计算

见下面的示例代码,更多可了解Session Class

# 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 threes ops in the
# graph: the two constants and matmul.
#
# The output of the op 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()

Sessions最后须要关闭,以释放相关的资源;你也可使用with模块,session在with模块中自动会关闭:

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

TensorFlow的这些节点最终将在计算设备(CPUs,GPus)上执行运算。若是是使用GPU,默认会在第一块GPU上执行,若是你想在第二块多余的GPU上执行:

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)
    ...

device中的各个字符串含义以下:

  • "/cpu:0": 你机器的CPU;
  • "/gpu:0": 你机器的第一个GPU;
  • "/gpu:1": 你机器的第二个GPU;

关于TensorFlow中GPU的使用见这里

交互环境下的使用

以上的python示例中,使用了SessionSession.run()来执行图计算。然而,在一些Python的交互环境下(如IPython中),你可使用InteractiveSession类,以及Tensor.eval()Operation.run()等方法。例如,在交互的Python环境下执行如下代码:

# 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()

Tensors

TensorFlow中使用tensor数据结构(实际上就是一个多维数据)表示全部的数据,并在图计算中的节点之间传递数据。一个tensor具备固定的类型、级别和大小,更加深刻理解这些概念可参考Rank, Shape, and 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)
new_value = tf.add(state, one)
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.initialize_all_variables()

# 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

赋值函数assign()add()函数相似,直到session的run()以后才会执行操做。与之相似的,通常咱们会将神经网络模型中的参数表示为一系列的变量,在模型的训练过程当中对变量进行更新操做。

抓取(Fetches)

为了抓取ops的输出,须要先执行sessionrun函数。而后,经过print函数打印状态信息。

input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermed = tf.add(input2, input3)
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)]

全部tensors的输出都是一次性 [连贯] 执行的。

填充(Feeds)

TensorFlow也提供这样的机制:先建立特定数据类型的占位符(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)]

若是不对placeholder()的变量进行数据填充,将会引起错误,更多的例子可参考MNIST fully-connected feed tutorial (source code)

示例:曲线拟合

下面是一段使用Python写的,曲线拟合计算。官网将此做为刚开始介绍的示例程序。

# 简化调用库名
import tensorflow as tf
import numpy as np

# 模拟生成100对数据对, 对应的函数为y = x * 0.1 + 0.3
x_data = np.random.rand(100).astype("float32")
y_data = x_data * 0.1 + 0.3

# 指定w和b变量的取值范围(注意咱们要利用TensorFlow来获得w和b的值)
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b

# 最小化均方偏差
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# 初始化TensorFlow参数
init = tf.initialize_all_variables()

# 运行数据流图(注意在这一步才开始执行计算过程)
sess = tf.Session()
sess.run(init)

# 观察屡次迭代计算时,w和b的拟合值
for step in xrange(201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(W), sess.run(b))

# 最好的状况是w和b分别接近甚至等于0.1和0.3
相关文章
相关标签/搜索