metamod.ANN_pt module¶
Custom ANN definition using TensorFlow.
This module contains the definition of an ANN comptable with the SMT Toolbox.
-
metamod.ANN_pt.activations¶ Available activation functions.
Type: dict
-
metamod.ANN_pt.initializers¶ Available kernel initializers.
Type: dict
Notes
Keras tuner logs are not stored in data due to max path length issues in kerastuner.
See also
PyTorch documentation - https://pytorch.org/docs/stable/index.html
-
class
metamod.ANN_pt.ANN_pt(**kwargs)¶ Bases:
metamod.ANN.ANN_baseANN class.
-
name¶ Name of the surrogate model.
Type: str
-
model¶ The model of the ANN.
-
early_stop¶ Number of training epchs before early stopping.
Type: int
-
build_hypermodel(hp)¶ Build the hypermodel of the ANN with Keras Tuner hyperparameters.
Parameters: hp (kerastuner.engine.hyperparameters.HyperParameters) – Hyperparameters. Returns: The model of the ANN. Return type: model () Notes
No kernel regularizers implemented so far.
See also
Keras Tuner documentation: https://keras-team.github.io/keras-tuner/
-
pretrain(inputs, outputs, iteration)¶ Optimize the hyperparameters of the ANN.
Parameters: - inputs (np.array) – All input data.
- outputs (np.array) – All output data.
- iteration (int) – Iteration number.
Returns: Optimal hyperparameters.
Return type: best_hp (kerastuner.engine.hyperparameters.HyperParameters)
Notes
Optimization objective fixed on val_loss.
-
save()¶ Save the ANN into an external file.
-
-
class
metamod.ANN_pt.SparseModel(neurons_hyp, activation_hyp, kernel_regularizer, in_dim, layers_hyp, out_dim, init, bias_init)¶ Bases:
torch.nn.modules.module.ModuleDefines an ANN with a subnetwork for each output.
-
activation¶ Activation function.
Type: function
-
subnetworks¶ Neural network.
-
early_stopping(history, metric, tolerance, patience)¶ Check whether the early stopping condition has been met.
Parameters: - history (metamod.ANN_pt.TrainHistory) – Metrics history during the training.
- metric (str) – Early stopping decision metric.
- tolerance (float) – Early stopping tolerance.
- patience (int) – Early stopping patience.
Returns: Whether to early stop the training.
Return type: stop (bool)
-
fit(epochs, train_in, train_out, test_in=None, test_out=None, optimizing=False)¶ Train the ANN.
Parameters: - epochs (int) –
- train_in (torch.Tensor) – Train input data.
- train_out (torch.Tensor) – Train output data.
- test_in (torch.Tensor) – Test input data.
- test_out (torch.Tensor) – Test output data.
- optimizing (bool) – Whether this is an optimization run.
Returns: Metrics history during the training.
Return type: history (metamod.ANN_pt.TrainHistory)
-
forward(x)¶ Forward-pass throught the network.
Parameters: x (torch.Tensor) – Input tensor. Returns: Output tensor. Return type: out (torch.Tensor)
-
-
class
metamod.ANN_pt.TrainHistory¶ Bases:
objectThis class stored the metrics history.
-
history¶ Stores training and validation losses.
Type: dict
-
store(loss, loss_eval=None)¶
-
-
metamod.ANN_pt.swish(x)¶ Swish activation function.
Parameters: x (torch.Tensor) – Weighted inputs. Returns: Neuron’s activations. Return type: activation (torch.Tensor)