timeserio.keras.multinetwork module

class timeserio.keras.multinetwork.MultiNetworkBase(**hyperparams)[source]

Bases: abc.ABC

Abstract base class for models that contain multiple keras sub-models.

Examples

Define a simple pair of regression models using MultiNetworkBase:

>>> from timeserio.keras.multinetwork import MultiNetworkBase
>>> from keras.models import Model
>>> from keras.layers import Input, Dense
>>> class MyNetwork(MultiNetworkBase):
...     def _model(self, hidden_units=8):
...         input = Input((1,))
...         intermediate = Dense(hidden_units)(input)  # shared layer
...         output_1 = Dense(1)(intermediate)
...         output_2 = Dense(1)(intermediate)
...         model_1 = Model(input, output_1)
...         model_2 = Model(input, output_2)
...         model_1.compile(loss="MSE", optimizer="SGD")
...         model_2.compile(loss="MSE", optimizer="SGD")
...         return {"model_1": model_1, "model_2": model_2}

Instantiate a multi-network, using keyword arguments to provide hyperparameters:

>>> mnet = MyNetwork(hidden_units=32)

Models are instantiated on demand:

>>> mnet.model
{'model_1': <keras.engine.training.Model ...>, 'model_2': <...>}
__getstate__()[source]

Get a picklable state dictionary of the model.

Return type

Dict

__setstate__(state)[source]

Set model from unpickled state dictionary.

property callbacks
check_model_name(model)[source]
check_params(params)[source]

Check for user typos in params.

Parameters

params – dictionary; the parameters to be checked

Raises

ValueError – if any member of params is not a valid argument.

Return type

None

evaluate(x, y, model=None, **kwargs)[source]

Evaluate estimator on given data.

Parameters
  • x – array-like, shape (n_samples, n_features) Test samples where n_samples is the number of samples and n_features is the number of features.

  • y – array-like

  • model – string

  • kwargs – keyword arguments. Legal arguments are the arguments of Sequential.evaluate.

Returns

metrics

List[float]

A list of loss and evaluation metric values

evaluate_generator(generator, model=None, **kwargs)[source]

Evaluate estimator on given data.

Parameters
  • generator – generator or keras.utils.Sequence predict batches as in Sequential.evaluate_generator

  • model – string

  • **kwargs – keyword arguments. Legal arguments are the arguments of Sequential.evaluate_generator.

Returns

evaluation metric value(s)

fit(x, y, model=None, reset_weights=False, reset_optimizers=True, reset_history=False, **kwargs)[source]

Run training on one model.

fit_generator(generator, model=None, reset_weights=False, reset_optimizers=True, reset_history=False, **kwargs)[source]

Train model from generator.

Parameters
  • generator – generator or keras.utils.Sequence Training batches as in Sequential.fit_generator

  • init – bool initialize new model; use False to continue training

  • **kwargs – dictionary arguments Legal arguments are the arguments of Sequential.fit_generator

Returns

history

List[Dict]

details about the training history at each epoch.

get_params(**params)[source]

Get parameters for this estimator.

Parameters

**params – ignored (exists for API compatibility).

Returns

Dictionary of parameter names mapped to their values.

property hyperparams
property model
property model_names
predict(x, model=None, **kwargs)[source]

Return predictions for the given test data.

Parameters
  • x – array-like, shape (n_samples, n_features) Test samples where n_samples is the number of samples and n_features is the number of features.

  • **kwargs – dictionary arguments Legal arguments are the arguments of Sequential.predict.

Returns

preds

array-like, shape (n_samples, …)

Predictions.

predict_generator(generator, model=None, **kwargs)[source]

Return predictions from a batch generator.

Parameters
  • generator – generator or keras.utils.Sequence predict batches as in Sequential.predict_generator

  • **kwargs – dictionary arguments Legal arguments are the arguments of Sequential.predict_generator

Returns

preds

array-like, shape (n_samples, …)

Predictions.

score(*args, **kwargs)[source]

Alias for scikit compatibility.

set_params(**params)[source]

Set the parameters of this estimator.

Parameters

**params – Dictionary of parameter names mapped to their values.

Returns

self

property trainable_models