Training using LM algorithm in Neural Network Toolbox in MATLAB - matlab

I am trying to use the Neural Toolbox in MATLAB to train a dataset using the LM algorithm. The network architecture I am using is feedforward with one hidden layer while the transfer functions I am using is the tansig for input-to-hidden layer and pureline for hidden-to-output layer. During training, the values of MSE with increasing number of epochs shows up on the screen till the performance goal is met, or the maximum no. of epochs is reached. However, what I am interested is in is to save the value of MSE at each epoch from the start till the end of training as a datafile (.txt or .dat) in my hard drive. I have browsed a lot but I could not find a way to do this. Can someone please help me in this regard. Thanks.

If you create you code with network called net then you can get the information about MSE using the function [net tr ] = train(net,x,t).
For instance if we use simplefit_dataset simple data with a simple network the result is on tr.perf which shows that MSE for each epoch for the train data:
close all, clear all, clc, plt=0;
[x,t] = simplefit_dataset;
net = fitnet(10);
rng(0)
[net tr ] = train(net, x, t);
plt = plt+1, figure(plt), hold on;
plot(tr.perf,'b', 'LineWidth', 2)
For more information,please visit the following link:
https://www.mathworks.com/matlabcentral/answers/57648-how-to-plot-mse-for-train-and-test

To save the output results in a text file please use the following code:
fileID = fopen('Output.txt','w');
fprintf(fileID,'%f\n',tr.perf);
fclose(fileID);
For more information about writing and reading data from/to the text in matlab refer to the below link:
https://www.mathworks.com/help/matlab/import_export/writing-to-text-data-files-with-low-level-io.html

All results of mlp_ANN toolbax is in tr variable on workspace.you have not to do anything as writing code to get MSE for each epoch.All you need is that go into the tr variable on workspace after halting train and open the pref and copy it to notpad as .txt file.

Related

Training Neural network to predict sin(x) matlab

It's been 3 days since i'm trying to train many neural networks to predict sin(x) function, i'm using matlab 2016b (i have to work with it in my assignement)
what i did :
change layers
duplicate dataset (big , small)
add/sub periods
shuffle the data
change neural's number per layer
change learning function
change the transfer function and mapped the target
all that with no good prediction, can anyone explain me what i'm doing wrong ,
and it would be very helpful to paste any good book for ("preparing dataset befor traing", "knowing the best NN's structure for your project",...
and any book seems helpful)
my actual code : (i'm using nntool for the training )
%% input and target
input = 0:pi/100:8*pi;
target = sin(input) ;
plot(input,sin(input)),
hold on,
inputA = input;
targetA = target;
plot(inputA,targetA),
hold on,
%simulate input
output=sim(network2,inputA);
plot(inputA,output,'or')
hold off

Normalizing/transforming/reshaping the output signal in simulink so that it is build only of connected spiking values

I have a system build in simulink and the output of the system.
The output has valuable data points in the peaks/spikes and other, not valuable data points at magnitude of 70.
What I am struggling to achieve is the output signal, consisting only of the valuable data points connected with each other straightforward(basically, these are the datapoints I need).
I attached the picture with the original output signal from the scope and the one I built from it in matlab after extracting the original as a structure with time into workspace.
Output in the scope
After processing the signal from the scope in matlab
Here is the code I use to process and plot it:
ab = [];
a = [];
for i=1:numel(Tc.signals.values)
if Tc.signals.values(i)<70
ab = [ab;Tc.signals.values(i)];
a = [a;Tc.time(i)];
end
end
plot(a, ab, '-k', 'LineWidth', 1);
grid on;
My question is what blocks and how should I add so that the output is transformed during the simulation into what I ploted outside the simulation from matlab?
I really have difficulties finding a good solution... :(
Thank you very much in advance!
Code the if statement you show using a switch block, trigger using a logical <70, if false loop last output back using zero hold, you will still get output each time step but it will just be the last point you caught. I assume this is a discrete sim with output set to hold.

Machine Learning (MATLAB) - Neural network is saved as 'struct' instead of 'network' in checkpoint

I am training a neural network to learn a function. Everything is going great so far.
I have input matrix of 4x10000 and output matrix of 3x10000. I have much more data points than 10000. But not all of them can be fit at once so I have decided to feed pack of 10000-10000 data points and train same neural network on it.
There are three layers and 7 units in hidden layer.
So what I do is, I train the network with 10000 data points randomly and then again train on another random 10000 data points and so on.
So for this I store CheckPoints (in-built functionality of neural net toolkit). But what happens is that the network, which is being trained, is stored as struct in CheckPoints rather than network type itself. So when I load the checkpoint next time I run the program, it shows error something as below.
Undefined function 'train' for input arguments of type 'struct'
I am using fitnet network.
% Create a Fitting Network
hiddenLayerSize = 7;
net = fitnet(hiddenLayerSize,'trainlm');
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 60/100;
net.divideParam.valRatio = 20/100;
net.divideParam.testRatio = 20/100;
load('Highlights_Checkpoint.mat');
existanceOfCheckpoint = exist('checkpoint', 'var');
if existanceOfCheckpoint==0
else
net = (checkpoint.net);
end
% Train the Network
[net,tr] = train(net,x,t,'useParallel', 'yes','showResources','yes', 'CheckpointFile','Highlights_Checkpoint.mat');
Well solution to this problem was quite easy.
All I had to do was the following:
net = network(checkpoint.net);
And all was set. :D

How to use SVM in Matlab?

I am new to Matlab. Is there any sample code for classifying some data (with 41 features) with a SVM and then visualize the result? I want to classify a data set (which has five classes) using the SVM method.
I read the "A Practical Guide to Support Vector Classication" article and I saw some examples. My dataset is kdd99. I wrote the following code:
%% Load Data
[data,colNames] = xlsread('TarainingDataset.xls');
groups = ismember(colNames(:,42),'normal.');
TrainInputs = data;
TrainTargets = groups;
%% Design SVM
C = 100;
svmstruct = svmtrain(TrainInputs,TrainTargets,...
'boxconstraint',C,...
'kernel_function','rbf',...
'rbf_sigma',0.5,...
'showplot','false');
%% Test SVM
[dataTset,colNamesTest] = xlsread('TestDataset.xls');
TestInputs = dataTset;
groups = ismember(colNamesTest(:,42),'normal.');
TestOutputs = svmclassify(svmstruct,TestInputs,'showplot','false');
but I don't know that how to get accuracy or mse of my classification, and I use showplot in my svmclassify but when is true, I get this warning:
The display option can only plot 2D training data
Could anyone please help me?
I recommend you to use another SVM toolbox,libsvm. The link is as follow:
http://www.csie.ntu.edu.tw/~cjlin/libsvm/
After adding it to the path of matlab, you can train and use you model like this:
model=svmtrain(train_label,train_feature,'-c 1 -g 0.07 -h 0');
% the parameters can be modified
[label, accuracy, probablity]=svmpredict(test_label,test_feaure,model);
train_label must be a vector,if there are more than two kinds of input(0/1),it will be an nSVM automatically.
train_feature is n*L matrix for n samples. You'd better preprocess the feature before using it. In the test part, they should be preprocess in the same way.
The accuracy you want will be showed when test is finished, but it's only for the whole dataset.
If you need the accuracy for positive and negative samples separately, you still should calculate by yourself using the label predicted.
Hope this will help you!
Your feature space has 41 dimensions, plotting more that 3 dimensions is impossible.
In order to better understand your data and the way SVM works is to begin with a linear SVM. This tybe of SVM is interpretable, which means that each of your 41 features has a weight (or 'importance') associated with it after training. You can then use plot3() with your data on 3 of the 'best' features from the linear svm. Note how well your data is separated with those features and choose a basis function and other parameters accordingly.

matlab neural network gradient descent and mean square error

I want to know how grdient descent algorithm works on matlab network training and how MSE is calculated - I have my own app but it doesnt work as the matlab nn and I want to know why.
My algorithm looks like this:
foreach epoch
gradient_vector = 0 // this is a vector
rmse = 0
foreach sample in data set
output = CalculateForward(sample.input)
error = sample.target - output
rmse += DotProduct(error,error)
gradient_part = CalculateBackward(error)
gradient_vector += (gradient_part / number_of_samples)
end
network.AddToWeights( gradient_vector * learning_rate)
rmse = sqrt(rmse/number_of_samples)
end
I it something similar what matlab does?
It appears close to what MATLAB does, but keep in mind that the toolbox is designed for a broad base of applications. Your algorithm gives each data entry once to the network once per epoch. Matlab's toolbox can present the data multiple times per epoch, update multiple times per epoch, and can update in a number of ways. I assure you that your exact method can be duplicated with the existing matlab toolbox, but with a very specific setting, which can be found by digging around in the help files for the neural network you're using. Some of them may be closer to what you're doing than others, so be discerning. Good luck!