Model factories

Model factories are stand alone functions which take an arbitrary number of primitive parameters (int, float, list, dict, etc) and return a model which can then be used in the kind parameter of some Scikit-Learn like wrapper model.

An example of this is KerasAutoEncoder which accepts a kind argument (as all custom gordo models do) and can be given feedforward_model. Meaning that function will be used to create the underlying Keras model for KerasAutoEncoder

feedforward factories

gordo.machine.model.factories.feedforward_autoencoder.feedforward_hourglass(n_features: int, n_features_out: int = None, encoding_layers: int = 3, compression_factor: float = 0.5, func: str = 'tanh', optimizer: Union[str, tensorflow.keras.optimizers.Optimizer] = 'Adam', optimizer_kwargs: Dict[str, Any] = {}, compile_kwargs: Dict[str, Any] = {}, **kwargs) → tensorflow.keras.models.Sequential[source]

Builds an hourglass shaped neural network, with decreasing number of neurons as one gets deeper into the encoder network and increasing number of neurons as one gets out of the decoder network.

Parameters
  • n_features (int) – Number of input and output neurons.

  • n_features_out (Optional[int]) – Number of features the model will output, default to n_features.

  • encoding_layers (int) – Number of layers from the input layer (exclusive) to the narrowest layer (inclusive). Must be > 0. The total nr of layers including input and output layer will be 2*encoding_layers + 1.

  • compression_factor (float) – How small the smallest layer is as a ratio of n_features (smallest layer is rounded up to nearest integer). Must satisfy 0 <= compression_factor <= 1.

  • func (str) – Activation function for the internal layers

  • optimizer (Union[str, Optimizer]) – If str then the name of the optimizer must be provided (e.x. “Adam”). The arguments of the optimizer can be supplied in optimization_kwargs. If a Keras optimizer call the instance of the respective class (e.x. Adam(lr=0.01,beta_1=0.9, beta_2=0.999)). If no arguments are provided Keras default values will be set.

  • optimizer_kwargs (Dict[str, Any]) – The arguments for the chosen optimizer. If not provided Keras’ default values will be used.

  • compile_kwargs (Dict[str, Any]) – Parameters to pass to keras.Model.compile.

Notes

The resulting model will look like this when n_features = 10, encoding_layers= 3, and compression_factor = 0.3:

* * * * * * * * * *
  * * * * * * * *
     * * * * *
       * * *
       * * *
     * * * * *
  * * * * * * * *
* * * * * * * * * *
Returns

Return type

keras.models.Sequential

Examples

>>> model = feedforward_hourglass(10)
>>> len(model.layers)
7
>>> [model.layers[i].units for i in range(len(model.layers))]
[8, 7, 5, 5, 7, 8, 10]
>>> model = feedforward_hourglass(5)
>>> [model.layers[i].units for i in range(len(model.layers))]
[4, 4, 3, 3, 4, 4, 5]
>>> model = feedforward_hourglass(10, compression_factor=0.2)
>>> [model.layers[i].units for i in range(len(model.layers))]
[7, 5, 2, 2, 5, 7, 10]
>>> model = feedforward_hourglass(10, encoding_layers=1)
>>> [model.layers[i].units for i in range(len(model.layers))]
[5, 5, 10]
gordo.machine.model.factories.feedforward_autoencoder.feedforward_model(n_features: int, n_features_out: int = None, encoding_dim: Tuple[int, ...] = (256, 128, 64), encoding_func: Tuple[str, ...] = ('tanh', 'tanh', 'tanh'), decoding_dim: Tuple[int, ...] = (64, 128, 256), decoding_func: Tuple[str, ...] = ('tanh', 'tanh', 'tanh'), out_func: str = 'linear', optimizer: Union[str, tensorflow.keras.optimizers.Optimizer] = 'Adam', optimizer_kwargs: Dict[str, Any] = {}, compile_kwargs: Dict[str, Any] = {}, **kwargs) → tensorflow.keras.models.Sequential[source]

Builds a customized keras neural network auto-encoder based on a config dict

Parameters
  • n_features (int) – Number of features the dataset X will contain.

  • n_features_out (Optional[int]) – Number of features the model will output, default to n_features.

  • encoding_dim (tuple) – Tuple of numbers with the number of neurons in the encoding part.

  • decoding_dim (tuple) – Tuple of numbers with the number of neurons in the decoding part.

  • encoding_func (tuple) – Activation functions for the encoder part.

  • decoding_func (tuple) – Activation functions for the decoder part.

  • out_func (str) – Activation function for the output layer

  • optimizer (Union[str, Optimizer]) – If str then the name of the optimizer must be provided (e.x. “Adam”). The arguments of the optimizer can be supplied in optimize_kwargs. If a Keras optimizer call the instance of the respective class (e.x. Adam(lr=0.01,beta_1=0.9, beta_2=0.999)). If no arguments are provided Keras default values will be set.

  • optimizer_kwargs (Dict[str, Any]) – The arguments for the chosen optimizer. If not provided Keras’ default values will be used.

  • compile_kwargs (Dict[str, Any]) – Parameters to pass to keras.Model.compile.

Returns

Return type

keras.models.Sequential

gordo.machine.model.factories.feedforward_autoencoder.feedforward_symmetric(n_features: int, n_features_out: int = None, dims: Tuple[int, ...] = (256, 128, 64), funcs: Tuple[str, ...] = ('tanh', 'tanh', 'tanh'), optimizer: Union[str, tensorflow.keras.optimizers.Optimizer] = 'Adam', optimizer_kwargs: Dict[str, Any] = {}, compile_kwargs: Dict[str, Any] = {}, **kwargs) → tensorflow.keras.models.Sequential[source]

Builds a symmetrical feedforward model

Parameters
  • n_features (int) – Number of input and output neurons.

  • n_features_out (Optional[int]) – Number of features the model will output, default to n_features.

  • dim (List[int]) – Number of neurons per layers for the encoder, reversed for the decoder. Must have len > 0.

  • funcs (List[str]) – Activation functions for the internal layers

  • optimizer (Union[str, Optimizer]) – If str then the name of the optimizer must be provided (e.x. “Adam”). The arguments of the optimizer can be supplied in optimization_kwargs. If a Keras optimizer call the instance of the respective class (e.x. Adam(lr=0.01,beta_1=0.9, beta_2=0.999)). If no arguments are provided Keras default values will be set.

  • optimizer_kwargs (Dict[str, Any]) – The arguments for the chosen optimizer. If not provided Keras’ default values will be used.

  • compile_kwargs (Dict[str, Any]) – Parameters to pass to keras.Model.compile.

Returns

Return type

keras.models.Sequential

lstm factories

gordo.machine.model.factories.lstm_autoencoder.lstm_hourglass(n_features: int, n_features_out: int = None, lookback_window: int = 1, encoding_layers: int = 3, compression_factor: float = 0.5, func: str = 'tanh', out_func: str = 'linear', optimizer: Union[str, tensorflow.keras.optimizers.Optimizer] = 'Adam', optimizer_kwargs: Dict[str, Any] = {}, compile_kwargs: Dict[str, Any] = {}, **kwargs) → tensorflow.keras.models.Sequential[source]

Builds an hourglass shaped neural network, with decreasing number of neurons as one gets deeper into the encoder network and increasing number of neurons as one gets out of the decoder network.

Parameters
  • n_features (int) – Number of input and output neurons.

  • n_features_out (Optional[int]) – Number of features the model will output, default to n_features.

  • encoding_layers (int) –

    Number of layers from the input layer (exclusive) to the

    narrowest layer (inclusive). Must be > 0. The total nr of layers including input and output layer will be 2*encoding_layers + 1.

    compression_factor: float

    How small the smallest layer is as a ratio of n_features (smallest layer is rounded up to nearest integer). Must satisfy 0 <= compression_factor <= 1.

  • func (str) – Activation function for the internal layers.

  • out_func (str) – Activation function for the output Dense layer.

  • optimizer (Union[str, Optimizer]) – If str then the name of the optimizer must be provided (e.x. “Adam”). The arguments of the optimizer can be supplied in optimization_kwargs. If a Keras optimizer call the instance of the respective class (e.x. Adam(lr=0.01,beta_1=0.9, beta_2=0.999)). If no arguments are provided Keras default values will be set.

  • optimizer_kwargs (Dict[str, Any]) – The arguments for the chosen optimizer. If not provided Keras’ default values will be used.

  • compile_kwargs (Dict[str, Any]) – Parameters to pass to keras.Model.compile.

Returns

Return type

keras.models.Sequential

Examples

>>> model = lstm_hourglass(10)
>>> len(model.layers)
7
>>> [model.layers[i].units for i in range(len(model.layers))]
[8, 7, 5, 5, 7, 8, 10]
>>> model = lstm_hourglass(5)
>>> [model.layers[i].units for i in range(len(model.layers))]
[4, 4, 3, 3, 4, 4, 5]
>>> model = lstm_hourglass(10, compression_factor=0.2)
>>> [model.layers[i].units for i in range(len(model.layers))]
[7, 5, 2, 2, 5, 7, 10]
>>> model = lstm_hourglass(10, encoding_layers=1)
>>> [model.layers[i].units for i in range(len(model.layers))]
[5, 5, 10]
gordo.machine.model.factories.lstm_autoencoder.lstm_model(n_features: int, n_features_out: int = None, lookback_window: int = 1, encoding_dim: Tuple[int, ...] = (256, 128, 64), encoding_func: Tuple[str, ...] = ('tanh', 'tanh', 'tanh'), decoding_dim: Tuple[int, ...] = (64, 128, 256), decoding_func: Tuple[str, ...] = ('tanh', 'tanh', 'tanh'), out_func: str = 'linear', optimizer: Union[str, tensorflow.keras.optimizers.Optimizer] = 'Adam', optimizer_kwargs: Dict[str, Any] = {}, compile_kwargs: Dict[str, Any] = {}, **kwargs) → tensorflow.keras.models.Sequential[source]

Builds a customized Keras LSTM neural network auto-encoder based on a config dict.

Parameters
  • n_features (int) – Number of features the dataset X will contain.

  • n_features_out (Optional[int]) – Number of features the model will output, default to n_features.

  • lookback_window (int) – Number of timesteps used to train the model. One timestep = current observation in the sample. Two timesteps = current observation + previous observation in the sample. …

  • encoding_dim (tuple) – Tuple of numbers with the number of neurons in the encoding part.

  • decoding_dim (tuple) – Tuple of numbers with the number of neurons in the decoding part.

  • encoding_func (tuple) – Activation functions for the encoder part.

  • decoding_func (tuple) – Activation functions for the decoder part.

  • out_func (str) – Activation function for the output Dense layer.

  • optimizer (Union[str, Optimizer]) – If str then the name of the optimizer must be provided (e.x. “Adam”). The arguments of the optimizer can be supplied in optimize_kwargs. If a Keras optimizer call the instance of the respective class (e.x. Adam(lr=0.01,beta_1=0.9, beta_2=0.999)). If no arguments are provided Keras default values will be set.

  • optimizer_kwargs (Dict[str, Any]) – The arguments for the chosen optimizer. If not provided Keras’ default values will be used.

  • compile_kwargs (Dict[str, Any]) – Parameters to pass to keras.Model.compile.

Returns

Returns Keras sequential model.

Return type

keras.models.Sequential

gordo.machine.model.factories.lstm_autoencoder.lstm_symmetric(n_features: int, n_features_out: int = None, lookback_window: int = 1, dims: Tuple[int, ...] = (256, 128, 64), funcs: Tuple[str, ...] = ('tanh', 'tanh', 'tanh'), out_func: str = 'linear', optimizer: Union[str, tensorflow.keras.optimizers.Optimizer] = 'Adam', optimizer_kwargs: Dict[str, Any] = {}, compile_kwargs: Dict[str, Any] = {}, **kwargs) → tensorflow.keras.models.Sequential[source]

Builds a symmetrical lstm model

Parameters
  • n_features (int) – Number of input and output neurons.

  • n_features_out (Optional[int]) – Number of features the model will output, default to n_features.

  • lookback_window (int) – Number of timesteps used to train the model. One timestep = sample contains current observation. Two timesteps = sample contains current and previous observation. …

  • dims (Tuple[int,..]) – Number of neurons per layers for the encoder, reversed for the decoder. Must have len > 0

  • funcs (List[str]) – Activation functions for the internal layers.

  • out_func (str) – Activation function for the output Dense layer.

  • optimizer (Union[str, Optimizer]) – If str then the name of the optimizer must be provided (e.x. “Adam”). The arguments of the optimizer can be supplied in optimization_kwargs. If a Keras optimizer call the instance of the respective class (e.x. Adam(lr=0.01,beta_1=0.9, beta_2=0.999)). If no arguments are provided Keras default values will be set.

  • optimizer_kwargs (Dict[str, Any]) – The arguments for the chosen optimizer. If not provided Keras’ default values will be used.

  • compile_kwargs (Dict[str, Any]) – Parameters to pass to keras.Model.compile.

Returns

Returns Keras sequential model.

Return type

keras.models.Sequential