Tensorflow Fast Food Tutorial (3) - Vector

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)

summary

From the functions we learned above, we can see that, unlike the functions provided in ordinary languages, most of them operate on one-dimensional arrays, and operations such as slicing and splicing in Tensorflow are also based on tensors.
When we encounter difficulties with tensors later, we may come back to this section. No matter how complicated the latter tensor is, it is actually just a generalization from one-dimensional to two-dimensional and multi-dimensional.

Guess you like

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