Coverage for src/sensai/tensorflow/tf_mlp.py: 37%
19 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-13 22:17 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-13 22:17 +0000
1from tensorflow import keras
3from .tf_base import KerasVectorRegressionModel
4from .. import normalisation
7class KerasMultiLayerPerceptronVectorRegressionModel(KerasVectorRegressionModel):
8 def __init__(self, hidden_dims=(5,5), hidden_activation="sigmoid", output_activation="sigmoid", loss="mse",
9 metrics=("mse",), optimiser="adam", normalisation_mode=normalisation.NormalisationMode.MAX_BY_COLUMN, **kwargs):
10 super().__init__(normalisation_mode, loss, metrics, optimiser, **kwargs)
11 self.hiddenDims = hidden_dims
12 self.hiddenActivation = hidden_activation
13 self.outputActivation = output_activation
15 def __str__(self):
16 params = dict(hiddenDims=self.hiddenDims, hiddenActivation=self.hiddenActivation,
17 outputActivation=self.outputActivation)
18 return f"{self.__class__.__name__}{params}={super().__str__()}"
20 def _create_model(self, input_dim, output_dim):
21 model_inputs = keras.Input(shape=(input_dim,), name='input')
22 x = model_inputs
23 for i, hiddenDim in enumerate(self.hiddenDims):
24 x = keras.layers.Dense(hiddenDim, activation=self.hiddenActivation, name='dense_%d' % i)(x)
25 model_outputs = keras.layers.Dense(output_dim, activation=self.outputActivation, name='predictions')(x)
26 return keras.Model(inputs=model_inputs, outputs=model_outputs)