TensorFlow Initial: The First Machine Learning Demo
This article mainly introduces the use of TensorFlow primary API through a simple Demo, because I am also a beginner, so the main purpose of this article is to guide students who have just contacted TensorFlow or machine learning to learn TensorFlow from the first step. Read this article to confirm that you have the following basic skills:
Can use Python programming (OK as a beginner, but TensorFlow also supports Java, C++, Go)
Some knowledge about arrays (linear algebra is all right if you don't forget it)
It's better to know more about machine learning (temporary Baidu, Google are also in time)
Basic knowledge
Tensor
TensorFlow's internal calculations are based on tensors, so it is necessary to have an understanding of tensors first. Tensors are defined on scalars and vectors that we are familiar with. Detailed definitions are rather complicated. We can first simply understand them as a multi-dimensional array:
3# This zero-order tensor is a scalar, shape=[]
[1., 2., 3.] This first-order tensor is a vector, shape= [3]
[[1., 2., 3.], [4., 5., 6.]] # This second-order tensor is a two-dimensional array, shape= [2, 3]
[[[1., 2., 3.], [[7., 8., 9.]]] This third-order tensor is a three-dimensional array, shape= [2, 1, 3]
One
Two
Three
Four
Within TensorFlow, an instance of the tf.Tensor class is used to represent tensors, and each tf.Tensor has two attributes:
The type of data stored in dtype Tensor can be tf. float32, tf. int32, tf. string...
The number of elements in each dimension of the multidimensional array stored by shape Tensor, such as the shape in the example above
We can now tap a few lines of code to see Tensor. Enter Python or Python 3 at the command terminal to start a Python session, and then enter the following code:
# Introducing tensorflow module
Import tensorflow as TF
# Create an integer constant, Tensor 0
T0 = tf. constant (3, dtype = tf. int32)
# Create a one-dimensional array of floating-point numbers, the first-order Tensor
T1 = tf. constant ([3., 4.1, 5.2], dtype = tf. float32)
# Create a 2x2 array of strings, the 2nd order Tensor
T2 = tf. constant (['Apple','Orange'], ['Potato','Tomato']), dtype = tf. string)
# Create a 2x3x1 array, the third-order tensor, with the default data type of integer
T3 = tf.constant ([[[5], [6], [7], [[4], [3], [2]])
# Print several Tensors created above
Print (t0)
Print (t1)
Print (t2)
Print (t3)
One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Eleven
Twelve
Thirteen
Fourteen
Fifteen
Sixteen
Seventeen
Eighteen
Nineteen
Twenty
The output of the above code is, note the type of shape:
> Print (t0)
Tensor ("Const: 0", shape =(), dtype = int32)
> Print (t1)
Tensor ("Const_1:0", shape= (3,), dtype = float32)
> Print (t2)
Tensor ("Const_2:0", shape= (2,2), dtype = string)
> Print (t3)
Tensor ("Const_3:0", shape = (2, 3, 1), dtype = int32)
One
Two
Three
Four
Five
Six
Seven
Eight
Print a Tensor can only print out its attribute definition, not its value. To view the value in a Tensor, you need to run through Session:
> sess = tf. Session ()
> Print (sess. run (t0))
Three
> Print (sess. run (t1))
[3.4.0999999 5.19999981]
> Print (sess. run (t2))
[[b'Apple'b'Orange']
[b'Potato'b'Tomato']]
> Print (sess. run (t3))
[[[5]
[6]
[7]]
[[4]
[3]
[2]]]
>
One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Eleven
Twelve
Thirteen
Fourteen
Fifteen
Sixteen
Seventeen
Data flow Graph
Data flow is a common parallel computing programming model. Data flow graph is a directed graph composed of nodes and edges.
Nodes represent computing units and can also be the starting point of input or the end point of output.
Lines represent input/output relationships between nodes
In TensorFlow, each node is represented by an example of tf. Tensor, that is, the input and output of each node are Tensor. In the following figure, the flow of Tensor in Graph shows the origin of the name of TensorFlow visually.
Data flow diagrams in TensorFlow have the following advantages:
Parallel computing nodes are connected by clear lines. The system can easily determine which computing operations can be executed in parallel.
The nodes in the distributable graph can be distributed in different computing units (CPU, GPU, TPU, etc.) or different machines, and the data generated by each node can be sent through a clear line to the next node.
Optimizable XLA compiler in TensorFlow can optimize code according to data flow graph to speed up operation.
Portable data flow graph information can be saved independently of code, such as a graph created with Python, which can be saved and used in C++ or Java.
Sesssion
When we need to do some computational operations in Python, we usually use NumPy. NumPy will use other languages (C/C++) to implement these computational logic when doing complex computations such as matrix operations, so as to ensure the efficiency of calculation. However, frequent switching between multiple programming languages can also be time-consuming, which may be neglected if only a single machine operates. But if in distributed parallel computing, computing operations may be distributed in different CPUs, GPUs or even different machines, these time-consuming may be more serious.
The bottom layer of TensorFlow is implemented in C++, which guarantees computational efficiency and uses the tf. Session class to connect customers.
Please read the Chinese version for details.