Keras is a high-level API for defining and training neural networks. In short, Keras is a further encapsulation of deep learning frameworks such as TensorFlow to provide a more elegant, user-friendly interface design. Therefore, Keras cannot run independently and requires the support of an underlying framework, such as TensorFlow, CNTK, or Theano. TensorFlow is recommended. This article uses TensorFlow as an example.
1. Prepare knowledge
1.1 What is a neural network?
In the field of artificial intelligence, the term “neural network” is often referred to as “artificial neural network”. It is a machine learning technology developed by humans to simulate the human nervous system with computer algorithms in order to make machines possess human intelligence behaviors. The human nervous system is a complex network of neurons connected to each other.
The picture below shows the structure of neurons in the brain, which receive inputs (impulses) from its multiple DENDRITES and process them to determine whether or not to send impulses through axons.
1.2 Perceptron
Just as neuron is the basic unit of brain nervous system, Perceptron is the basic unit of artificial neural network. It receives multiple inputs (), through linear functions and activation functions (Step Function is a kind of activation Function), get the output.
Linear function:
Among them, 和 As the vector,., so it can also be expanded as follows:
Step Function is the Step Function. In neural network, this kind of Function is usually called activation Function:
1.3 Neural networks and Linear Algebra
The mathematical principle of neural network is linear algebra. In a single perceptron, input to output is essentially the input vectorAnd weight vectorDot product of PI, plus a offset unit(scalar). For consistency of representation, it is also common to think of the offset unit as input 1 and weight 1A special unit of.
One perceptron corresponds to one output. If multiple perceptron with different weight vectors receive the same input vector, it corresponds to multiple outputs. In this way, a group of perceptron constitutes the layer of neural network. Compared with a single perceptron, layers can receive multiple inputs and get multiple outputs, and weights are no longer represented as vectors, but as matrices. So, the mathematical model of the layer is that the input vector is multiplied by the weight matrix to get the output vector.
1.4 Deep neural network
The so-called deep learning or deep neural network is a multi-layer perceptron composed of multiple sensory layers connected end to end, that is, the output of the former layer corresponds to the input of the latter layer. Among them, the first Layer is also called Input Layer, the last Layer is also called Output Layer, and the middle Layer is also called Hidden Layer.
2. Preparation
After having a basic concept of deep neural network, Keras is used to build a deep neural network model. To do this, you need to install the relevant software and Python packages.
If you haven’t installed Anaconda, it is recommended that the first installation, download address: www.anaconda.com/distributio… .
After Anaconda is installed, run the following command to create an environment and install python packages.
Conda create -n DNN python=3.6 conda activate DNN PIP install --upgrade PIP install numpy pandas matplotlib jupyter notebook tensorflow Keras pip install seaborn pip freeze | grep -Ei"numpy|pandas|matplotlib|tensorflow|Keras"
Copy the code
out:
Keras==2.3.0 keras-applications ==1.0.8 keras-preprocessing ==1.1.0 matplotlib==3.1.1 numpy==1.17.2 pandas==0.25.1 Tensorflow = = 1.14.0 tensorflow - estimator = = 1.14.0Copy the code
3. Use Keras to create a deep neural network model
Keras provides two methods for creating neural networks: a Sequential model and a Functional API.
The following two methods are used respectively to create the simplest 3-layer neural network model, with 1 input layer, 1 hidden layer and 1 output layer, where the input size (SHAPE) is 100, the output size is 10, and the hidden layer size is 32.
3.1 Sequential Model
from keras.models import Sequential
from keras.layers.core import Dense
Copy the code
Create a sequence model:
model = Sequential()
Copy the code
Add layers to the model. Keras will automatically infer the shape of all subsequent layers based on the first layer. This means that you only need to set the input dimension for level 1.
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(10, activation='softmax'))
Copy the code
Compile the model, specify the loss function, optimize the program and evaluate the finger.
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
Copy the code
View the model architecture:
model.summary()
Copy the code
Model: "sequential_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_1 (Dense) (None, 32) 3232 _________________________________________________________________ dense_2 (Dense) (None, 10) 330 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Total params: 3562 Trainable params: 3562 Non - trainable params: 0 _________________________________________________________________Copy the code
3.2 Functional API (Functional API)
The Keras functional API is used to define models with more complex structures. Examples are multi-output models, directed acyclic graphs, or models with shared layers.
from keras.layers import Input, Dense
from keras.models import Model
Copy the code
# return a tensor
inputs = Input(shape=(100)),# For the model layer instance, pass in the input tensor and return an output tensor
output_1 = Dense(32, activation='relu')(inputs)
outputs = Dense(10, activation='softmax')(output_1)
Create a model with one input layer, one hidden layer, and one output layer based on the inputs and outputs
model = Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
View the structure of the model
model.summary()
Copy the code
Model: "model_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) (None, 100) 0 _________________________________________________________________ dense_3 (Dense) (None, 32) 3232 _________________________________________________________________ dense_4 (Dense) (None, 10) 330 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Total params: 3562 Trainable params: 3562 Non - trainable params: 0 _________________________________________________________________Copy the code
conclusion
This article introduces the basic concepts and mathematical principles of deep neural networks from a beginner’s perspective, and then guides the reader through the transition from theory to practice by creating a model of the same structure using two methods provided by the very popular deep learning framework Keras. The theory is esoteric, but with a well-packaged framework, the hot technology of deep learning can be put into practice in the shortest time. Hopefully this article will help readers who are interested in deep learning take their first steps into the palace of deep learning.
Wechat scan qr code to obtain the latest technology original