39.3 Training the Tuned Architecture on the Test Data
Since we are interested in the explainability of the model, we will train the tuned architecture on the test data.
spotpythons’s test_model function [DOC] is used to train the model on the test data.
Note: Until now, we do not use any information about the NN’s weights and biases. Only the architecture, which is available as the config, is used.
spotpython used the TensorBoard logger to save the training process in the ./runs directory. Therefore, we have to enable the TensorBoard logger in the fun_control dictionary. To get a clean start, we remove an existing runs folder.
from spotpython.light.testmodel import test_modelfrom spotpython.light.loadmodel import load_light_from_checkpointfun_control.update({"tensorboard_log": True})test_model(config, fun_control)
As shown in the code above, the last checkpoint ist saved.
spotpython’s method load_light_from_checkpoint is used to load the last checkpoint and to get the model’s weights and biases. It requires the fun_control dictionary and the config_id as input to find the correct checkpoint.
Now, the model is trained and the weights and biases are available.
39.4 Visualizing the Neural Network Architecture
# get the devicefrom spotpython.utils.device import getDevicedevice = getDevice()
from spotpython.plot.xai import viz_netviz_net(model, device=device)
39.5 XAI Methods
spotpython provides methods to explain the model’s predictions. The following neural network elements can be analyzed:
39.5.1 Weights
Weights are the parameters of the neural network that are learned from the data during training. They connect neurons between layers and determine the strength and direction of the signal sent from one neuron to another. The network adjusts the weights during training to minimize the error between the predicted output and the actual output.
Interpretation of the weights: A high weight value indicates a strong influence of the input neuron on the output. Positive weights suggest a positive correlation, whereas negative weights suggest an inverse relationship between neurons.
39.5.2 Activations
Activations are the outputs produced by neurons after applying an activation function to the weighted sum of inputs. The activation function (e.g., ReLU, sigmoid, tanh) adds non-linearity to the model, allowing it to learn more complex relationships.
Interpretation of the activations: The value of activations indicates the intensity of the signal passed to the next layer. Certain activation patterns can highlight which features or parts of the data the network is focusing on.
39.5.3 Gradients
Gradients are the partial derivatives of the loss function with respect to different parameters (weights) of the network. During backpropagation, gradients are used to update the weights in the direction that reduces the loss by methods like gradient descent.
Interpretation of the gradients: The magnitude of the gradient indicates how much a parameter should change to reduce the error. A large gradient implies a steeper slope and a bigger update, while a small gradient suggests that the parameter is near an optimal point. If gradients are too small (vanishing gradient problem), the network may learn slowly or stop learning. If they are too large (exploding gradient problem), the updates may be unstable.
sptpython provides the method get_gradients to get the gradients of the model.