MLreview2013
Jump to navigation
Jump to search
==Basic restricted Boltzmann machine to learn letter patterns==
clear; nh=100; nepochs=150; lrate=0.01;
%load data from text file and rearrange into matrix
load pattern1.txt;
letters = permute( reshape( pattern1, [12 26 13]), [1 3 2]);
%%train rbm for nepochs presentations of the 26 letters
input = reshape(letters,[12*13 26])
vb =zeros(12*13,1); hb =zeros(nh,1); w =.1*randn(nh,12*13);
figure; hold on;
xlabel 'epoch'; ylabel 'error'; xlim([0 nepochs]);
for epoch=1:nepochs;
err=0;
for i=1:26
%Sample hidden units given input, then reconstruct.
v = input(:,i);
h = 1./(1 + exp(-(w *v + hb))); %sigmoidal activation
hs= h > rand(nh,1); %probabilistic sampling
vr= 1./(1 + exp(-(w'*hs+ vb))); %input reconstruction
hr= 1./(1 + exp(-(w *vr+ hb))); %hidden reconstruction
%Contrastive Divergence rule: dw ~ h*v - hr*vr
dw = lrate*(h*v'-hr*vr'); w = w +dw;
dvb = lrate*( v - vr ); vb= vb+dvb;
dhb = lrate*( h - hr ); hb= hb+dhb;
err = err + sum( (v-vr).^2 ); %reconstruction error
end
plot( epoch, err/(12*13*26), '.'); drawnow;%figure output
end
%%plot reconstructions of noisy letters
r = randomFlipMatrix(round(.2*12*13)); %(20% of bits flipped)
noisy_letters = abs(letters - reshape(r,[12 13 26]));
recon = reshape(noisy_letters, 12*13, 26); %put data in matrix
recon=recon(:,1:5); %only plot first 10
figure; set(gcf,'Position',get(0,'screensize'));
for i=0:3
for j=1:5
subplot(3+1, 5, i*5 + j);
imagesc( reshape(recon(:,j),[12 13]) ); %plot
colormap gray; axis off; axis image;
h = 1./(1 + exp(-(w *recon(:,j) + hb))); %compute hidden
hs= h > rand(nh,1); %sample hidden
recon(:,j) = 1./(1 + exp(-(w'*hs + vb)));%compute visible
recon(:,j) = recon(:,j) > rand(12*13,1); %sample visible
end
end
function r=randomFlipMatrix(n);
% returns matrix with components 1 at n random positions
r=zeros(156,26);
for i=1:26
x=randperm(156);
r(x(1:n),i)=1;
end
An MLP with backpropagation for solving the XOR problem
clear; clf;
N_i=2; N_h=2; N_o=1;
w_h=rand(N_h,N_i)-0.5; w_o=rand(N_o,N_h)-0.5;
% training vectors (XOR)
r_i=[0 1 0 1 ; 0 0 1 1];
r_d=[0 1 1 0];
% Updating and training network with sigmoid activation function
for trial=1:10000;
% training randomly on one pattern
i=ceil(4*rand);
r_h=1./(1+exp(-w_h*r_i(:,i)));
r_o=1./(1+exp(-w_o*r_h));
d_o=(r_o.*(1-r_o)).*(r_d(:,i)-r_o);
d_h=(r_h.*(1-r_h)).*(w_o'*d_o);
w_o=w_o+0.7*(r_h*d_o')';
w_h=w_h+0.7*(r_i(:,i)*d_h')';
% test all pattern
r_o_test=1./(1+exp(-w_o*(1./(1+exp(-w_h*r_i)))));
d(trial)=0.5*sum((r_o_test-r_d).^2);
end
plot(d)
Using libsvm for classification
clear; close all; figure; hold on; axis square %% training data and training SVM r1=2+rand(300,1); a1=2*pi*rand(300,1); polar(a1,r1,'bo'); r2=randn(300,1); a2=.5*pi*rand(300,1); polar(a2,r2,'rx'); x=[r1.*cos(a1),r1.*sin(a1);r2.*cos(a2),r2.*sin(a2)]; y=[zeros(300,1);ones(300,1)]; model=svmtrain(y,x); %% test data and SVM predicition r1=2+rand(300,1); a1=2*pi*rand(300,1); r2=randn(300,1); a2=.5*pi*rand(300,1); x=[r1.*cos(a1),r1.*sin(a1);r2.*cos(a2),r2.*sin(a2)]; yp=svmpredict(y,x,model); figure; hold on; axis square [tmp,I]=sort(yp); plot(x(1:600-sum(yp),1),x(1:600-sum(yp),2),'bo'); plot(x(600-sum(yp)+1:600,1),x(600-sum(yp)+1:600,2),'rx');
Program for the chain example using policy iteration
% Chain example: Policy iteration
% Parameters
clear; N=10; P=0.8; gamma=0.9;
% Reward function
r = zeros(1,N) - 0.1; r(1)=-1; r(N)=1;
% Initiality random start policy and value function
policy = ceil(2*rand(1,N)); policy(1)=2; policy(N)=1;
Vpi = rand(1,N); Vpi(1)=r(1); Vpi(N)=r(N);
for iter=1:3
% Estimate V for this policy
for i=1:10
for s=2:N-1
snext = s-1+2*(policy(s)-1);
sother = s+1-2*(policy(s)-1);
Vpi(s) = r(s)+gamma*(P*Vpi(snext)+(1-P)*Vpi(sother));
end
end
%Updating policy
for s=2:N-1
[tmp, policy(s)] = max([Vpi(s-1),Vpi(s+1)]);
end
end
plot(Vpi);