Keras official Chinese document: functional model API

Time:2021-3-5

Functional model interface

Keras’s functional model isModelThat is, the generalized model with input and output, we useModelTo initialize a functional model

from keras.models import Model
from keras.layers import Input, Dense
 
a = Input(shape=(32,))
b = Dense(32)(a)
model = Model(inputs=a, outputs=b)

Here, our model usesaFor input, tobFor output, we can also construct models with multiple inputs and multiple outputs

model = Model(inputs=[a1, a2], outputs=[b1, b2, b3])

Common model properties

  • model.layers: the layers that make up the model diagram
  • model.inputs: list of input tensors for the model
  • model.outputs: list of output tensors of the model

Model method

compile

compile(self, optimizer, loss, metrics=None, loss_weights=None, sample_weight_mode=None, weighted_metrics=None, target_tensors=None)

  

This function compiles the model for training, and the parameters are

  • Optimizer: optimizer, predefined optimizer name or optimizer object, refer tooptimizer

  • Loss: loss function. It is a predefined loss function name or an objective function. Refer toloss function

  • Metrics: a list of metrics used to evaluate the performance of a model in training and testingmetrics=['accuracy']If you want to specify different indicators for different outputs in the multi output model, you can pass a dictionary like this parameter, such asmetrics={'ouput_a': 'accuracy'}

  • sample_ weight_ Mode: if you need to weight samples in time steps (2D weight matrix), set the value to “temporary”. The default value is “None”, which means weighting by sample (1D weight). If the model has multiple outputs, you can pass in the specified sample to this parameter_ weight_ Dictionary or list of mode. hereunderfitThere are some references in the explanation of function.

  • weighted_ Metrics: a list of metrics that will be used by thesample_weightorclss_weightCalculate and empower

  • target_ Tensors: by default, keras will create a placeholder for the target of the model, which will be replaced by the target data during training. If you want to use your own target tensors (accordingly, keras will not want to load external numpy data for these target tensors during training), you can specify them manually through this parameter. The target tensor can be a single tensor (corresponding to the single output model), a tensor list, or a tensor dictionary with name > tensor.

  • Kwargs: if tensorflow is used as the back end, please ignore this parameter. If theano / cntk is used as the back end, the value of kwargs will be passed to k.function. If tensorflow is used as the back end, the value here will be passed to the tf.Session.run

An exception is thrown when an illegal value is passed in for a parameter

Tips if you just load the model and use its predict, you don’t need to compile it. In keras, compile mainly completes some configuration of loss function and optimizer for training. Predict compiles symbolic functions internally (by calling_ make_ predict_ Function generating function),

 fit

fit(self, 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)

  

This function is used to train the model

  • x: Input data. If the model has only one input, then the type of X is numpy array. If the model has multiple inputs, then the type of X should be list, and the elements of list are numpy array corresponding to each input. If each input of the model has a name, you can pass in a dictionary to correspond the input name with its input data.

  • y: Label, numpy array. If the model has multiple outputs, a list of numpy array can be passed in. If the output of the model has a name, you can pass in a dictionary to correspond the output name with its label.

  • batch_ Size: an integer that specifies the number of samples contained in each batch during gradient descent. During the training, a batch sample will be calculated once gradient descent to optimize the objective function.

  • Epochs: integer, epoch value at the end of training. Training will stop when the epoch value is reached. When initial is not set_ When epoch, it is the total number of training rounds, otherwise the total number of training rounds is epochs – initial_ epoch

  • Verb: log display: 0 is not in the standard output stream to output log information, 1 is to output progress bar record, and 2 is to output one line record for each epoch

  • Callbacks: list, where the element iskeras.callbacks.CallbackThe object of the. The callback function in this list will be called at an appropriate time in the training process. Please refer toCallback function

  • validation_ Split: a floating-point number between 0 and 1, which is used to specify a certain proportion of the data in the training set as the verification set. The verification set will not participate in the training and will test the indicators of the model after each epoch, such as loss function, accuracy, etc. Attention, validation_ Split is divided after shuffle, so if your data itself is in order, you need to manually scramble and then specify validation_ Otherwise, the sample of verification set may be uneven.

  • validation_ Data: in the form of (x, y) or (x, y, sample)_ Weights) is the specified verification set. This parameter will override validation_ spilt。

  • Shuffle: Boolean value, indicating whether the order of input samples is randomly scrambled before each epoch in the training process.

  • class_ Weight: the dictionary maps different classes to different weights, which are used to adjust the loss function during training (only for training). This parameter can make the loss function pay more attention to the data with insufficient samples when dealing with unbalanced training data.

  • sample_ Weight: numpy array of weights, used to adjust the loss function during training (only for training). A 1D vector with the same length as the sample can be passed to weight the sample 1-to-1, or in the face of time series data, a vector in the form of (samples, sequence) can be passed_ Length) matrix to assign different weights to the samples in each time step. In this case, be sure to add thesample_weight_mode='temporal'

  • initial_ Epoch: training starts from the epoch specified by this parameter, which is useful when continuing the previous training.

  • steps_ per_ Epoch: the number of steps contained in an epoch (each step is the data input of a batch). When training with input tensors such as tensorflow data tensor, the default none represents automatic segmentation, that is, the number of dataset samples / the number of batch samples.

  • validation_ Steps: only if steps_ per_ Useful when epoch is specified, the total number of steps on the validation set.

An error is thrown when the input data does not match the specified data

fitFunction returns aHistoryThe object of theHistory.historyAttribute records the changes of the loss function and other indicators with epoch. If there is a verification set, it also includes the changes of these indicators in the verification set

evaluate

evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None)

  

This function calculates the model error on some input data according to batch

  • x: Input data, andfitSimilarly, it is a numpy array or a list of numpy arrays

  • y: Label, numpy array

  • batch_ Size: integer, the same meaningfitParameter with the same name

  • Verb: same meaningfitBut only 0 or 1 can be taken

  • sample_ Weight: numpy arrayfitParameter with the same name

This function returns a scalar value of test error (if the model has no other evaluation indicators), or a scalar list (if the model has other evaluation indicators).model.metrics_namesThe meaning of each value in the list is given.

Unless otherwise specified, the parameters of the following functions remain the samefitThe parameters with the same name have the same meaning

Unless otherwise specified, the verb parameter (if any) of the following functions can only take 0 or 1

predict

predict(self, x, batch_size=32, verbose=0)

  

This function obtains the output corresponding to the input data by batch, and its parameters are as follows:

The return value of the function is the numpy array of the predicted value

train_on_batch

train_on_batch(self, x, y, class_weight=None, sample_weight=None)

  

This function performs a parameter update on the data of a batch

Function returns the scalar value of training error or the list of scalar values, andevaluateThe situation is the same.

 

test_on_batch

test_on_batch(self, x, y, sample_weight=None)

  

This function evaluates the model on a batch sample

Return and return of functionevaluateThe situation is the same


predict_on_batch

predict_on_batch(self, x)

  

This function tests the model on a batch sample

Function returns the prediction result of the model on a batch


fit_generator

fit_generator(self, generator, steps_per_epoch, epochs=1, verbose=1, callbacks=None, validation_data=None, validation_steps=None, class_weight=None, max_q_size=10, workers=1, pickle_safe=False, initial_epoch=0)

  

Using Python generator, generate data batch one by one and train. The generator and model will be executed in parallel to improve efficiency. For example, this function allows us to perform real-time data promotion on CPU and model training on GPU

The parameters of the function are:

  • Generator: generator function. The output of the generator should be:

    • A tuple in the form of (inputs, targets)

    • An input, targets, sample_ Weight). All return values should contain the same number of samples. The generator loops infinitely over the dataset. Each epoch is achieved by the number of samples passing through the modelsamples_per_epochWrite an epoch to end

  • steps_ per_ Epoch: integer, when the generator returnssteps_per_epochThe next epoch is executed after the end of one epoch

  • Epochs: integer, the number of rounds of data iteration

  • Verb: log display: 0 is not in the standard output stream to output log information, 1 is to output progress bar record, and 2 is to output one line record for each epoch

  • validation_ Data: has one of the following three forms

    • Generator for generating validation sets

    • A tuple in the form of (inputs, targets)

    • An input, targets, sample_ Tuple of weights

  • validation_ Steps: when validation_ When data is the generator, this parameter specifies the number of times the generator returns the validation set

  • class_ Weight: a dictionary that specifies the weight of categories. It maps categories to weights. It is often used to deal with sample imbalance.

  • sample_ Weight: numpy array of weights, used to adjust the loss function during training (only for training). A 1D vector with the same length as the sample can be passed to weight the sample 1-to-1, or in the face of time series data, a vector in the form of (samples, sequence) can be passed_ Length) matrix to assign different weights to the samples in each time step. In this case, be sure to add thesample_weight_mode='temporal'

  • Workers: maximum number of processes

  • max_ q_ Size: the maximum capacity of the generator queue

  • pickle_ Safe: if true, use process based threads. Because the implementation relies on multiple processes, it cannot pass non pickable parameters to the generator because they cannot be easily passed into child processes.

  • initial_ Epoch: training starts from the epoch specified by this parameter, which is useful when continuing the previous training.

Function returns aHistoryobject

example

def generate_arrays_from_file(path):
    while 1:
    f = open(path)
    for line in f:
        # create numpy arrays of input data
        # and labels, from each line in the file
        x1, x2, y = process_line(line)
        yield ({'input_1': x1, 'input_2': x2}, {'output': y})
    f.close()
 
model.fit_generator(generate_arrays_from_file('/my_file.txt'),
        steps_per_epoch=10000, epochs=10)

  

evaluate_generator

evaluate_generator(self, generator, steps, max_q_size=10, workers=1, pickle_safe=False)

This function uses a generator as the data source to evaluate the model, and the generator should return thetest_on_batchInput data of the same type.

The parameters of the function are:

  • Generator: a generator that generates input batch data

  • val_ Samples: the total number of samples that the generator should return

  • Steps: the number of rounds of data to be returned by the generator

  • max_ q_ Size: the maximum capacity of the generator queue

  • nb_ Worker: the number of processes using process based multithreading

  • pickle_ Safe: if set to true, process based threads are used. Note that because its implementation relies on multiprocessing, nonpickle parameters cannot be passed to the generator because they cannot be easily passed to child processes.


predict_generator

predict_generator(self, generator, steps, max_queue_size=10, workers=1, use_multiprocessing=False, verbose=0)

  

To get data from a generator and make predictions, the generator should return thepredict_on_batchEnter similar data

The parameters of the function are:

  • Generator: a generator that generates input batch data

  • val_ Samples: the total number of samples that the generator should return

  • max_ q_ Size: the maximum capacity of the generator queue

  • nb_ Worker: the number of processes using process based multithreading

  • pickle_ Safe: if set to true, process based threads are used. Note that because its implementation relies on multiprocessing, nonpickle parameters cannot be passed to the generator because they cannot be easily passed to child processes.

Reproduced in: https://blog.csdn.net/weixin_ 30716141/article/details/99481089?utm_ medium= distribute.pc_ relevant.none -task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_ 1-utm_ source= distribute.pc_ relevant.none -task-blog-BlogCommendFromMachineLearnPai2-1.control