I have the following code while running i'm getting parse error at 'clear':usage might be invalid MATLAB syntax. I'm unable to fix this error could anyone help me..??
1 view (last 30 days)
Show older comments
function nmse_calc = nmse(wbr, net, input, target)
% wbr contains the weights and biases vector in row vector form as
% passed to it by the genetic algorithm. This must be transposed
% when being set as the weights and biases vector for the network.
% net must be configured to the sizes of input and target
% input I x N matrix of N I-dimensional "I"nput vectors
% target O x N matrix of N O-dimensional "O"utput target vectors
% vart1 = mean(var(target',1)); % Reference MSE
net = setwb( net, wbr' );
output = net( input );
nmse_calc = mean( ( target(:) - output(:) ).^2 ) / vart1;
end
clear all,
close all,
clc,
plt=0;
tic
[ x t ] = simplefit_dataset;
[ I N ] = size(x);%[ 1 94 ]
[O N ] = size(t);%[ 1 94 ]
% Reference MSE: Average Target Variance
vart1 = mean(var(t',1)) ;% 8.3378
% No. of Training Equations
Ntrneq = N*O; % 94
% No. of unknown weights for H hidden Nodes
% Nw = ( I +1 )*H + (H +1)*O
% No Overfitting Upper Bound for H
% Nw < Ntrneq <==> H < Hnoub
Hnoub = ( Ntrneq - O ) / ( I + O + 1 ); % 31
% Simplefit Data Plot: Target vs Input
plt = plt+1;
figure( plt ), plot( x, t ),
% No. of Smooth Plot Local Extrema
NLE = 4;
% Hidden Node Bounds
% NLE <= H < Hnoub % 4 <= H <= 31
% Hidden Node Choice
H = NLE;
Nw = ( I + 1)*H + ( H +1)*O; % 4, 13
% Create Regression/Curve-Fitting Neural Network:
net = fitnet( H );
net.divideFcn = 'dividetrain'; % Nval = Ntst = 0
% Configure the Net for the Simplefit Dataset
s = rng( 'default' );
net = configure( net, x, t );
% Initial Weights and Normalized MSE
wbr = getwb( net )'; % row vector
NMSE0 = mse( t - net( x ) ) / vart1;% 2.7851
% Create handle to the NMSE function,
hand = @(wbr) nmse( wbr, net, x, t );
% Set the Genetic Algorithm tolerance for minimum change in fitness function % before terminating algorithm to 1e-4 and display each iteration's results.
gaopts = gaoptimset( 'TolFun', 1e-4, 'display', 'iter' );
[ wbopt, errga ] = ga( hand, Nw, gaopts );
% Optimization terminated: average change in the fitness value less than
% options.TolFun.
% Best Mean Stall
% Generation f-count f(x) f(x) Generations
% 146 29400 0.1345 1777 29
errga = errga; % 0.13448
wbopt = wbopt;
% wbopt = [13.532 -7.440 -24.08 -8.759 12.595 5.777 -21.553
% 20.281 4.983 2.095 8.649 -1.585 -0.522 ]
net = setwb( net, wbopt');
NMSE = mse( t - net( x ) ) / vart1; % 0.13448
totaltime = toc; % 690 sec (11.5 min)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FOR COMPARISON
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all, close all;
clc, plt = 0; tic
[ x t ] = simplefit_dataset;
[ I N ] = size(x); % [ 1 94 ]
[O N ] = size(t); % [ 1 94 ]
% Reference MSE: Average Target Variance
vart1 = mean(var(t',1)); % 8.3378
% No. of Training Equations
Ntrneq = N*O; % 94
% No. of unknown weights for H hidden Nodes
% Nw = ( I +1 )*H + (H +1)*O
% No Overfitting Upper Bound for H
% Nw < Ntrneq <==> H < Hnoub
Hnoub = ( Ntrneq - O ) /( I + O + 1 ); % 31
% Simplefit Data Plot: Target vs Input
plt = plt+1;
figure( plt ), plot( x, t ),
% No. of Smooth Plot Local Extrema
NLE = 4 ;
% Hidden Node Bounds
% NLE <= H < Hnoub % 4 <= H <= 31
% Hidden Node Choice
H = NLE;
Nw = ( I + 1)*H + ( H +1)*O; % 4, 13
% Create Regression/Curve-Fitting Neural Network:
net = fitnet( H );
net.divideFcn = 'dividetrain'; % Nval = Ntst = 0
% Configure the Net for the Simplefit Dataset
s = rng( 'default' );
net = configure( net, x, t );
net = train(net,x,t);
NMSE = mse( t - net( x ) ) / vart1; % 3.274e-4
totaltime = toc; % 11 sec
2 Comments
Accepted Answer
Greg Heath
on 21 Mar 2016
You did not copy the original code correctly:
1. The function nmse should be stored in a separate file
2. You divided the first line into separate lines ending in commas
Hope this helps.
Greg
0 Comments
More Answers (3)
Greg Heath
on 19 Mar 2016
The Code for the function nmse (above the clear command) has to saved in a separate file.
2 Comments
Stephen23
on 19 Mar 2016
Edited: Stephen23
on 19 Mar 2016
@Priya Dharshini: the error message is quite clear: you are not providing enough input arguments to the function. The solution is also simple: provide all of the input arguments.
function nmse_calc = nmse(wbr, net, input, target)
There are four input arguments. How many acre you calling the function with?
Walter Roberson
on 19 Mar 2016
You should not end a line with a comma unless the comma is commented out, or unless the comma occurs inside a "[" that has a matching "]" on the later line.
Wrong:
clear all,
Acceptable:
clear all %,
Better:
clear all
Best:
%we had a clear all here but we realized it was wrong and destructive
Which is to say, you should never have a "clear all" in your code. Having "clear all" in code is rather like Wile E. Coyote exploding the bridge underneath himself and expecting to stay safe in mid-air as long as he does not look down.
2 Comments
Steven Lord
on 20 Mar 2016
Commas work just fine when separating commands if you want one of them to display something in the Command Window.
x = 1; y = 2; z = x+y; % None of these will display anything
x = 1, y = 2, z = x+y, % Each of these will display
USUALLY you're going to want to use semicolons to suppress display as in the first line of the example above, but occasionally you want to see the display.
Steven Lord
on 20 Mar 2016
You have an extra END in your code immediately before the CLEAR call. That ends the main function in the file, so the CLEAR call (and everything after it) is outside the function. That is invalid, and when the MATLAB parser attempts to figure out what to do with those lines it can't. Either eliminate that END (if everything in that file is supposed to be part of one function) or move everything after that END into a separate file (if the later commands are not supposed to be part of that function) and your code should "work".
The reason I put "work" in quotes is because it's usually a Bad Idea to include "clear all" in a function. That will clear all the variables that exist at that time in the function workspace, which includes the input arguments with which you called the function! It's a little more acceptable in a script file, but I would still avoid it unless I was sure I absolutely needed to use it. I would probably eliminate the "clc" and "close all" calls as well. The latter two aren't as big a deal, but it's a little annoying when I run a function that I don't expect to clear the Command Window or close all my figures and the function does one or both of those operations.
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!