%%%%%%
%%%%%% Learning by a Tensor Machine
%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%% Preparation %%%%%%%%%%%%%%%%%%%%
clear;
clf;
close all hidden;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MATLAB=1;
if(MATLAB==1)
opengl software; %%%%% For matlab drawing case
end
%%%%%%%%%%%%%%%%%%%%%% Excersize %%%%%%%%%%%%%%%%%%%%%%%
n=100; %%% n=100, 1000, Number of Training samples
CASE=1; %%% CASE= 1, 2, Case of a true function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NOISE=0.1; %%% Output noise
Hyperparameter=0.0001;
%%%%%%%%%%%%%%%%%%%%% Definition of a True function %%%%%%%%%%%%%%%%
if(CASE==1)
g=@(x1,x2)(x1.*x2);
end
if(CASE==2)
g=@(x1,x2)(2*exp(-3*(x1.^2+x2.^2)));
end
%%%%%%%%%%%%%%%%%%%%%%%%%% Figure 1 : True function %%%%%%%%%%%%%%%%%%%%%%
D=-1:0.05:1;
[X1 X2]=meshgrid(D);
Y=g(X1,X2);
figure(1);
mesh(D,D,Y);
title('True function');
%%%%%%%%%%%%%%%%%%%%% Figure2 : Training Samples %%%%%%%%%%%%%%%%%
x1=2*rand(1,n)-1;
x2=2*rand(1,n)-1;
y=g(x1,x2)+NOISE*randn(1,n);
figure(2);
plot3(x1,x2,y,'b.');
title('Training samples');
%%%%%%%%%%%%%%%%%%%%%%%%%%%% Definition of a Tensor Learning Machine %%%%%%%%%%%%%%
ee=@(m,x1,x2)(x1.^(mod(m-1,4)).*x2.^(floor((m-1)/4)));
wee=@(m,w,x1,x2)(w(m)*ee(m,x1,x2));
f=@(x1,x2,w)(wee(1,w,x1,x2)+wee(2,w,x1,x2)+wee(3,w,x1,x2)+wee(4,w,x1,x2)+wee(5,w,x1,x2)...
+wee(6,w,x1,x2)+wee(7,w,x1,x2)+wee(8,w,x1,x2)+wee(9,w,x1,x2)+wee(10,w,x1,x2)+wee(11,w,x1,x2)...
+wee(12,w,x1,x2)+wee(13,w,x1,x2)+wee(14,w,x1,x2)+wee(15,w,x1,x2)+wee(16,w,x1,x2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Learning Mathematics %%%%%%%%%%%
%%% f= sum_k w(k)*e_k(x)
%%% E = sum_i { sum_k w(k)*e_k(x(i))-y(i) }^2
%%% dE/dw(j)=sum_i (sum_k w(k)*e_k(x(i))-y(i))*e_j(x(i))=0
%%% sum_k w(k)*{ sum_i e_k(x(i))*e_j(x(i)) } =sum_i y(i)*e_j(x(i))
%%% A(k,j)=sum_i e_k(x(i))*e_j(x(i))
%%% b(j)=sum_i y(i)*e_j(x(i))
%%% w=A^{-1}*b
%%%%%%%%%%%%%%%%%%%%%% Learning Process %%%%%%%%%%%%%%%%%%%%%%%
for j=1:1:16
for k=1:1:16
A(j,k)=sum(ee(j,x1,x2).*ee(k,x1,x2));
end
end
for j=1:1:16
B(j,1)=sum(y.*ee(j,x1,x2));
end
ww=inv(A+Hyperparameter)*B;
%%%%%%%%%%%%%%%%%%%% Figure 3: Output of Trained Learning Machine %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Y=f(X1,X2,ww);
figure(3);
mesh(D,D,Y);
title('Trained Learning Machine');
%%%%%%%%%%%%%%%%%%% Figure 4: Generalization Error %%%%%%%%%%%%%%%%%%%%
Z=abs(f(X1,X2,ww)-g(X1,X2));
figure(4);
mesh(D,D,Z);
title('Generalization error for each point');
fprintf('CASE=%g, n=%g\n',CASE,n);
fprintf('Training Error=%f\n',mean((f(x1,x2,ww)-y).^2));
fprintf('Generalization Error=%f\n',NOISE^2+sum(sum(Z.^2))/41^2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%