【前言】这篇文章将告诉你tensorflow的基本概念以及tensorflow的基本操作

TensorFlow的基本概念Tensor

TensorFlow的核心API

TensorFlow提供非常丰富的API,最底层的API—TensorFlow Core—提供了完整的编程控制接口。更高级别的API则是基于TensorFlow Core API之上,并且非常容易学习和使用,更高层次的API能够使一些重复的工作变得更加简单。比如tf.contrib.learn帮助你管理数据集等。

这篇博文将从TensorFlow Core开始。

Tensors(张量)

张量是所有深度学习框架中最核心的组件,TensorFlow中也是,在TensorFlow中,将数据表示为Tensor,一个Tensor是一个多维数组,例如,你可以将一批图像表示为一个四维数组[batch,height,width,channels],数组中的值均为浮点数。

在几何代数中定义的张量是基于向量和矩阵的推广,通俗理解,我们可以将标量视为零阶张量,那么矩阵就是二阶张量。

举例来说,我们可以将任意一张RGB彩色图片表示成一个三阶张量(三个维度分别是图片的高度、宽度和色彩数据)。

在线tensorflow拟合函数 tensorflow r_在线tensorflow拟合函数

如图是一张普通水果图片按照RGB三原色拆分成的三张灰度图片,如果将这种表示方法用张量的形式写出来,就是下图的表格。

在线tensorflow拟合函数 tensorflow r_数组_02

图中只显示了前5行、320列数据,每个方格代表一个像素点,其中的数据[1.0,1.0,1.0]即为颜色。假设用[1.0,0,0]表示红色,[0,1.0,0]代表绿色,[0,0,1.0]代表蓝色,那么如图所示,前面5行的数据则全部是白色。

将这一定义进行扩展,我们也可以用四阶张量表示一个包含多张图片的数据集,其中的四个维度分别是:图片在数据集中的编号,图片高度、宽度,以及色彩数据。

将各种各样的数据抽象成张量表示,然后再输入神经网络模型进行后续处理是一种非常必要且高效的策略。因为如果没有这一步骤,我们就需要根据各种不同类型的数据组织形式定义各种不同类型的数据操作,这会浪费大量的开发者精力。更关键的是,当数据处理完成后,我们还可以方便地将张量再转换回想要的格式。例如Python NumPy包中numpy.imread和numpy.imsave两个方法,分别用来将图片转换成张量对象(即代码中的Tensor对象),和将张量再转换成图片保存起来。

高维向量(Multidimensional Arrays)在matlab中的定义

An array having more than two dimensions is called a multidimensional array in the MATLAB® application. Multidimensional arrays in MATLAB are an extension of the normal two-dimensional matrix. Matrices have two dimensions: the row dimension and the column dimension.

在线tensorflow拟合函数 tensorflow r_数据_03

You can access a two-dimensional matrix element with two subscripts: the first representing the row index, and the second representing the column index.

Multidimensional arrays use additional subscripts for indexing. A three-dimensional array, for example, uses three subscripts:

  • The first references array dimension 1, the row.
  • The second references dimension 2, the column.
  • The third references dimension 3. This illustration uses the concept of a page to represent dimensions 3 and higher.

To access the element in the second row, third column of page 2, for example, you use the subscripts (2,3,2).

在线tensorflow拟合函数 tensorflow r_API_04

As you add dimensions to an array, you also add subscripts. A four-dimensional array, for example, has four subscripts. The first two reference a row-column pair; the second two access the third and fourth dimensions of data.

Most of the operations that you can perform on matrices (i.e., two-dimensional arrays) can also be done on multidimensional arrays.

理解高维数组的建议

需要和高维数组(Tensor)打交道的话,思考时不要想着row, column, page这些术语, 要用dim_1, dim_2,... dim_M 来思考。好多地方说Matlab是先存column, 用我们的术语其实就是Matlab按照从左到右:dim_1, dim_2,... dim_M的顺序存储元素。例如想想下面 2 x 4 x 3 x 5 矩阵的存储顺序:

可视化高维数组

在线tensorflow拟合函数 tensorflow r_数据_05

在线tensorflow拟合函数 tensorflow r_API_06

Rank

Rank本意是矩阵的秩,不过Tensor Rank和Matrix Rank的意义不太一样,这里就还叫Rank防止混淆了。Tensor Rank的意义看起来更像是维度,比如Rank = 1就是向量,Rank = 2就是矩阵了。(Rank = 0就是一个值了)

官网原话:A tensor's rank is its number of dimensions. 

3# a rank 0 tensor; this is a scalar with shape []
[1.2., 3.] # a rank 1 tensor; this is a vector with shape [3]
[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]
[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]

Shape

Shape就是Tensor在各个维度上的长度组成的数组,譬如Rank = 0的Tensor Shape = [](因为没有维度嘛),Rank = 1的Tensor Shape = [ a ],Rank = 2的Tensor Shape = [ a, b ]这样。