Tensor generation
A tensor of order 0 is called a scalar, which represents a single number, such as s=123,
a tensor of order 1 is called a vector, which represents a one-dimensional array, such as a list v=[1, 2, 3]
tensor of order 2 It is called a matrix, which represents a two-dimensional array, which can have i-row and j-column elements, and each element is indexed by its row number and column number. For example, in a matrix, the index of element 2 is the 0th row and 1st column of matrix m
To determine the order of the tensor, it depends on how many'[' square brackets. 0 is order 0, 1 is order 1, 2 is order 2, and n is order n.
So tensors can represent arrays from order 1 to order n.
Tensorflow's data types are 32-bit integer, 32-bit floating point, 64 Bit float, boolean, string, etc.
We can use constant() to create a tensor. The first parameter specifies the content of the tensor, and the second parameter specifies the data type of the tensor.
Code example:
import tensorflow as tf
a = tf.constant([1, 5], dtype=tf.int64)#创建一个一阶张量,里面有两个元素1 5
#指定数据类型是64位整形 并赋值给a
print("a:", a)#打印出a
print("a.dtype:", a.dtype)#打印出a的数据类型
print("a.shape:", a.shape)#打印出a的形状
# 本机默认 tf.int32 可去掉dtype试一下 查看默认值
Result analysis:
Printing a will output all the information of the tensor, including the content of the tensor, the shape of the tensor, and the data type of the
tensor. The shape of the tensor is separated by several numbers and several numbers separated by a comma. It means that this tensor is several-dimensional.
In this example, a number is separated by parentheses in the shape brackets, indicating that it is a one-dimensional tensor. This number is 2, indicating that there are two elements in this tensor, namely the value 1 and the value 5.
Many times the data is composed of Given in numpy format, we can use convert_to_tensor() to turn it into Tensor format
import tensorflow as tf
import numpy as np
a = np.arange(0, 5)
b = tf.convert_to_tensor(a, dtype=tf.int64)
print("a:", a)
print("b:", b)
For example:
b = tf.convert_to_tensor(a, dtype=tf.int64)
This statement turns numpy format a into Tensor format b
Similarly, we can use zeros() to create a tensor with all 0s, use ones() to create a tensor with all 1s, and fill() to create a tensor with all specified values.
import tensorflow as tf
a = tf.zeros([2, 3])
b = tf.ones(4)
c = tf.fill([2, 2], 9)
print("a:", a)
print("b:", b)
print("c:", c)
Operation result:
tf.zeros([2, 3]) creates a two-dimensional tensor, the first dimension has two elements, and the second dimension has three elements, that is, 2 rows by 3 columns. The contents of the elements are all 0
tf.ones(4) creates a one-dimensional tensor with 4 elements and the contents are all 1
tf.fill([2, 2], 9) creates a two-row two-column Two-dimensional tensor, the first dimension has two elements, the second dimension also has two elements, the content is 9
The dimension can be remembered as follows:
When writing code, we often randomly generate initialization parameters that must conform to the normal distribution. The tf.random.normal() function generates a tensor of the specified dimension conforming to the normal distribution.
Sometimes we want to generate random numbers More focused, so you can use the
tf.random.truncated_normal() function.
Code example:
import tensorflow as tf
d = tf.random.normal([2, 2], mean=0.5, stddev=1)
print("d:", d)
e = tf.random.truncated_normal([2, 2], mean=0.5, stddev=1)
print("e:", e)
For example,
use tf.random.normal() to generate a tensor with two rows and two columns. The elements inside conform to a distribution
with 0.5 as the mean and 1 as the standard deviation. Use tf.random.truncated_normal() to generate a tensor with two rows and two columns. The elements inside conform to a random number with 0.5 as the mean and 1 as the standard deviation, and these elements are distributed within the mean plus or minus twice the standard deviation , and the data is more concentrated towards the mean 0.5
You can use tf.random.uniform() to generate a uniformly distributed random number of a specified dimension, use minval to give the minimum value of the random number, and maxval to give the maximum value of the random number.
The minimum and maximum is the interval before closing and then opening. For
example: tf.random.uniform([2, 2], minval=0, maxval=1)
generates a tensor with two rows and two columns, and each element of it conforms to 0 and Average distribution between 1