Unrecognized function or variable 'a'.

Error in training (line 16) weight = sgd(weight, x,a ,b, y1);
this is my code
simOut = sim('TestSim23.slx');
x1 = simOut.get('i1');
x2 = simOut.get('d1');
x3 = simOut.get('p1');
x = [x1;x2;x3];
y1 = simOut.get('y1');
weight = 2*rand(1,3) - 1;
for k = 1:1000
weight = sgd(weight, x,a ,b, y1);
end
save('trained_network.mat')
and here is the function sgd
function weight = sgd(weight, x, a, b, y1)
alpha(1) = 0.7;
alpha(2) = 0.6;
alpha(3) = 0.4;
beta = 0.01;
gamma = 0.2;
N = 3;
for k = 1:N
t_x(k) = x(k, :)';
d = y1(k);
v(k) = a(k)*y(k-1) + (1-a(k))*v(k-1);
w_sum = weight*t_x + b(k)*v(k-1);
y = Sigmoid(w_sum);
error = d - y;
delta = y*(1-y)*error;
dweight = alpha*delta*t_x;
d_a = beta*delta*b(k)*(y(k-1) - v(k-1));
d_b = gamma*delta*v(k);
weight(1) = weight(1) - dweight(1);
weight(2) = weight(2) - dweight(2);
weight(3) = weight(3) - dweight(3);
a(k) = a(k) - d_a(k);
b(k) = b(k) - d_b(k);
end
end

1 Comment

... Yes?
We have no idea what value a should have for you or where you should be getting it from. We can tell from the code that it needs to be a vector of length at least 3, but we cannot tell what the initial values should be.
====
a(k) = a(k) - d_a(k);
Inside your for loop, you use a(k) but no other location of a -- for example you do not use all of a and you do not use a(k-1) . You also do not return a or b from the function. It is therefore a waste of time to change a(k) and b(k) since you are not ever going to use the changed version. But since you appear to be in some kind of feedback loop, perhaps you should be returning the updated a and b ?

Sign in to comment.

Answers (1)

The error is clear, you have to define a value for a.
simOut = sim('TestSim23.slx');
x1 = simOut.get('i1');
x2 = simOut.get('d1');
x3 = simOut.get('p1');
x = [x1;x2;x3];
y1 = simOut.get('y1');
a = %%%%% ; % define your value here
weight = 2*rand(1,3) - 1;
for k = 1:1000
weight = sgd(weight, x,a ,b, y1);
end
save('trained_network.mat')

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Products

Asked:

on 24 Jul 2020

Commented:

on 24 Jul 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!