1
\$\begingroup\$

enter image description here

Solution:

Generate Training Set

traincol1 = linspace(0.1, 15, 40)';
eps = (0.2*rand(40,1)) - 0.1;
traincol2 = sin(traincol1)./traincol1 - eps;
train = [traincol1 traincol2];
save('snn_a.txt','train');
save('snn_a.mat','train');

Generate Test Set

testcol1 = linspace(0.1, 15, 400)';
eps = (0.2*rand(400,1)) - 0.1;
testcol2 = sin(testcol1)./testcol1 - eps;
test = [testcol1 testcol2];
save('snn_b.txt','test');
save('snn_b.mat','test');

Training the Neural Network

function net = train_net(trainingset, hidden_neurons)    
% Parameters:     
% train_set:      
% labels - y     
% hidden_neurons_count:      
% Return value:     
% net – object representing a neural network    
% initialization     
% hidden neuron activation function- tanh,     
% output neuron activation - linear     

net=newff(trainingset(:, 1)', trainingset(:, 2)',hidden_neurons,
{'tansig', 'purelin'},'trainlm'); 
rand('state',sum(100*clock));      %random numbers generator initialization     
net=init(net);                     %weights initialization     
net.trainParam.goal = 0.01;        %stop- mse criterion     
net.trainParam.epochs = 400;       %number of epochs iterations     
net=train(net,trainingset(:, 1)', trainingset(:, 2)'); %network training 

Main Program

% input data area
load('snn_a.mat');
load('snn_b.mat');
hidden_neurons = 4;
% net training
net = train_net(train, hidden_neurons);
% assigning results
resulttrain = net(train(:, 1)')';
resulttest = net(test(:, 1)')';
% drawing
hold on
sn = @(x) sin(x) / x;
fplot(sn, [0, 15],'g');
plot(train(:, 1), resulttrain, 'r');
legend('Original function', ' Result')
hold off
% print mse results
mse(net, train(:, 2)', resulttrain')
mse(net, test(:, 2)', resulttest')

Is this code correctly implemented?

Are there any hidden bugs?

How can it be improved?

\$\endgroup\$
  • \$\begingroup\$ It is not clear to me from your question what, specifically, you want feedback / review about. First of all, does this code work? If it does, great -- but what aspects of code are you looking for comment on? Performance? Bugs? Architecture? \$\endgroup\$ – allquixotic Dec 15 '16 at 0:25
  • \$\begingroup\$ @allquixotic, Is this code correctly implemented? Are there any hidden bugs? How can it be improved? \$\endgroup\$ – user3804 Dec 15 '16 at 0:42
  • \$\begingroup\$ Why did you tag this both Matlab and octave? I'm assuming you're using the neural network toolbox of Matlab, not an octave toolbox? \$\endgroup\$ – Stewie Griffin Dec 15 '16 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy