How do I access individual weight matrices in a custom training loop?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Suppose I have a basic feedforward network of the type
, minimising a loss function
with respect to targets
.
![3-layer network function equaition](https://www.mathworks.com/matlabcentral/answers/uploaded_files/993405/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/993410/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/993415/image.png)
I would like to add a penalty term that regularises an intermediate signal, such as
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/993420/image.png)
Could I define a custom loss function to do this? How can I access individual weight matrices in a custom training loop?
0 comentarios
Respuestas (1)
Abhijit Bhattacharjee
el 19 de Mayo de 2022
I assume by custom training loops, you are referring to custom training loops in the Deep Learning Toolbox. If so, here is a good resource to start with: Define Model Loss Function for Custom Training Loop.
There are two different methods to access the weight arrays, depending on how you defined the neural network in the custom training loop. If you used a dlnetwork, then you can access the weights of any layer that has learnable parameters.
Example for dlnetwork:
% Create feedforward neural network
layers = [
featureInputLayer(1,"Name","featureinput")
fullyConnectedLayer(10,"Name","fc_1")
sigmoidLayer("Name","sigmoid_1")
fullyConnectedLayer(10,"Name","fc_2")
sigmoidLayer("Name","sigmoid_2")
fullyConnectedLayer(10,"Name","fc_3")
sigmoidLayer("Name","sigmoid_3")];
dlnet = dlnetwork(layers);
% Access weights of the first FC layer
dlnet.Layers(2).Weights
If you used a model function in the custom training loop instead, then you would need to define a parameters array that contains the weights and biases of the model upfront. You can access these parameters at any time using the names you already defined.
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!