Tensorflow2.0 simple application 2

Custom Model
4.3 custom layer
by tf.keras.layers.Layer subclass and implement the following methods to create custom layer:

build: the right to re-create layers. Adding weight to use add_weight method.

call: the spread of previously defined.

compute_output_shape: Specifies the output given the shape of how to calculate the shape of the input layer. Alternatively, the sequence may be achieved by a layer from_config get_config methods and class methods.

class MyLayer(layers.Layer):
    def __init__(self, output_dim, **kwargs):
        self.output_dim = output_dim
        super(MyLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        shape = tf.TensorShape((input_shape[1], self.output_dim))
        self.kernel = self.add_weight(name='kernel1', shape=shape,
                                   initializer='uniform', trainable=True)
        super(MyLayer, self).build(input_shape)

    def call(self, inputs):
        return tf.matmul(inputs, self.kernel)

    def compute_output_shape(self, input_shape):
        shape = tf.TensorShape(input_shape).as_list()
        shape[-1] = self.output_dim
        return tf.TensorShape(shape)

    def get_config(self):
        base_config = super(MyLayer, self).get_config()
        base_config['output_dim'] = self.output_dim
        return base_config

    @classmethod
    def from_config(cls, config):
        return cls(**config)

model = tf.keras.Sequential(
[
    MyLayer(10),
    layers.Activation('softmax')
])

model.compile(optimizer=tf.keras.optimizers.RMSprop(0.001),
             loss=tf.keras.losses.categorical_crossentropy,
             metrics=['accuracy'])

4.3 Callback

callbacks = [
    tf.keras.callbacks.EarlyStopping(patience=2, monitor='val_loss'),
    tf.keras.callbacks.TensorBoard(log_dir='./logs')
]
model.fit(train_x, train_y, batch_size=16, epochs=5,
         callbacks=callbacks, validation_data=(val_x, val_y))

Here is the prototype compile function

def compile(optimizer,
            loss=None,
             metrics=None,
             loss_weights=None,
             sample_weight_mode=None,
             weighted_metrics=None,
             target_tensors=None,
              **kwargs):

optimizer: The optimizer

loss:. loss function, may be carrying, if the model may have multiple custom output, or loss can pass a list of dictionary, the model will reduce these losses are added together

metrics: evaluation function, and loss of function is similar, but the results of the evaluation function is not used in the training process, you can pass the existing evaluation function name, or pass theano / tensorflow a custom function to use, built-in evaluation functions are: binary_accuracy (y_true, y_pred), categorical_accuracy (y_true, y_pred), sparse_categorical_accuracy (y_true, y_pred), top_k_categorical_accuracy (y_true, y_pred, k = 5) to customize the evaluation function should compile passed in at compile time, the function. in need (y_true, y_pred) as input parameters, and returns as output a tensor.

loss_weights: optional, it is a list or dictionary, specify a different coefficient of loss

model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
loss=‘categorical_crossentropy’,
metrics=[‘accuracy’])

model.save_weights(’./weights/model’)
model.load_weights(’./weights/model’)
model.save_weights(’./model.h5’)
model.load_weights(’./model.h5’)



```python
fit( x, y, batch_size=32, epochs=10, verbose=1, callbacks=None,
validation_split=0.0, validation_data=None, shuffle=True, 
class_weight=None, sample_weight=None, initial_epoch=0)

x: the input data. If the model has only one input, the type of x is numpy
Array, if the model has a plurality of inputs, the type of x should be list, list of elements corresponding to the respective input Array numpy
Y: labels, numpy Array
the batch_size: integer, Specifies the number of samples of each batch gradient descent when included. When training a batch of samples will be calculated once the gradient descent, the objective function optimization step.
epochs: integer, epoch value when the training is terminated, the training will stop when it reaches the epoch value, when there is no set initial_epoch, it is the total number of rounds of training, or the total number of rounds of training for epochs - inital_epoch
verbose: log shows, 0 is not on the standard output stream output log information, a recording progress bar output, two for each epoch output a row
callbacks: list, wherein the elements are keras.callbacks.Callback object. The callback function will list an appropriate time in the training process is called, with reference to the callback function
validation_split: floating point number between 0 and 1 to specify a certain proportion of the training data set as the validation set. Validation set will not participate in training, indicators and test the model at the end of each epoch, such as loss of function, accuracy and so on. Note, validation_split division before the shuffle, so if your data itself is in order, you need to manually re-designated upset validation_split, this may appear uneven validation set of samples.
validation_data: the form (X, y) of the tuple, is specified in the validation set. This parameter overrides validation_spilt.
shuffle: Boolean or string, typically a Boolean value indicating whether the random scrambled input samples in the training process. When a string "batch", is a special case HDF5 data for processing, it will disrupt the data within the batch.
class_weight: dictionary, the different classes are mapped to different weights, the parameter is used during training to adjust the loss function (can only be used for training)

sample_weight: numpy weights
array, adjusted for the loss of function in the training (for training only). 1D may be a transfer matrix with a vector length of the sample weight and the like for samples 1 to 1, or in the face of time series data is transmitted in the form of a (samples, sequence_length) to each time step of the sample assigned different weights. In this case sure added sample_weight_mode = 'temporal' during compilation model.

initial_epoch:, useful to start training from the epoch of the parameters specified in the training before continuing.
History fit function returns an object, which is the case where the attribute record History.history loss function values and other indicators with the epoch change, then if the validation set, also includes a validation set indicators changes

5.3 save the entire model

model = tf.keras.Sequential([
  layers.Dense(10, activation='softmax', input_shape=(72,)),
  layers.Dense(10, activation='softmax')
])
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(train_x, train_y, batch_size=32, epochs=5)
model.save('all_model.h5')
model = tf.keras.models.load_model('all_model.h5')
Released two original articles · won praise 0 · Views 71

Guess you like

Origin blog.csdn.net/dpoerpoiwetiop/article/details/104347421