preface

I still remember a week ago, I had some idea of graphic verification code, and I was confident to do the work of cracking it, but I was actually hit by all kinds of faces. I didn’t touch this line, and I really didn’t understand the routines inside.

The opening

If you don’t say the words deep learning, artificial intelligence, machine learning, neural network, etc., you may be a fake Internet person. But I don’t know if there are any students like JB.





literacy

Deep learning, artificial intelligence, machine learning, neural network, these four words come up a lot, but what is the relationship between them? In particular, how is machine learning different from deep learning?

Artificial Intelligence, English Artificial Intelligence, referred to as AI, what is the purpose of Artificial Intelligence? A lot on the net, pleasant to hear is called liberation/development productivity, liberation of mankind, in general is to improve efficiency but better understanding is: lazy!

For example, when you speak a sentence, the machine can recognize the text, and understand the meaning of your words, analysis and dialogue.

The core of artificial intelligence lies in the word intelligence, how does that intelligence come? Largely thanks to an approach to artificial intelligence called machine learning;

What are the current application scenarios of ARTIFICIAL intelligence: OCR, voice technology (such as Siri), big data applications, etc

Machine learning, in its most basic form, uses algorithms to parse data, learn from it, and make decisions and predictions about events. It takes a lot of data to “train”, and algorithms to learn how to do things from that data.

For example, when browsing online shopping malls, there is often a product recommendation information. This is where the mall, based on past purchases and lengthy collection lists, identifies which of these products are truly interesting and willing to buy. Such a decision-making model can help the mall offer suggestions to customers and encourage product consumption.

There are three types of machine learning:

The first is unsupervised learning, which automatically looks for patterns from information and categorizes them into categories, sometimes called “clustering problems.”

The second category is supervised learning, which involves putting a label on history and using models to predict outcomes. If we have a fruit and we decide whether it is a banana or an apple based on its shape and color, this is an example of supervised learning.

The last type is reinforcement learning, which refers to a learning method that can be used to support people to make decisions and plans. It is a feedback mechanism that generates rewards for some actions and behaviors of people and promotes learning through this feedback mechanism, which is similar to human learning. Therefore, reinforcement learning is one of the important directions of current research.

Deep learning is a sub-field of machine learning, which uses deep neural networks to make models more complex, so that models can understand data more deeply.

The core of deep learning is that we now have computers fast enough and enough data to actually train large neural networks

The difference and connection of the three

Machine learning is a method to achieve artificial intelligence, and deep learning is a technology to achieve machine learning.

According to JB, deep learning may eventually permeate all other machine learning algorithms;

Of course, it does not mean that deep learning is a panacea, nor is it necessarily superior to machine learning.

Artificial neural network: a machine learning algorithm in order to “Stop sign” (Stop), for example, will be a Stop sign image all the elements of broken, then “check” by neurons, octagonal shape, fire engine red color, highlighted the letters of the typical size, traffic signs and stationary movement characteristics and so on. The neural network’s job is to decide whether or not it’s a stop sign. The neural network would come up with a considered guess — a “probability vector” — based on the weight of ownership.

Neural network needs to be modulated and trained, otherwise it is easy to make mistakes

Ok, at this point, I feel that I have some understanding of this information ~ at least I know what these things are; To summarize briefly:

Machine learning is a method to realize artificial intelligence, deep learning is a technology to realize machine learning, and neural network is an algorithm to realize machine learning

TensorFlow profile

TensorFlow is the address of Github, an artificial intelligence system that Google opened in November 2015. It can be used for speech recognition, image recognition, and more.

TensorFlow is an open source software library that uses data flow graph technology to perform numerical calculations.

  • Nodes in the data flow graph, representing numerical operations;
  • Nodes The edges between nodes, representing some relationship between multidimensional data (tensors).
  • The functionality of the system can be used with simple API calls on a variety of devices, including cpus or Gpus.

TensorFlow is a project led by developers on the Google Brain team.

What is a Data Flow graph a data flow graph describes the numerical calculation process in a directed graph. Nodes in directed graphs usually represent mathematical operations, but they can also represent data input, output, read and write operations. Edges in a directed graph, which represent some relationship between nodes, are responsible for transmitting multidimensional data (Tensors).

Nodes can be assigned to multiple computing devices and can perform operations asynchronously and in parallel. Because it is a directed graph, the current node can only perform the operation after the calculation state of the previous entry nodes is completed.

The characteristics of TensorFlow

  • Flexibility, TensorFlow is not a strict neural network toolkit, you can do anything with TensorFlow as long as you can use data flow diagrams to describe your computation process. You can also easily build data flow diagrams on demand, using the simple Python language for high-level functionality.
  • Portability, TensorFlow can run on any device with a CPU or GPU, you can focus on implementing your idea regardless of the hardware environment, and you can even use Docker technology to implement related cloud services.
  • TensorFlow makes it easy to productize your work and share code with your peers – it currently supports Python and C++.
  • Make full use of hardware resources to maximize computing performance

TensorFlow installation

JB chose to use PyCharm to install TensorFlow.

Pycharm is a Python IDE that is easy to install and write code for.

Open PyCharm, in the menu bar, search for Project Inter in Lie-Settings, select the compiler you are using, and hit + :

Type tensorflow, install package, and wait for the long installation process;

Of course, if you like the difficulty, you can check out github’s website. I wish you good luck ~ https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/get_started/os_setup.md

Import tensorFlow directly and run it to see if there is any problem. If there is a problem, an error will be reported

The first example

Here’s an example from the official website.

Import tensorflow as tf hello = tf.constant(" hello, tensorflow ") # TensorFlow sess = tf.session () # define a Session print(sess.run(hello)) # execute graph calculate a = tf.constant(10) b = tf.constant(32) # create graph Constant()ops print(sess.run(a+b)) #Copy the code

Running results:

The 2018-06-14 18:48:55. 967672: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 b'Hello,TensorFlow' 42Copy the code

The first line above is still red, although it doesn’t affect the output, but it doesn’t look good. How to do? Direct shielding is good ~

import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]='2'
Copy the code

Import OS, add the above sentence; What do you mean you don’t understand? Come on ~ let

Os. environ["TF_CPP_MIN_LOG_LEVEL"]='1' # This is the default display level, Os. environ["TF_CPP_MIN_LOG_LEVEL"]='2' # Displays only warning and ErrorCopy the code

Look at the above example, can not see the special, first to understand the basic knowledge ~

The basic use

You need to understand how in TensorFlow:

  • The calculation process is represented as a graph;
  • Perform graph calculations with Sessions;
  • The data were expressed as Tensors;
  • Use Variables to maintain state information;
  • Use feeds and fetches, respectively, to populate data and capture arbitrary operation results;

Basic concepts of TensorFlow

  • Graph, “Graph,” is used to represent a computational task, which is the operation we’re going to do.
  • Session: A Session is established, and an empty graph is generated. Nodes and edges are added to the session to form a graph. A session can have multiple graphs, and the results are obtained by executing these graphs. If you think of each graph as a lathe, the session is a workshop with several lathes that produce data into results.
  • Tensor means data, your stuff.
  • Variables: Used to record some data and state, is our container.
  • Feed and FETCH: You can assign values to or retrieve data from arbitrary operations. It’s like a shovel that can manipulate data.

The metaphor is: think of conversations as workshops, diagrams as lathes, with Tensor as raw materials, variables as containers, feeds and fetch as shovels, processing data into our results.

The creation of a graph in TensorFlow is similar to a construction phase, while a session is used in the execution phase to execute the nodes in the graph. It is very common to create a diagram to represent and train the neural network during the construction phase, and to repeat a series of training operations within the diagram during the execution phase.

Create diagrams and running diagrams

In the first example, there is an illustration of how the diagram is created. Here is another example:

Matrix1 = tf.constant([[3.,3.]]) matrix2 = tf.constant([[2.],[2.]]) product = tf.matmul(matrix1,matrix2) # Sess = tf.session () # define a Session result = sess. Run (product) # multiply, Print (result) sess.close() #Copy the code

If a GPU is required, perform the following operations:

with tf.Session() as sess: with tf.device("/cpu:0"): Matrix1 = tf.constant([[3,3]]) # matrix2 = tf.constant([[2],[2]]) # matrix1 = tf.matmul(matrix1,matrix2) Sess = tf.session () # define a Session result = sess. Run (product) Print (result) sess.close() #Copy the code

The meanings of the strings in device are as follows:

  • “/ CPU :0”: the CPU of your machine;
  • “/ GPU :0”: your machine’s first GPU;
  • “/ GPU :1”: the second GPU on your machine;

Tf. constant(value, dtype=None, shape=None, name=’Const’, verify_shape=False),value = value, dtype, shape= tensor shape, Name Name and verify_shape are optional. Creates a constant.

A = tf.constant(2, name="a") # print(a) = 2 b = tf.constant(2.0, dType =tf.float32, Shape =[2,2], name="b") # 2x2 matrix, Value of 2 c = tf. Constant ([[1, 2], [3, 4]], name = "c") # 2 x2 matrix, the value of 1, 2, 3, 4Copy the code

Are you confused? Nothing, leave a general impact on good ~

Second example — creating a variable

Num = tf.variable (0,name = "count") num new_value = tf.add(num,10) Assign (num,new_value) # assign new_value to num with tf.session () as sess: Sess.run (tf.global_variables_initializer()) # Print (sess.run(num)) for I in range(5): Print (sess.run(num)) print(sess.run(num)) print(sess.run(num)Copy the code

The third example is populating

Sometimes a feed comes in handy when a variable is declared without an assignment and evaluated instead

Input1 = tf.placeholder(tf.float32) # create a placeholder input2 = tF.placeholder (tF.float32) # create a placeholder input2 mul = Multiply (input1, input2) # multiply with tf.session () as sess: Result = sess.run([mul], feed_dict={input1:[7.], input2:[2.]})Copy the code

Comprehensive example

import tensorflow as tf import os os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' import numpy as np x_data = Np.random.rand (100).astype("float32") y_data = x_data * 0.1 + 0.3 The corresponding function is y = x * 0.1 + 0.3 # numpy W = tf.Variable(tf.random_uniform([1], -1.0, 1)) b = tf.variable (tf.zeros([1])) # Generate a tensor of size 1 y = W * x_data + b # specify the range of values of W and b (note that we are using TensorFlow to get values of W and b) # Tf. random_uniform, TensorFlow random value function, returns 1 matrix, Value generated between -1.0 and 1.0 Loss = tf.reduce_mean(tf.square(y-y_data)) #reduce_mean = square Tf. Train. GradientDescentOptimizer # (0.5) this class is to realize the optimizer of gradient descent algorithm, The first parameter is the learning rate to be used train = optimize.minimize (loss) # mean square error init = tf.global_variables_initializer() # initialize the TensorFlow parameter sess Sess.run (train) if step % 20 == 0: = tf.session () sess.run(init) # for step in range(201): sess.run(train) if step % 20 == 0 Print (step, sess.run(W), sess.run(b)) print(step, sess.run(W), sess.run(b)Copy the code

Output results:

0 [-0.39065683] [0.66207296]
20 [-0.0082044] [0.3498935]
40 [0.07841039] [0.30995506]
60 [0.09569232] [0.3019863]
80 [0.09914052] [0.30039632]
100 [0.09982852] [0.30007908]
120 [0.09996579] [0.30001578]
140 [0.09999318] [0.30000314]
160 [0.09999864] [0.30000064]
180 [0.09999974] [0.30000013]
200 [0.09999995] [0.30000004]
Copy the code

Output data every 20 times. As can be seen from the above results, with the increase of times, the values of W and B are closer and closer to 0.1 and 0.3. JB wonders, why is this happening? From the above code, nothing to do? How about this one? TensorFlow: TensorFlow: TensorFlow: TensorFlow: TensorFlow

This is the end of this chapter, originally wanted to introduce Hello World, but considering the whole chapter will be long, put down the article ~

summary

TensorFlow: TensorFlow, TensorFlow, TensorFlow, TensorFlow, TensorFlow, TensorFlow, TensorFlow

Our ultimate goal is to use TensorFlow to train our model and improve the recognition rate of graphic verification codes.

Thank you.