MATLAB Answers

Can change the output.

2 views (last 30 days)
Ali Asghar
Ali Asghar on 18 Dec 2019
Edited: Adam Danz on 18 Dec 2019
Dear all
I can change the output (D)
clear all, clc
rng(3);
X = zeros(5, 5, 5);
X(:, :, 1) = [ 0 1 1 0 0;
0 0 1 0 0;
0 0 1 0 0;
0 0 1 0 0;
0 1 1 1 0
];
X(:, :, 2) = [ 1 1 1 1 0;
0 0 0 0 1;
0 1 1 1 0;
1 0 0 0 0;
1 1 1 1 1
];
X(:, :, 3) = [ 1 1 1 1 0;
0 0 0 0 1;
0 1 1 1 0;
0 0 0 0 1;
1 1 1 1 0
];
X(:, :, 4) = [ 0 0 0 1 0;
0 0 1 1 0;
0 1 0 1 0;
1 1 1 1 1;
0 0 0 1 0
];
X(:, :, 5) = [ 1 1 1 1 1;
1 0 0 0 0;
1 1 1 1 0;
0 0 0 0 1;
1 1 1 1 0
];
D = [ 1 0 0 0 0; % what to write D as [1 2 3 4 5; 0 1 2 3 4; etc]
0 1 0 0 0;
0 0 1 0 0;
0 0 0 1 0;
0 0 0 0 1
];
W1 = 2*rand(50, 25) - 1;
W2 = 2*rand( 5, 50) - 1;
for epoch = 1:10000 % train
[W1 W2] = MultiClass(W1, W2, X, D);
end
N = 5; % inference
for k = 1:N
x = reshape(X(:, :, k), 25, 1);
v1 = W1*x;
y1 = Sigmoid(v1);
v = W2*y1;
y = Softmax(v)
end
When i rewrite the D it show error of NaN....
FUNCTIONS
function [W1, W2] = MultiClass(W1, W2, X, D)
alpha = 0.9;
N = 5;
for k = 1:N
x = reshape(X(:, :, k), 25, 1);
d = D(k, :)';
v1 = W1*x;
y1 = Sigmoid(v1);
v = W2*y1;
y = Softmax(v);
e = d - y;
delta = e;
e1 = W2'*delta;
delta1 = y1.*(1-y1).*e1;
dW1 = alpha*delta1*x';
W1 = W1 + dW1;
dW2 = alpha*delta*y1';
W2 = W2 + dW2;
end
end
function y = Sigmoid(x)
y = 1 ./ (1 + exp(-x));
end
function y = Softmax(x)
ex = exp(x);
y = ex / sum(ex);
end

  1 Comment

Adam Danz
Adam Danz on 18 Dec 2019
I edited your question to format the code. In the future, please format your code in all questions, comments, and answers (I see you've got some experience here so that should be clear by now).
"When i rewrite the D it show error of NaN.... "
I run your code without error.

Sign in to comment.

Answers (0)

Tags

Products