Tensorflow-tensor Understanding and use
Flyfish
How to understand the tensor in TensorFlow
Tensor tensor
English [' tensə-sɔː] beauty [' Tɛnsɚ]
What is a Tensor?
Tensors are simply mathematical objects that can is used to describe
Physical properties, just like scalars and vectors. In fact tensors
are merely a generalisation of scalars and vectors; A scalar is a zero
Rank tensor, and a vector is a-a-rank tensor.
Tensor is a simple mathematical object that can be used to describe physical properties, just like scalar and vector. In fact, tensor is only a generalization of scalar and vector, scalar is 0-order tensor, and vector is first-order tensor. (Rank and order general)
The rank (or order) of a tensor was defined by the number of directions
(and hence the dimensionality of the array) required to describe it.
For example, properties so require One direction (A/I) can be
Fully described by a 3x1 column vector, and properties that require
Two directions (second rank tensors), can is described by 9 numbers,
As a 3x3 matrix. As such, in the general a nth rank tensor can be
Described by 3n coefficients.
The rank (or order) of a tensor is defined by the number of orientations (and the number of dimensions of the array). For example, a property that requires a direction (first order) can be fully described by a 3x1 column vector, the attribute requires two directions (second order tensor), can be 9 digits, described as a 3x3 matrix. Therefore, in general, the nth-order tensor can be described by the n-th exponent of 3.
The following quotes from the TensorFlow: actual combat Google Depth learning framework
Concept
From the name of TensorFlow can be seen that tensor (tensor) is a very heavy concept. All the data in the TensorFlow program is represented by the tensor form. From a functional point of view, the tensor can be understood as a multidimensional array. The 0-order tensor indicates a scalar (scalar) is a number, the first-order tensor is a vector, that is, a one-dimensional array; n-order tensor can be interpreted as an n-dimensional array. But the implementation of tensor is not directly in the form of an array, it is only a reference to the result of the operation in TensorFlow. The number is not saved in the tensor, and it holds the computational process of how to get these numbers.
Vector Addition Code Description
Import TensorFlow as TF
a=tf.constant ([1.0,2.0],name= ' a ')
b=tf.constant ([2.0,3.0],name= ' B ')
result= Tf.add (a,b,name= ' Add ') print (Result
output Tensor ("add:0", shape= (2,), Dtype=float32)
A tensor mainly holds three properties: Name, Dimension (shape), and type.
The name of tensor can be given in the form of "Node:str_output". Where node is the name of nodes, Str_output represents the current tensor from the first few outputs of the node. For example, the "add:0" above the code shows that result this tensor is the first outcome of the compute node "add" output (numbering starts from zero).
The second property of the tensor is the dimension (shape) of the tensor. This property describes the dimension information for a tensor. The third property of the tensor is type, and each tensor has a unique type.
Use
The use of tensor is divided into two categories. 1 A reference to the intermediate calculation result. When a calculation contains a lot of calculation results, the use of tensor can greatly improve the readability of the Code
Code comparison
#使用张量记录中间结果
a=tf.constant ([1.0,2.0],name= ' a ')
b=tf.constant ([2.0,3.0],name= ' B ')
result=a+b
# The direct knot computes the result=tf.constant of the vector and may be poor
([1.0,2.0],name= ' a ') + tf.constant ([2.0,3.0],name= ' B ')
2 When the graph structure is completed, the tensor can get the result of calculation, that is to get the real number. Although the tensor itself does not store specific numbers, these specific numbers can be obtained through session sessions.
Some examples of C + + using tensor
The source directory for class tensor is
\tensorflow\core\framework\tensor.h
Python inside the Tensor is NumPy ndarry,c++ inside uses the third party library Eigen Library's encapsulation-class Tensor
。
Code in
Eigen\unsupported\eigen\cxx11\src\tensor
There is no official support for the unsupported part of the Eigen library.
CHECK fails indicates that an error has been found at compile time
typedef float T;
Tensor T1 (Dt_float, Tensorshape ({}));
Tensor My_mat (Dt_float, Tensorshape ({3, 5})); Built with Shape{rows:3, cols:5} auto MAT1 = My_mat.matrix<t> ();
2d Eigen::tensor, 3 x 5. Auto MAT2 = my_mat.tensor<t, 2> ();
2d Eigen::tensor, 3 x 5.
Auto MAT3 = my_mat.matrix<int32> ()//CHECK fails as type mismatch. Auto VEC1 = my_mat.vec<t> ();
CHECK fails as My_mat is 2D. Auto VEC2 = my_mat.tensor<t, 3> ();
CHECK fails as My_mat is 2D.
Tensor My_ten (... built with Shape{planes:4, Rows:3, cols:5} ...)
Tensor My_ten (Dt_float, Tensorshape ({4, 3, 5}));
1D eigen::tensor, size 60:auto flat = my_ten.flat<t> ();
2d eigen::tensor x 5:auto inner = my_ten.flat_inner_dims<t> ();
2d eigen::tensor 4 x 15:auto outer1 = my_ten.shaped<t, 2> ({4, 15}); CHECK fails, bad num elements://auto outer2 = my_ten.shaped<t, 2>({4, 8});
3D eigen::tensor 6 x 5 x 2:auto weird = my_ten.shaped<t, 3> ({6, 5, 2}); CHECK fails, type mismatch://auto bad = my_ten.flat<int32> ();