Tensorflow Fast Food Tutorial (3) - Vector

Original link: click to open the link

Summary:  Tensorflow vector operations

vector

Vectors are the most commonly used one-dimensional arrays in programming languages.
Two-dimensional arrays are called matrices, and more than three-dimensional arrays are called tensors.

Vectors are simple, efficient, and easy to understand. But it is still different from manipulating 0-dimensional scalar data. For example, a vector is often used to represent a sequence, and it is not cost-effective to generate a sequence like a scalar one by one manually. Of course it can be written in a loop. This is fine in a vector, but if it is in a matrix or a tensor, it is strongly recommended not to use a loop to do it. The functions provided by the system are generally highly optimized and can use GPU resources for acceleration.
On the one hand, we use the functions of the system as much as possible, and on the other hand, we should not be superstitious about them. Code optimization is a practical process that can be compared and measured.

A quick way to generate vectors

The range function generates an arithmetic sequence

The tf.range function is used to quickly generate an arithmetic sequence. It is equivalent to the np.arange function when we talked about numpy before.

prototype:

tf.range(start, limit, delta=1, dtype=None, name='range')

example:

>>> b11 = tf.range(1,100,1)
>>> b11
<tf.Tensor 'range:0' shape=(99,) dtype=int32>
>>> sess.run(b11)
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
       35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
       52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
       69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
       86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99],
      dtype=int32)

linspace generates floating-point arithmetic arrays

The difference between tf.linspace and tf.range is that the data types are different.

tf.lin_space(
    start,
    stop,
    num,
    name=None
)

Among them, start and stop must be floating point numbers, and the type must be the same. num must be an integer.

example:

>>> a2 = tf.linspace(1.0,10.0,4)  
>>> a2
<tf.Tensor 'LinSpace_2:0' shape=(4,) dtype=float32>
>>> sess.run(a2)
array([ 1.,  4.,  7., 10.], dtype=float32)

tile

It is to repeat a certain vector several times.

>>> a10 = tf.range(1,4,1)
>>> sess.run(a10)
array([1, 2, 3], dtype=int32)
>>> a11 = tf.tile(a10,[3])
>>> sess.run(a11)
array([1, 2, 3, 1, 2, 3, 1, 2, 3], dtype=int32)

vector operations

reverse the vector

You can use the tf.reverse function.
prototype:

tf.reverse(
    tensor,
    axis,
    name=None
)

Tensor is a vector, the axis axis is not important for the vector, just give [-1]. The tossing axis is a matter of tensor time, and it is not used for the time being.

>>> a2 = tf.linspace(1.0,10.0,4)
>>> a3 = tf.reverse(a2,[-1])
>>> sess.run(a3)
array([10.,  7.,  4.,  1.], dtype=float32)

slice

Slicing is also one of the common operations for vectors, which is to take part of an array.

example:

>>> a5 = tf.linspace(1.0,100.0, 10)
>>> sess.run(a5)
array([  1.,  12.,  23.,  34.,  45.,  56.,  67.,  78.,  89., 100.],
      dtype=float32)
>>> a6 = tf.slice(a5, [2],[4])
>>> sess.run(a6)
array([23., 34., 45., 56.], dtype=float32)

When dealing with tensors in the future, it will be much more fun if we slice a piece from a matrix, or slice a piece from a tensor. But the principle is the same as for vectors.

connect

tf.concat also requires given axis information. For two linear vectors, we just give 0 or -1.

>>> a20 = tf.linspace(1.0,2.0,10)
>>> sess.run(a20)
array([1.       , 1.1111112, 1.2222222, 1.3333334, 1.4444444, 1.5555556,
       1.6666667, 1.7777778, 1.8888888, 2.       ], dtype=float32)
>>> a21 = tf.linspace(2.0,3.0,5)
>>> sess.run(a22)
array([1.       , 1.1111112, 1.2222222, 1.3333334, 1.4444444, 1.5555556,
       1.6666667, 1.7777778, 1.8888888, 2.       , 2.       , 2.25     ,
       2.5      , 2.75     , 3.       ], dtype=float32)
>>> a23 = tf.concat([a20,a21],-1)
>>> sess.run(a23)
array([1.       , 1.1111112, 1.2222222, 1.3333334, 1.4444444, 1.5555556,
       1.6666667, 1.7777778, 1.8888888, 2.       , 2.       , 2.25     ,
       2.5      , 2.75     , 3.       ], dtype=float32)

vector calculation

vector addition and subtraction

Addition and subtraction operations can be performed between vectors of the same length.

example:

>>> a40 = tf.constant([1,1])
>>> a41 = tf.constant([2,2])
>>> a42 = a40 + a41
>>> sess.run(a42)
array([3, 3], dtype=int32)
>>> a43 = a40 - a41
>>> sess.run(a43)
array([-1, -1], dtype=int32)
>>> a43
<tf.Tensor 'sub:0' shape=(2,) dtype=int32>

vector multiplication and division scalar

Vector multiplication and division scalar is also very easy to understand, that is, multiplication and division are performed for each number in the vector.

example:

>>> a44 = a40 * 2
>>> sess.run(a44)
array([2, 2], dtype=int32)
>>> a45 = a44 / 2  
>>> sess.run(a45)
array([1., 1.])
>>> a44
<tf.Tensor 'mul:0' shape=(2,) dtype=int32>
>>> a45
<tf.Tensor 'truediv_1:0' shape=(2,) dtype=float64>

broadcast operation

如果针对向量和标量进行加减运算,也是会对向量中的每个数进行加减运算。这种操作称为广播操作。

例:

>>> a46 = a40 + 1
>>> sess.run(a46)
array([2, 2], dtype=int32)
>>> a46
<tf.Tensor 'add_1:0' shape=(2,) dtype=int32>

向量乘法

两个向量相乘,默认的运算是求元素对应乘积(element-wise product),也叫做Hadamard积。

例:

>>> b1 = tf.constant([1,2])
>>> b2 = tf.constant([2,1])
>>> b3 = b1 * b2
>>> b3
<tf.Tensor 'mul_7:0' shape=(2,) dtype=int32>
>>> sess.run(b3)
array([2, 2], dtype=int32)

直接调用tf.multiply也是同样的效果,例:

>>> b4 = tf.multiply(b1,b2)
>>> b4   
<tf.Tensor 'Mul_2:0' shape=(2,) dtype=int32>
>>> sess.run(b4)
array([2, 2], dtype=int32)

如果要计算点积(dot product)的话,我们得提前剧透一下矩阵的内容了。
首先,用向量是没法做矩阵计算的。
例:

>>> a21 = tf.constant([2,3]) 
>>> a22 = tf.constant([4,5])
>>> a21   
<tf.Tensor 'Const_20:0' shape=(2,) dtype=int32>
>>> a22
<tf.Tensor 'Const_21:0' shape=(2,) dtype=int32>

这样(2,)的形状是向量,我们得先把它转换成(2,1)这样的单行矩阵,如下:

>>> a31 = tf.constant(sess.run(tf.reshape(a21,[2,1])))
>>> a32 = tf.constant(sess.run(tf.reshape(a22,[2,1])))
>>> a31
<tf.Tensor 'Const_22:0' shape=(2, 1) dtype=int32>
>>> a32
<tf.Tensor 'Const_23:0' shape=(2, 1) dtype=int32>

下面我们终于可以计算点积了,我们知道点积A.B相当于A的转置乘以B,我们可以通过matmul函数来进行矩阵乘法。

>>> a31 = tf.matmul(a31,a32,transpose_a=True)
>>> sess.run(a31)
array([[23]], dtype=int32)

我们也可以用tf.tensordot函数来计算点积。我们刚才为什么没用呢?答案是tensordot要求是浮点型矩阵。
例:
第一步,需要浮点数:

>>> f01 = tf.constant([1,1],dtype=tf.float32) 
>>> f02 = tf.constant([1,2],dtype=tf.float32)

第二步,reshape成单行矩阵:

>>> f11 = tf.constant(sess.run(tf.reshape(f01,[2,1])))
>>> f12 = tf.constant(sess.run(tf.reshape(f02,[2,1])))
>>> f11
<tf.Tensor 'Const_26:0' shape=(2, 1) dtype=float32>
>>> f12
<tf.Tensor 'Const_27:0' shape=(2, 1) dtype=float32>

第三步,调用tensordot

>>> f13 = tf.tensordot(f11,f12,2)
>>> sess.run(f13)
3.0

小结

从上面我们学习的函数我们可以看到,与普通语言中提供的函数多是为一维数组操作不同,Tensorflow中的切片、拼接等操作也是基于张量的。
当我们后面学到张量遇到困难时,不妨回来看下这一节。不管后面张量多么复杂,其实也只是从一维向二维和多维推广而己。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324910108&siteId=291194637