Hello, I am using nftool to build a network that takes a vector of 8192 data points (in reality spread across a frequency dimension), which goes through a hidden layer with the standard tansig transfer function, then is reduced to 3 outputs that describe the intensity of certain features in the frequency domain data. The three outputs must sum to 1, and can have any value from 0 to 1 for each parameter. My training data samples this with increments of 0.01 for each of the three parameter values. I am currently using nftool with 30 nodes in the hidden layer and 3 outputs. I tried using two outputs since the third variable is not independent, but that seemed to make things worse. Upon training my network converges rapidly, and the regression plot loks good. When I test the net with new simulation data, it is remarkably robust, extracting the correct values even when the noise makes the data "bad" to my eye. However, when I use my actual data, the network returns negative values for some of the variables, which should not be possible. The output layer has a linear transfer function. I am wondering if using a sigmoidal output function with values of 0 to 1 would solve this issue in a straightforward way? I am also exploring the posibility that my simulated noise (randn) does not recapitulate the noise in my system as well, but using a sigmoidal output layer might be the easiest way to take care of this. Once I generate my netwrok using nftool, can I change the output layer transfer function prior to training? If I can change that transfer function, could I complete the training inside nftool? I find it very easy to use and would like to stick to it for simplicity.
Kshitij Singh answered .
2025-11-20
Your use of the Neural Network Fitting Tool (nftool) is well-suited for regression tasks, such as mapping a high-dimensional frequency domain vector to a low-dimensional output that describes specific features. Let’s address your situation and challenges, including the requirement for outputs to sum to 1 and potential adjustments to your network setup.
Enforcing the Constraint (Outputs Sum to 1):
nftool, you may need to:
Example of the softmax function:
softmax(zi)=exp?(zi)∑j=13exp?(zj)\text{softmax}(z_i) = \frac{\exp(z_i)}{\sum_{j=1}^3 \exp(z_j)}Here, ziz_i are the raw network outputs before applying softmax.
If using MATLAB's nftool, this might require writing custom code outside the GUI for post-processing.
Output Dimensionality:
Recommendation: Stick with 3 outputs and enforce the sum-to-1 constraint explicitly.
Hidden Layer Configuration:
Training Data Granularity:
Performance Evaluation:
Here’s how you can set up a neural network programmatically in MATLAB, enforcing the sum-to-1 constraint via a softmax layer:
% Load your data
input_data = ...; % (8192 x num_samples matrix)
target_data = ...; % (3 x num_samples matrix)
% Create the network
hiddenLayerSize = 30;
net = feedforwardnet(hiddenLayerSize);
% Configure the network
net.layers{2}.transferFcn = 'softmax'; % Enforce sum-to-1 constraint on outputs
% Divide data for training, validation, and testing
net.divideParam.trainRatio = 0.7;
net.divideParam.valRatio = 0.15;
net.divideParam.testRatio = 0.15;
% Train the network
[net, tr] = train(net, input_data, target_data);
% Evaluate the performance
outputs = net(input_data);
performance = perform(net, target_data, outputs);
% Display performance
disp(['Performance (MSE): ', num2str(performance)]);
The softmax activation ensures your outputs are valid probabilities, summing to 1. Without softmax, the network's output layer can produce unconstrained values, making it harder to meet your requirement.
If Overfitting Occurs:
If Convergence is Slow:
If the Model Underperforms:
trainlm, trainbr, etc.).Let me know if you need further clarification or assistance in implementing these ideas!