Tensor data-related operation and function explanation
Tensor
It is used in TensorFlow to represent data. Can be viewed as a multidimensional array or list.
Scalar is tensor, vector is tensor, matrix is tensor, matrix is tensor
Several definition methods commonly used
A 1.variable variable, typically a value that can be updated or changed, that is, the values that can be dynamically adjusted while the flow diagram is running. When we train a model, we use the variable (Variables) in TensorFlow, which we need to keep and update the parameter values, as well as the tensor, and the variables are stored in the memory buffer.
We have to initialize the variables beforehand, and the TensorFlow variables must be initialized before the values are available. The constant value tensor is not required, the variable can be set to initialize the way, but the real initialization is to Sess.run (Tf.global_variables_initializer ())
is really initialized.
2.CONSTANT constant tensor
3.placeholder: Placeholder Dynamic change value feeddict
NumPy
b = Np.array ([(1.5,2,3), (4,5,6)])
The difference between TensorFlow and NumPy
Same point: Both provide n-bit arrays
Different points: NumPy supports Ndarray, while TensorFlow has tensor;numpy does not provide create tensor functions and derivations, nor does it provide GPU support.
Show
Tensor
Need to add the Eval function
Ta = Tf.zeros ((2,2))
Print (TA)
Tensor ("zeros_1:0", Shape= (2, 2), Dtype=float32)
Print (Ta.eval ())
NumPy
A = Np.zeros ((2,2))
Print (a)
Tensor related Operations
Arithmetic operations
1. Addition operation
The effects of Tensor and NumPy two are automatically expanded when they encounter different dimensions uniformly. But the size of the same dimension must be the same, except for a certain dimension where the value is 1.
The shape of the tensor is (tensor,1) and (1,tensor) which can be added and automatically expanded.
2. Matrix multiplication
Tensor
A * B means to calculate by element
Tf.mul (A,B) represents a calculation by element
Tf.matmul (a,b) representation matrix multiplication
3.numpy
A * B means to calculate by element
Dot (a,b) representation matrix multiplication
Data type conversions
Tf.to_double (a)
Tf.to_float (a)
Tf.cast (x, Dtype, Name=none)
Tensor A is [1.8, 2.2], dtype=tf.float
Tf.cast (A, Tf.int32) ==> [1, 2] # Dtype=tf.int32
Shape action
1.shape
Numpy:a.shape ()
Tensor:a.get_shape () Tf.shape (a)
2.reshape
Tensor:tf.reshape (A, (1,4))
Numpy:np.reshape (A, (1,4))
3.tf.size (a) number of elements that return data
Tf.constant ([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]] size = 12
4.tf.rank (a) returns rank of tensor
# ' t ' is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]
# shape of tensor ' t ' is [2, 2, 3]
Rank (t) ==> 3
5. A sum of one dimension
Tensor:tf.reduce_sum (B,reduction_indices=1)
Numpy:np.sum (B,axis=1)
Slices and merges for arrays
1. Merging, connecting arrays
Tensor
Tf.concat (0,[a,b]) The first argument expresses the number of digits
If a (1,128,128,3) b (1,128,128,3)
Tf.concat (0,[a,b]) (2,128,128,3)
NumPy
Vstack and Hstack
Stack (a,axis=)
2. Get whole row of data
Tensor
temp = Tf.constant (0,shape=[5,5])
Temp1 = temp[0,:] Getting a row
Temp2 = temp[:,1] Gets a column
temp[1,1] Get an element
Temp[1:3,1:3] Gets a range of row and column elements
Separation of tensor from Num_split tensors along a dimension
Tf.split (Split_dim, Num_split, value, name= ' Split ')
# ' value ' is a tensor with shape [5, 30]
# Split ' value ' into 3 tensors along Dimension 1
Split0, split1, split2 = Tf.split (1, 3, value)
Tf.shape (split0) ==> [5, 10]
3. Slice operations on tensor
Tf.slice (input_, begin, size, Name=none)
# ' input ' is
#[[[1, 1, 1], [2, 2, 2]],[[3, 3, 3], [4, 4, 4]],[[5, 5, 5], [6, 6, 6]]]
Tf.slice (input, [1, 0, 0], [1, 1, 3]) ==> [[[3, 3, 3]]]
Tf.slice (input, [1, 0, 0], [1, 2, 3]) ==>
[[[3, 3, 3],
[4, 4, 4]]]
Tf.slice (input, [1, 0, 0], [2, 1, 3]) ==>
[[[3, 3, 3]],
[[5, 5, 5]]]
4. Packing
Tf.pack (values, axis=0, name= ' pack ')
# ' x ' is [1, 4], ' Y ' are [2, 5], ' Z ' is [3, 6]
Pack ([x, Y, z]) => [[1, 4], [2, 5], [3, 6]]
# along the first dimension pack
Pack ([x, Y, z], Axis=1) => [[1, 2, 3], [4, 5, 6]]
Equivalent to Tf.pack ([x, Y, z]) = Np.asarray ([x, Y, z])
5.tf.reverse (tensor, dims, Name=none)
Sequence reversal along a dimension
Where Dim is a list, the element is bool, size equals rank (tensor)
# tensor ' t ' is
[[[[0, 1, 2, 3],
#[4, 5, 6, 7],
#[8, 9, 10, 11],
#[[12, 13, 14, 15],
#[16, 17, 18, 19],
#[20, 21, 22, 23]]]
# tensor ' t ' shape is [1, 2, 3, 4]
# ' dims ' is [False, False, False, True]
Reverse (t, dims) ==>
[[[[3, 2, 1, 0],
[7, 6, 5, 4],
[11, 10, 9, 8]],
[[15, 14, 13, 12],
[19, 18, 17, 16],
[23, 22, 21, 20]]]
6.tf.transpose (A, Perm=none, name= ' transpose ')
Swap tensor dimension Order
If defined, the perm is (n-1 ...). 0)
# ' x ' is [[1 2 3],[4 5 6]]
Tf.transpose (x) ==> [[1 4], [2 5],[3 6]]
# equivalently
Tf.transpose (x, perm=[1, 0]) ==> [[1 4],[2 5], [3 6]]
Matrix related Operations
Inverse matrix of 1.tf.matrix_inverse matrices
Determinant of 2.tf.matrix_determinant matrices
3.tf.transpose Transpose
4.tf.diag the value on the given diagonal to return diagonally tensor
Tensor and numpy Array Mutual transfer
1.numpy Array to Tensor
Numpydata = Np.zeros ((1,10,10,3), Dtype=np.float32)
Tf.convert_to_tensor (Numpydata)
2.Tensor to NumPy array
Eval ()
Tf.constant ([1,2,3]). Eval ()
Reference documents
http://blog.csdn.net/lenbow/article/details/52152766