Preface
It has been a long time for us to study in depth. We have learned a lot of models and theoretical knowledge. Recently, I want to learn Keras. I also read a few keras codes themselves. It feels simple and clear. So I want to learn about keras. Personal thoughts. While studying, I want to take notes. First, it's convenient for me to read later. SEcond, it's an honor for me to record my personal learning process. Third, it's an honor for me to help someone see my blog and help you. My learning materials come from Keras Chinese documents, links: https://keras.io/zh/ . If you are interested, you can directly visit the official website to learn, it must be better than I wrote.
Introduction of Keras
Keras is an advanced neural network API written in Python, which can run as a back end with TensorFlow, CNTK, or Theano. Keras focuses on supporting fast experiments. To be able to translate ideas into experimental results with minimal delay is the key to doing a good job of research. Keras allows you to design models simply and quickly, while supporting convolutional and cyclic neural networks, which can run seamlessly on GPU and CPU.
Keras'Guiding Principles:
- User friendliness: keras is designed for human beings, putting user experience first and foremost. Provide a simple API to minimize the number of user actions required for common use cases, and provide clear and operational feedback when user errors occur.
- Modularization: A sequence or graph of independent, fully configurable modules. These modules can be assembled with as few constraints as possible. Especially, the neural network layer/loss function, optimizer, initialization method, activation function and regularization method can be combined to construct the module of the new model.
- Extensibility: New modules can be easily built.
- Python-based implementation: Keras does not have a single configuration file in a specific format. Defined in Python code, it is easy to debug and expand.
2. Using Keras Sequential Sequential Sequence Model
1. Linear stacking of multiple network layers
Method 1: Create a Sequential model by passing the list of network layer instances to the constructor of Sequential
from keras.models import Sequential from keras.layers import Dense, Activation model = Sequential([ Dense(32, input_shape=(784,)), # Fully connected layer Activation('relu'), # activation Dense(10), Activation('softmax'), ])
Method 2: Use. add() method to add layers to the model
model = Sequential() model.add(Dense(32, input_dim=784)) model.add(Activation('relu'))
2. Specify the size of the input data
The model needs to know the expected input size, so in the first layer of the sequential model, and only the first layer needs to accept information about its input size. Other layers can automatically infer dimensions.
- Pass an input_shape parameter to the first layer. A tuple representing size, an integer or a non-e tuple, and a non may be any positive integer. The batch size does not contain any data in input_shape. If you want to specify a batch size, you can pass a batch_size parameter.
- For some 2D layers, the input size is specified by the parameter input_dim. Some 3D time series support input_dim and input_length parameters
# The following two are equivalent model = Sequential() model.add(Dense(32, input_shape(784,))) model = Sequential() model.add(Dense(32, input_dim=784))
3. Model Compilation
Before training the model, it is necessary to configure the learning process, that is, to define the loss function and other information. Complete by compile method. Accept three parameters:
- Optimizer optimizer: Can be a string identifier for an existing optimizer, such as rmsprop
- Loss function loss: The objective function of model minimization, such as categerical_crossentropy, can also be an objective function.
- Evaluation criteria metrics: For any classification problem, the hope is accuracy, you can customize the evaluation function.
# Multi-Classification Problem model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # Bi-classification problem model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) # Mean Square Error Regression Problem model.compile(optimizer='rmsprop', loss='mse') # Custom Evaluation Standard Function import keras.backend as K def mean_pred(y_true, y_pred): return K.mean(y_pred) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy', mean_pred])
4. Model training
Keras is trained on the Nupy matrix of input data and labels. In order to train the model, fit function is usually used.
# Single-input model with two classes model = Sequential() model.add(Dense(32, activation='relu', input_dim=100)) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) # Generate data import numpy as np data = np.random.random((1000, 100)) labels = np.random.randint(2, size=(1000, 1)) # Training model model.fit(data, labels, epochs=10, batch_size=32)
# Single Input Model with Ten Classes model = Sequential() model.add(Dense(32, activation='relu', input_dim=100)) model.add(Dense(10, activation='softmax')) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # Generate data import numpy as np data = np.random.random((1000, 100)) labels = np.random.randint(2, size=(1000, 1)) # Converting labels to one-hot coding for classification one_hot_labels = keras.utils.to_categorical(labels, num_classes=10) # train model.fit(data, one_hot_labels, epochs=10, batch_size=32)