using same for loop but with different input

1 view (last 30 days)
hello,
I am working on a basic machine learning algorithm.
here are some of my code
this code works, but i want to run this code with different alpha value (learning rate)
for example, i want to know the different result according to the different alpha values (alpha = [0.1:0.1:1])
i know that i can manually change the alpha value each time to get the each results
but i want to know if there is any other method so that i dont have to change alpha value each time.
trn = 10000; % training pattern 갯수
ten = 10000; % testing pattern 갯수
echo = 10; % 학습반복 횟수
rate = 1;
w = (rand(784,10)-0.5)*2; % -1 ~ 1 사이의 weight값 random하게 생성
s = zeros(1,10); % Integration 부분
o = zeros(1,10); % Activation 부분
alpha = 0.1; % learning rate
count = 0;
%-------------------------------------------------------Learing--------------------------------------------------
for z=1:echo
for j=1:trn
i=image(j,:); % j-th pattern
l=label(j,:); % j-th label
s=i*w; % input*weight 의 합
i_t = i.'; % transposd of i
o=s;
y = 1./(1+exp(-1.*o)); % Sigmoid Activation function 적용
mean_square = (y - l).^2; % mean_square 적용
del_y = y.*(1-y);
del_mean = 2.*(y-l);
del_w = i_t.* del_y .* del_mean;
w=w-(alpha.*del_w);
end
end
%---------------------------------------------------Testing-----------------------------------------------
for k=1:ten
i=image1(k,:);
l=label1(k,:);
s=i*w; % input*weight 의 합
o=s;
y = 1./(1+exp(-1.*o)); % Sigmoid Activation function 적용
h=max(y);
y(y < h) = 0;
y(y == h) = 1;
if y == l
count=count+1;
end
end

Answers (1)

Anmol Dhiman
Anmol Dhiman on 3 Mar 2020
According to my understanding you can save you code in one function, for eg myfunction. You can create two arrays, one for alpha and one for variable w (which is determined using alpha). You can call the myfunction in a for loop.
alpha = [0:0.1:1]
w= []
for i=1:length(alpha)
w_temp = myfunction(a);
w(i) = w_temp;
end
--
w = myFunction(a)
%your code with alpha =a;
end
Please share some data in case the above approach did not work.

Community Treasure Hunt

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

Start Hunting!