Skip to content

Commit

Permalink
added mlp
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Feb 29, 2016
1 parent 867fa9b commit 12a91a1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 172 deletions.
28 changes: 0 additions & 28 deletions chapter05/XOR_Example.m

This file was deleted.

6 changes: 6 additions & 0 deletions chapter05/demo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
clear; close all;
h = 4;
X = [0 0 1 1;0 1 0 1];
Y = [0 1 1 0];
[model,mse] = mlp(X,Y,h);
plot(mse);
51 changes: 25 additions & 26 deletions chapter05/mlp.m
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
function model = mlp(X, Y, h)



[D,N] = size(X);
h = [h,size(Y,1)];
function [model, mse] = mlp(X, Y, h)
% Multilayer perceptron
% Input:
% X: d x n data matrix
% Y: p x n response matrix
% h: L x 1 vector specify number of hidden nodes in each layer l
% Ouput:
% model: model structure
% mse: mean square error
% Written by Mo Chen ([email protected]).
h = [size(X,1);h(:);size(Y,1)];
L = numel(h);

maxiter = 200;
eta = 1/N;

W = cell(L);
W{1} = randn(D,h(1));

for l = 2:L
W{l} = randn(h(l-1),h(l));
W = cell(L-1);
for l = 1:L-1
W{l} = randn(h(l),h(l+1));
end


Z = cell(L);

Z{1} = X;
eta = 1/size(X,2);
maxiter = 2000;
mse = zeros(1,maxiter);
for iter = 1:maxiter
% forward
Z{1} = sigmoid(W{1}'*X);
for l = 2:L
Z{l} = sigmoid(W{l}'*Z{l-1});
Z{l} = sigmoid(W{l-1}'*Z{l-1});
end
% backward
E = Y-Z{L};
df = Z{L}.*(1-Z{L});

mse(iter) = mean(dot(E(:),E(:)));
for l = L-1:-1:1
df = Z{l}.*(1-Z{l});
df = Z{l+1}.*(1-Z{l+1});
dG = df.*E;
dW = Z{l}*dG;
W = W+eta*dW;
E = (W{l}*dG);
dW = Z{l}*dG';
W{l} = W{l}+eta*dW;
E = W{l}*dG;
end
end
mse = mse(1:iter);
model.W = W;
28 changes: 0 additions & 28 deletions chapter05/runMLP.m

This file was deleted.

45 changes: 0 additions & 45 deletions chapter05/trainMLP.m

This file was deleted.

45 changes: 0 additions & 45 deletions chapter05/trainMLP2.m

This file was deleted.

0 comments on commit 12a91a1

Please sign in to comment.