With keras frame image classification model ---- training Study Notes

Learning opencv parking spaces recognition, neural network model used in training, use keras framework.
Write down one study notes.
Chinese documents keras
keras official document summarizes several guiding principles keras framework:

  1. friendly user
  2. Modular (build in our own time neural networks, in particular neural network layer, loss of function, optimizer, initialization method, the activation function, regularization method, which are the building blocks can be combined with the new model.)
  3. Easy to expand
  4. Python implemented based
    core data structure is keras Model , the simplest is the order of the Sequential model ,
    for more complex structures, there Keras functional the API , which allows the construction of any neural network of FIG.

From simple sequential structure Sequential start:

Sequential model is a linear stack layers

Sequence structure giving a feeling that a line down, all the modules required down list.

from keras.models import Sequential
model = Sequential()

First, the input 1 of the shape of the shape

Determining a shape data input through the first layer model Sequential model, only we need to define in the first layer, since the other layers
can be automatically inferred shape.

Then on the use of .add()stacked model.
Dense in the meaning of the parameters of the
meaning of the parameters

from keras.layers import Dense
# units表示该层输出的维度
# activation表示激活函数
# input_dim表示张量的维度
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))

1. input_shape parameter transmitted to the first layer. This is a tuple, None represents any integer can be input. This parameter does not contain the size of the batch.
2. For the two-dimensional input with input_dimto develop the dimension input.
3. For three-dimensional input, the parameters input_dimand input_length
4. To the fixed size of the batch, then use the parameter batch_size, if both transfer batch_size=32and delivery input_shape=(6, 8), the shape of each batch are inputted (32, 6, 8)

2 compilation

Specifies the shape data before training models need to compileconfigure the learning process.
After you've built the model, we use .compileto configure the learning process.

from keras import optimizers
# loss表示损失函数
# optimizer表示优化器选择随机梯度下降
# metrics表示评价指标,为准确率
model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

All keras optimizer common parameters
lr: learning rate
clipnorm: gradient cut

mean_squared_error (mean square error)

keras.losses.mean_squared_error(y_true, y_pred)

mean_absolute_error (mean absolute error)

keras.losses.mean_absolute_error(y_true, y_pred)

mean_absolute_percentage_error (mean absolute percentage error)

keras.losses.mean_absolute_percentage_error(y_true, y_pred)

binary_crossentropy (the Mutual entropy)

keras.losses.binary_crossentropy(y_true, y_pred, from_logits=False, label_smoothing=0)

Loss function is a model attempts to minimize the objective
of which:
y_true: tensor real targets.
y_pred: the predicted target tensor.

binary_accuracy

keras.metrics.binary_accuracy(y_true, y_pred, threshold=0.5)

categorical_accuracy

keras.metrics.categorical_accuracy(y_true, y_pred)

sparse_categorical_accuracy

keras.metrics.sparse_categorical_accuracy(y_true, y_pred)

3 start training model

Keras model on training in Numpy array of input data and labels. Use fittrained models.
Official Documents

# x_train 和 y_train 是 Numpy 数组 -- 就像在 Scikit-Learn API 中一样。
model.fit(x_train, y_train, epochs=5, batch_size=32)

Each training epoch fixed
iterates over datasets

fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_freq=1, max_queue_size=10, workers=1, use_multiprocessing=False)

Parameter Description:
xindicates the input data,

1.Numpy array (or array-like array) array or list (if the model having a plurality of inputs).
2. If the model named input, then enter the name dict will be mapped to the corresponding array / tensor.
3. keras.utils.Sequence generator or return (inputs, targets), or (inputs, targets, sample weights) .
4. If the input is returned from the tensor itself (e.g. TensorFlow tensor data) frame, compared None (default).

yIt represents the target data
batch_sizerepresents the number of samples per update gradient, usually to an integer or None. If not specified, batch_size the default is 32.
epochsRepresentation is divided into several batches to train the model, set to an integer. Each lot iterative data.
verboseVisually represents the training process, 0,1,2, wherein 1 represents a progress bar, each training batch 2 represents one row.

4 return record

History.history properties with consecutive recording training epoch loss values ​​and metrics, and verification and validation of the value of loss metric.

5 final assessment model

evaluate(x=None, y=None, batch_size=None, verbose=1, sample_weight=None, steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False)

Return loss value of the test pattern models (Loss), and index values (metrics).
Interpretation of test parameters official documents

Published 10 original articles · won praise 0 · Views 212

Guess you like

Origin blog.csdn.net/weixin_43227526/article/details/105160772