Tensorflow tensor

Time:2021-10-8

Concept of Tensor

Tensor in tensorflow is tensor, which is a mathematical object and a generalization of scalar, vector and matrix. We can directly understand that tensors are lists and multidimensional arrays.

 

The dimension of the tensor is expressed in order:

0-order tensor scalar single value example: a = 1
Dimension group of tensor vector 1 of order 1: a = [1,2,3]
2-dimensional group of second-order tensor matrix: a = [[1,2,3], [4,5,6]]
N-order tensor n-dimension group example: a = [[[[… N parentheses…]]

 

How many orders does the judgment tensor have   Just look at the square brackets to the right of the equal sign   0 is order 0 and 2 is order 2.

Multidimensional tensors are also stored continuously in memory in the form of one-dimensional arrays.

 

Create tensor

The TF. Constant () method is used to create a tensor. The incoming content can be numbers, python lists and numpy arrays. The second parameter is the specified data type and can be omitted.

For example:

import tensorflow as tf
a = tf.constant([1,2,3],dtype=tf.int32)
print(a)

Print results:

tf.Tensor([1 2 3], shape=(3,), dtype=int32)

 

Shape is the shape of a tensor. The order of a tensor depends on how many numbers are separated by commas in the shape. For example, the tensor with shape (3,) is separated by commas, so its order is order 1. There are three elements in it. For example, the tensor with shape (3,3)   The comma separates the numbers 3 and 3, so its order is order 2, and it is a matrix with 3 rows and 3 columns.

Of course, you can also view tensor information in the form of attributes

Print ('order of tensor: ', a.ndim)
Print ('tensor shape: ', a.shape)
Print ('type of tensor: ', a.dtype)

Print results:

Order of tensor: 1
Tensor shape: (3,)
Type of tensor:

 

Data type of tensor

In the above tensor creation, I added the dtype parameter to specify the data type as int32, and it is the same without specifying the data type. Tensorflow will automatically infer the type according to the tensor content. If the tensor content is a number, the default data type is int32. If the tensor content is a floating point number, the default data type is float32, If you need to specify other bits, such as Int64 or float64, you need to pass parameters and specify them manually

The data types of tensors are:

Int signed integer
Float signed floating point number
Uint unsigned integer
The above three types are divided into digits, such as tf.int8, tf.int16, tf.int32 and tf.int64.

String string
Bool Boolean
Complex complex numbers consist of real and imaginary numbers

 

TF. Cast () method can convert the data type of tensor, for example:

import tensorflow as tf
a = tf.constant([1,2,3],dtype=tf.int32)
print(a)

b = tf.cast(a,dtype=tf.int64)
print(b)

Print results:

tf.Tensor([1 2 3], shape=(3,), dtype=int32)
tf.Tensor([1 2 3], shape=(3,), dtype=int64)

 

Numpy data conversion

If the data is created by numpy, you need to use TF. Convert_ to_ The tensor () function converts the data type of numpy to the tensor data type of tensorflow.

import tensorflow as tf
import numpy as np
a = np.arange(0,10)
print(a)
b = tf.convert_to_tensor(a)
print(b)

Print results:

[0 1 2 3 4 5 6 7 8 9]
tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)

 

Note that when numpy creates a floating-point matrix, the default floating-point type is float64. During conversion, tensorflow will also receive its data type. Therefore, the tensor generated by numpy data will have the default floating-point type of float64, as follows:

import tensorflow as tf
import numpy as np
a = np.array([1.1,2.2,3.3])
print(a)
b = tf.constant(a)
print(b)
c = tf.convert_to_tensor(a)
print(c)

Print results:

[1.1 2.2 3.3]
tf.Tensor([1.1 2.2 3.3], shape=(3,), dtype=float64)
tf.Tensor([1.1 2.2 3.3], shape=(3,), dtype=float64)

 

In most machine learning scenarios, 32-bit floating-point numbers are enough to meet the calculation requirements. The operation of 32-bit floating-point numbers is much faster than that of 64 bit floating-point numbers. Therefore, when generating tensors with numpy array, we can explicitly specify the data type:

import tensorflow as tf
import numpy as np
a = np.array([1.1,2.2,3.3])
print(a)
b = tf.constant(a,dtype=tf.float32)
print(b)
c = tf.convert_to_tensor(a,dtype=tf.float32)
print(c)

Print results:

[1.1 2.2 3.3]
tf.Tensor([1.1 2.2 3.3], shape=(3,), dtype=float32)
tf.Tensor([1.1 2.2 3.3], shape=(3,), dtype=float32)

 

Fixed tensor

Tensorflow provides some functions to create fixed tensors, such as tensors with all values of 0 and tensors with all values of 1.

Total zero tensor

 tf.zeros(shape,dtype=float32)

There are more than one way to write parameters, just remember one easy to remember, such as one-dimensional direct write number, two-dimensional write [number of rows and columns], three-dimensional write [number of rows, columns and height], multi-dimensional and so on [n, m, K, J,…]

#Create 4 elements of one-dimensional tensor
a = tf.zeros(4)
print(a)

#Create 4 elements of one-dimensional tensor
aa = tf.zeros([4])
print(aa)

#Create a two-dimensional tensor with 3 rows and 3 columns
b = tf.zeros([3,3])
print(b)

#Create a two-dimensional tensor with 3 rows and 3 columns
bb = tf.zeros((3,3))
print(bb)

#Specify type
c = tf.zeros([3,3],tf.int8)
print(c)

 

Total 1 tensor

 tf.ones(shape,dtype=float32)

Create tensors whose values are all 1. Except for replacing zeros function with ones function, the creation usage is exactly the same as that of all 0 tensors.

 

Tensor with the same element value

tf.fill(shape,value)

The fill function has no dtype parameter. It automatically determines the type according to the value parameter.

#Create 4 elements of one-dimensional tensor
aa = tf.fill([4],10)
print(aa)

Print results:

tf.Tensor([10 10 10 10], shape=(4,), dtype=int32)

 

#Create a two-dimensional tensor with 3 rows and 3 columns
c = tf.fill([3,3],4.4)
print(c)

Print results:

tf.Tensor(
[[4.4 4.4 4.4]
[4.4 4.4 4.4]
[4.4 4.4 4.4]], shape=(3, 3), dtype=float32)

 

Random number tensor

Normal distribution

Function signature: tf.random.normal (shape, mean, StdDev, dtype)

Shape: tensor shape
Mean: the default value is 0
StdDev: the standard deviation is 1 by default
Dtype: the storage type is float32 by default

 

Example:

#Create a random number tensor with a mean of 0 and a standard deviation of 1 in 2 rows and 2 columns
a = tf.random.normal([2, 2], mean=0, stddev=1)
print(a)

Print results:

tf.Tensor(
[[-0.849204 0.42851505]
[-2.1599882 -0.6131579 ]], shape=(2, 2), dtype=float32)

 

Truncated normal distribution

Function signature: tf.random.truncated_ normal(shape,mean,stddev,dtype)

This function returns a truncated normal distribution. The truncated standard is twice the standard deviation, which can ensure that the generated data is more concentrated to the mean.

 

Example:

b = tf.random.truncated_normal([2, 2], mean=0, stddev=1)
print(b)

Print results:

tf.Tensor(
[[-0.06899758 1.8015716 ]
[-0.39744875 0.00498725]], shape=(2, 2), dtype=float32)

 

Generally speaking, when the mean value is 0 and the standard deviation is 1, points outside the interval [- 2,2] cannot appear in the random tensor created by this function, while points outside the interval [- 2,2] may appear by using tf.random.normal function.

 

Set random seed

If you want to ensure that the random number tensors generated multiple times are completely consistent, you can set the same random seed.

tf.random.set_seed(10)
a = tf.random.normal([1,5])
print(a)

tf.random.set_seed(10)
b = tf.random.normal([1,5])
print(b)

Print results:

tf.Tensor([[-0.8757808 0.3356369 -0.35219625 -0.30314562 -0.03882965]], shape=(1, 5), dtype=float32)
tf.Tensor([[-0.8757808 0.3356369 -0.35219625 -0.30314562 -0.03882965]], shape=(1, 5), dtype=float32)

 

uniform distribution

Function signature: tf.random.uniform (shape, minval, maxval, dtype)

Shape: tensor shape
Minval: minimum closed interval
Maxval: maximum open interval
Dtype: the default data type is float32

c = tf.random.uniform([2, 2], minval=0, maxval=1)
print(c)

Print results:

tf.Tensor(
[[0.46760368 0.58308244]
[0.5945486 0.2994659 ]], shape=(2, 2), dtype=float32)

 

Sequence tensor

Function signature: tf.range (start, limit, Delta, dtype)

Start: closed interval of starting number
Limit: end the digital open interval
Delta: step size is 1 by default
Dtype: default auto inference of data type

 

The usage is as follows:

#Create 0-9 series
a = tf.range(10)
print(a)

Print results:

tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)

 

#Create even sequence
b = tf.range(10,delta=2)
print(b)

Print results:

tf.Tensor([0 2 4 6 8], shape=(5,), dtype=int32)

 

#Create odd sequence
c = tf.range(1,10,delta=2)
print(c)

Print results:

tf.Tensor([1 3 5 7 9], shape=(5,), dtype=int32)

 

Tensor operation

Four arithmetic

Note that only tensors of the same order can do four operations

 

Add: tf.add

a = tf.constant([1,2,3])
b = tf.constant([1,2,3])
print(tf.add(a,b))

Print results:

tf.Tensor([2 4 6], shape=(3,), dtype=int32)

 

Subtraction: tf.subtract

a = tf.constant([2,4,6])
b = tf.constant([0,0,3])
print(tf.subtract(a,b))

Print results:

tf.Tensor([2 4 3], shape=(3,), dtype=int32)

 

Multiplication: ttf.multiply

a = tf.constant([2,4,6])
b = tf.constant([0,0,3])
print(tf.multiply(a,b))

Print results:

tf.Tensor([ 0  0 18], shape=(3,), dtype=int32)

 

Division: tf.divide

a = tf.constant([2,4,6])
b = tf.constant([0,0,3])
print(tf.divide(b,a))

Print results:

tf.Tensor([0.  0.  0.5], shape=(3,), dtype=float64)

 

Square, power and square

Square the tensor: tf.square

a = tf.constant([1,2,3,4,5])
print(tf.square(a))

Print results:

tf.Tensor([ 1  4  9 16 25], shape=(5,), dtype=int32)

 

Find the power of tensor: tf.pow

A = tf.range (5) # create a sequence tensor 0 1 2 3 4
B = tf.pow (a, 3) # find the 3rd power of sequence tensor
print(b)

Print results:

tf.Tensor([ 0  1  8 27 64], shape=(5,), dtype=int32)

 

If we find the power of the B tensor of the a tensor, it is equivalent to finding the power of the corresponding element in the B tensor from the element at each position of the a tensor.

a = tf.constant([[2,2],[2,2]])
b = tf.constant([[1,2],[3,4]])
print(tf.pow(a,b))

Print results:

tf.Tensor(
[[ 2 4]
[ 8 16]], shape=(2, 2), dtype=int32)

 

If the exponent is a fraction, it is the root operation. For example, if the index is 0.5, it is to find the square root of the tensor element by element.

a = tf.constant([1,4,9,16,25],dtype=tf.float32)
print(tf.pow(a,0.5))

Print results:

tf.Tensor([1. 2. 3. 4. 5.], shape=(5,), dtype=float32)

The open root sign involves a decimal point, so the tensor element type must be a floating point type

 

Square root of tensor: tf.sqrt

a = tf.constant([1,4,9,16,25],dtype=tf.float64)
print(tf.sqrt(a))

Print results:

tf.Tensor([1. 2. 3. 4. 5.], shape=(5,), dtype=float64)

 

Other operations

In addition to common operations, tensorflow provides a wider range of operations.

TF. Sign (x) take sign
TF. ABS (x) find the absolute value
TF. Negative (x) find the opposite number
TF. Reciprocal (x) find reciprocal
Tf.ceil (x) rounded up
TF. Floor (x) rounded down
TF. Round (x) round
Tf.maximum (x, y) returns the maximum value of the two tensors (compare the elements of the two tensors one by one, take the maximum value, and finally return a new tensor)
Tf.minimum (x, y) returns the minimum value of two tensors (compare the elements of two tensors one by one, take the minimum value, and finally return a new tensor)

 

overload operators

In order to be easy to use, tensorflow overloads the commonly used mathematical operation symbols, so that we can directly operate the tensor without explicitly calling the function.

For example, in addition operation, TF. Add (a, b) can be directly written as a + B

a = tf.constant([1,2,3])
b = tf.constant([1,2,3])
print(tf.add(a,b))
print(a+b)

Print results:

tf.Tensor([2 4 6], shape=(3,), dtype=int32)
tf.Tensor([2 4 6], shape=(3,), dtype=int32)

 

Matrix multiplication

Matrix multiplication is a very important content in linear algebra. The condition of matrix multiplication is that the number of columns of the first matrix must be equal to the number of rows of the second matrix.

The calculation rule is that each number of each row of the first matrix is multiplied by each number of each column of the second matrix, and then added. The calculation result is the number of rows of the first matrix and the value corresponding to the number of columns of the second matrix.

 

For example, matrix A:
2 3
4 5

Matrix B:
1 2
3 4

AB two matrices are multiplied to obtain a new matrix C
C11:2*1 + 3*3 = 11
C12:2*2 + 3*4 = 16
C21:4*1 + 5*3 = 19
C22:4*2 + 5*4 = 28

That is:
11 16
19 28

 

You can use the TF. Matmul (a, b) function to calculate the matrix product or the @ operator.

a = tf.constant([[2,3],[4,5]])
print(a)
b = tf.constant([[1,2],[3,4]])
print(b)
print(tf.matmul(a,b))

Print results:

tf.Tensor(
[[2 3]
[4 5]], shape=(2, 2), dtype=int32)
tf.Tensor(
[[1 2]
[3 4]], shape=(2, 2), dtype=int32)
tf.Tensor(
[[11 16]
[19 28]], shape=(2, 2), dtype=int32)

 

Tensor statistics

There are four common statistical functions:

tf.reduce_ Sum (tensor, axis)
tf.reduce_ Mean (tensor, axis)
tf.reduce_ Max (tensor, axis)
tf.reduce_ Min (tensor, axis)

When the parameter axis is 0, it represents vertical operation, along the longitude direction (i.e. by column and cross column). When axis is 1, it represents horizontal operation, along the latitude direction (i.e. by row and cross column). If axis is not specified, it represents global operation.

a = tf.constant([[5,3,1],[2,4,6]])
print(a)

Print ('vertical operation: {} '. Format (TF. Reduce_sum (a, 0)))
Print ('horizontal operation: {} '. Format (TF. Reduce_sum (a, 1)))
Print ('global operation: {} '. Format (TF. Reduce_sum (a)))

Print results:

tf.Tensor(
[[5 3 1]
[2 4 6]], shape=(2, 3), dtype=int32)
Longitudinal operation: [7]
Lateral operation: [9 12]
Global operations: 21

 

To calculate the average value, it should be mentioned that the data type of the calculated result will be consistent with the data type of the original tensor, and the following phenomena will occur:

a = tf.constant([[5,3,1],[2,4,6]])
print(a)
print(tf.reduce_mean(a,0))

Print results:

[[5 3 1]
[2 4 6]], shape=(2, 3), dtype=int32)
tf.Tensor([3 3 3], shape=(3,), dtype=int32)

The first column of vertical average value is 5 + 2 = 7, divided by 2 to get 3.5. However, due to the int data type, the final value is 3

In order to solve this problem, we can convert the data type of tensor into float and then make statistics.

a = tf.constant([[5,3,1],[2,4,6]])
print(a)
print(tf.reduce_mean(tf.cast(a,dtype=tf.float32),0))

Print results:

tf.Tensor(
[[5 3 1]
[2 4 6]], shape=(2, 3), dtype=int32)
tf.Tensor([3.5 3.5 3.5], shape=(3,), dtype=float32)

 

Recommended Today

New childhood fairy tale [139] little prince 32 — I planted this tree

Pain makes people sober. Everything in life originally depends on themselves. The mercy of others can’t win a better future. A petite thief said, “this time… I planted the tree and this time… I opened the road. If you want to buy the road money this time… ························································································· Another tall thief who blocked the way […]