clear all;, close all;
function g = vl_sigmoid(z)
%SIGMOID Compute sigmoid functoon
%   J = SIGMOID(z) computes the sigmoid of z.

g = 1.0 ./ (1.0 + exp(-z));
end
c=2; % number of classes
m=128; % number of hidden layers
ite_max=1000;
J=zeros(ite_max,2);

alp=0.05;
x1=[-4:.1:4];, x2=[-4:.1:4];

x=dlmread('sin_data.txt');
t=dlmread('label.txt');

[d n]=size(x);
x=[x;ones(1,n)];
[d n]=size(x);

% weights
v=randn(m,c);
w=randn(d,m);

dv=zeros(size(v));
dw=zeros(size(w));

for loop = 1 : ite_max
	index=randperm(n);
	for ii=1 : n
		xx=[x(:,index(ii))];
		%% -- forword --
		z=vl_sigmoid(w'*xx);
		y=vl_sigmoid(v'*z);
		%% -- back --
		eps=(y-t(:,index(ii))).*y.*(1-y);
		J(loop,2)=J(loop,2)+norm(y-t(:,index(ii))).^2;
		%% modifying v
		dv=z*eps';
		v=v-alp.*dv;
		%% modifying w
		delta=(v*eps).*z.*(1-z);
		dw=xx*delta';
		w=w-alp.*dw;
	end
	J(loop,1)=loop;
	fprintf(2,'m=%d ite=%d J=%f\n',m,loop,J(loop,2));
end

f=zeros(length(x1),length(x2));

for xx1 = 1 : length(x1)
	for xx2 = 1 : length(x2)
		xx=[x1(xx1);x2(xx2);1];
		z=vl_sigmoid(w'*xx);
		y=vl_sigmoid(v'*z);
		f(xx2,xx1)=y(1)-y(2);
	end
end

figure(1),clf,hold on,box on
figure(1),plot(x(1,find(t(1,:)==1)),x(2,find(t(1,:)==1)),'mo');
figure(1),plot(x(1,find(t(2,:)==1)),x(2,find(t(2,:)==1)),'co');
figure(1),plot(2.*sin(x2),x2,'g--','LineWidth',1.5);
figure(1),contour(x1,x2,f,[-0.0001 0.0001],'LineWidth',1.5);
axis square

%filename=sprintf('J%d.txt',m);
%dlmwrite(filename,J,'\t');
