Sequential model (Sequential) : Single input and single output, one path leads to the end, there is only adjacent relationship between layers, and there is no cross-layer connection. This model has fast compilation speed and relatively simple operation
model = Sequential()
The core operation of the Sequential model is to add layers. The following shows how to add some of the most popular layers to the model:
Convolutional layer
model.add(Conv2D(64, (3, 3), activation='relu')) #激活函数为relu
Maximum pooling layer
model.add(MaxPooling2D(pool_size=(2, 2)))
Fully connected layer
model.add(Dense(256, activation='relu'))
Dropout
Dropout refers to the process of deep learning training. For neural network training units, remove them from the network according to a certain probability. Note that it is temporary to reduce overfitting.
model.add(Dropout(0.5))
Flattening layer
model.add(Flatten())
When using the sequential model, the loss function and the optimizer must be defined.
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
For the built model to be trained, the fit() method is used at this time.
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
The batch size, the number of iterations, and the validation data set all need to be defined by ourselves. The values may be different for different networks. You must find the appropriate value according to your own model.
Finally, use the evaluate method to evaluate the model:
score = model.evaluate(x_train,y_train,batch_size = 32)
It will show the loss and accuracy of the training set and test set.
Finally, the trained model must be preserved to realize the sustainability of the model.
# 保存模型参数
model.save('model.h5')