kslearn.wrappers.KerasRegressor

class KerasRegressor(build_fn=None, model_id=None, logroot_dir=None, tb_display_url=None, period=None, patience=None, random_state=None, reuse=True, **sk_params)[source]

Implementation of the scikit-learn regressor API for Keras.

build_fn: callable function or class instance.

The build_fn should construct, compile and return a Keras model, which will then be used to fit/predict. One of the following three values could be passed to build_fn:

  1. A function
  2. An instance of a class that implements the __call__ method

3. None. This means you implement a class that inherits from either KerasClassifier or KerasRegressor. The __call__ method of the present class will then be treated as the default build_fn.

model_id: str or None, default=None.

This is used to log filename.

When model_id is None, this is generated by date time.

logroot_dir: str or None, default=None.

Log root dir. this model’s logfile is saved under “logroot_dir/model_id/”.

When logroot_dir is None, a logfile is not saved.

tb_display_url: str or None, default=None.

Tensorboard’s url. When run fit, tensorboard and keras model plot is displayed on jupyter.

When tb_display_url is None, this is not displayed.

period: int or None, default=None.

Interval (number of epochs) between checkpoints for save the model (keras.callbacks.ModelCheckpoint’s arg) .

When period is None, unless proper ‘callback’ argument is given at the time of running ‘fit’, this model is not saved.

patience: int or None, default=None.

Number of epochs with no improvement after which training will be stopped (keras.callbacks.EarlyStopping’s arg).

When patience is None, unless proper ‘callback’ argument is given at the time of running ‘fit’, earlystopping is not used.

random_state: int or None, default=None.

The seed used by the random number generator.

(this seed is used in tensorflow, numpy and python random.)

reuse: bool, default=True.
When reuse is True, tensorflow session is not initialized per running fit.
**sk_params: dictonary.

model parameters & fitting parameters.

sk_params takes both model parameters and fitting parameters.

Legal modelparameters are the arguments of build_fn.

Methods

__init__([build_fn, model_id, logroot_dir, …])
check_params(params) Checks for user typos in “params”.
evaluate(x, y, **kwargs) Retern model’s loss.
evaluate_generator(generator, steps, **kwargs) Evaluates the model on a data generator.
filter_sk_params(fn[, override]) Filters sk_params and returns those in fn’s arguments.
fit(x, y[, validation_data, callbacks]) Constructs a new model with build_fn & fit the model to (x, y).
fit_generator(generator, steps_per_epoch[, …]) Constructs a new model with build_fn & fit the model to genarator’s output (x, y).
get_params(**params) Gets parameters for this estimator.
predict(x, **kwargs) Returns predictions for the given test data.
predict_generator(generator, steps, **kwargs) Generates predictions for the input samples from a data generator.
save(filepath[, overwrite, include_optimizer]) Save a model to a HDF5 file.
score(x, y, **kwargs) Returns the mean loss on the given test data and labels.
set_params(**params) Sets the parameters of this estimator.
set_session_config(config) Set tensorfow session config.
evaluate(x, y, **kwargs)

Retern model’s loss.

Parameters:
  • x (array-like, shape (n_samples, n_features)) – Training samples where n_samples in the number of samples and n_features is the number of features.
  • y (array-like, shape (n_samples,) or (n_samples, n_outputs)) – True values for X.
  • **kwargs (dictionary.) – Legal arguments are the arguments of evaluate (builded model method).
  • Returns (#) –
  • loss – model’s loss
evaluate_generator(generator, steps, **kwargs)

Evaluates the model on a data generator.

Parameters:
  • generator (generator) –

    The output of the generator must be either

    • a tuple (inputs, targets)
    • a tuple (inputs, targets, sample_weights).
  • steps (int.) – Total number of steps (batches of samples).
  • **kwargs (dictionary.) – Legal arguments are the arguments of predict_generator (builded model method).
Returns:

model’s loss

Return type:

loss

fit(x, y, validation_data=None, callbacks=None, **kwargs)

Constructs a new model with build_fn & fit the model to (x, y).

Parameters:
  • x (array-like, shape (n_samples, n_features)) – Training samples where n_samples in the number of samples and n_features is the number of features.
  • y (array-like, shape (n_samples,) or (n_samples, n_outputs)) – True values for X.
  • validation_data (tuple of array-like (x, y) or None.) –

    Data for validation.

    When validation_data is None, skip validation.

  • callbacks (list of keras callback classes or None.) –

    When callbacks is None, set proper this model’s callbacks.

    Otherwise, set this arg as this model’s callbacks.

  • **kwargs (dictionary.) – Legal arguments are the arguments of fit (builded model method).
  • Returns (#) –
  • history (object) – details about the training history at each epoch.
fit_generator(generator, steps_per_epoch, validation_data=None, callbacks=None, **kwargs)

Constructs a new model with build_fn & fit the model to genarator’s output (x, y).

Parameters:
  • generator (generator) –

    The output of the generator must be either

    • a tuple (inputs, targets)
    • a tuple (inputs, targets, sample_weights).
  • steps_per_epoch (int.) – Total number of steps (batches of samples).
  • validation_data (tuple of array-like (x, y) or None.) –

    validation_data: This can be either

    • A generator for the validation data
    • A tuple (inputs, targets)
    • A tuple (inputs, targets, sample_weights).

    When validation_data is None, skip validation.

  • callbacks (list of keras callback classes or None.) –

    When callbacks is None, set proper this model’s callbacks.

    Otherwise, set this arg as this model’s callbacks.

  • **kwargs (dictionary.) – Legal arguments are the arguments of fit_generator (builded model method).
  • Returns (#) –
  • history (object) – details about the training history at each epoch.
predict(x, **kwargs)[source]

Returns predictions for the given test data.

Parameters:
  • x (array-like, shape (n_samples, n_features)) – Test samples where n_samples in the number of samples and n_features is the number of features.
  • **kwargs (dictionary arguments) – Legal arguments are the arguments of predict (builded model method).
Returns:

preds – Predictions.

Return type:

array-like, shape (n_samples,)

predict_generator(generator, steps, **kwargs)

Generates predictions for the input samples from a data generator.

Parameters:
  • generator (generator) –

    The output of the generator must be either

    • a tuple (inputs, targets)
    • a tuple (inputs, targets, sample_weights).
  • steps (int.) – Total number of steps (batches of samples).
  • **kwargs (dictionary.) – Legal arguments are the arguments of predict_generator (builded model method).
Returns:

Predictions.

Return type:

preds

save(filepath, overwrite=True, include_optimizer=True)

Save a model to a HDF5 file.

Parameters:
  • model – Keras model instance to be saved.
  • filepath (str.) – path where to save the model.
  • overwrite (bool, default=True.) – Whether we should overwrite any existing model at the target location, or instead ask the user with a manual prompt.
  • include_optimizer (bool, default=True.) – If True, save optimizer’s state together.
score(x, y, **kwargs)[source]

Returns the mean loss on the given test data and labels.

Parameters:
  • x (array-like, shape (n_samples, n_features)) – Test samples where n_samples in the number of samples and n_features is the number of features.
  • y (array-like, shape (n_samples,)) – True labels for X.
  • **kwargs (dictionary arguments) – Legal arguments are the arguments of evaluate (builded model method).
Returns:

score – Mean accuracy of predictions on X wrt. y.

Return type:

float

set_session_config(config)

Set tensorfow session config.

Parameters:config – config for tensorflow session.