Train a Feedforward NN with Error Weights for the performance function
5 views (last 30 days)
Show older comments
Hello, my name is Jansen Acosta and I am currently trying to develop a Feedforward NN to predict 6 different responses.
At this time I am using the Mean squared error performance function - mse. However, for the function mse(net,targets,outputs,errorWeights,...parameters...), the default error weight is {1}, which weights the importance of all targets equally. Is there any way to set different errorWeights values for my Feedforward NN training process? My code looks like this:
nnsize = [10 6] ; nn_Hls = length(nnsize) ; % Hidden Layers
nnTrainFun = 'traingdx' ; % TRAINING
NN = feedforwardnet(nnsize, nnTrainFun) ; % 8 -10,6- 6
nnActFunc = 'tansig' ; % ACTIVATION
for h=1:nn_Hls ; NN.layers{h}.transferFcn = nnActFunc ; end % | HIDDEN Layers
NN.layers{end}.transferFcn = 'purelin'; % Linear transfer function | OUTPUT Layer
NN.performFcn = 'mse' ; % Mean Squared Error performance metric <-------- HELP NEEDED
NN.performParam.normalization = 'standard' ; % OUTPUTS ERROR
NN.performParam.regularization = 0.2 ; %
Thanks in advance
1 Comment
Juan Miguel Serrano Rodríguez
on 14 Mar 2024
Edited: Juan Miguel Serrano Rodríguez
on 14 Mar 2024
I tried the other given answer and it was a waste of time since it was a made up response from chatGPT, errorWeights is not a parameter of the train function.
trainedNet = train(net,X,T,Xi,Ai,EW)
The problem I am facing is that I am not able to set Xi and Ai to its defaults so I can just speficy Ew. This is my (unsuccesful) attempt:
inputs_net = rand(10,5); % 5 inputs
outputs_net = rand(10,2); % 2 outputs
errorWeights = {1; 0.5}; % Half the weight for the second output as in https://www.mathworks.com/help/deeplearning/ref/network.perform.html
net = feedforwardnet();
net = configure(net, inputs_net', outputs_net')
% train(net, inputs_net', outputs_net', zeros, zeros, errorWeights)
train(net, inputs_net', outputs_net', zeros(net.numInputs, net.numInputDelays), zeros(net.numLayers, net.numLayerDelays), errorWeights)
Answers (1)
Mrutyunjaya Hiremath
on 25 Jul 2023
- Yes, you can set different error weights for your Feedforward Neural Network training process. The error weight values can be specified for each target separately to weigh their importance differently. To achieve this, you need to adjust the "errorWeights" parameter when training the network.
- In MATLAB's Neural Network Toolbox, you can use the "train" function to train the network and set the error weights. The train function allows you to pass additional training parameters, including the error weights.
- Here's how you can modify your code to set different error weights for the targets:
nnsize = [10 6]; % Hidden Layers
nnTrainFun = 'traingdx'; % TRAINING
NN = feedforwardnet(nnsize, nnTrainFun); % 8 -10,6- 6
nnActFunc = 'tansig'; % ACTIVATION
for h = 1:numel(nn.layers)
NN.layers{h}.transferFcn = nnActFunc;
end % | HIDDEN Layers
NN.layers{end}.transferFcn = 'purelin'; % Linear transfer function | OUTPUT Layer
NN.performFcn = 'mse'; % Mean Squared Error performance metric
% Set different error weights for each target
errorWeights = [w1, w2, w3, w4, w5, w6]; % Replace w1, w2, ..., w6 with your desired weights
NN.performParam.normalization = 'standard'; % OUTPUTS ERROR
NN.performParam.regularization = 0.2;
% Set the error weights for the network
NN.divideFcn = 'divideind'; % Use individual errors (per sample) for validation and test
NN.divideParam.trainInd = your_train_indices; % Replace with indices of your training data
NN.divideParam.valInd = your_validation_indices; % Replace with indices of your validation data
NN.divideParam.testInd = your_test_indices; % Replace with indices of your test data
NN.performParam.normalization = 'standard';
% Train the network with specified error weights
[NN, tr] = train(NN, inputs, targets, 'useGPU', 'yes', 'errorWeights', errorWeights);
% You can then use the trained network 'NN' for prediction or evaluation.
- In the code above, you need to replace w1, w2, ..., w6 with the desired error weight values for each target. Additionally, replace "your_train_indices", "your_validation_indices", and "your_test_indices" with the appropriate indices for your training, validation, and test datasets, respectively.
0 Comments
See Also
Categories
Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!