Can change the output.

1 次查看(过去 30 天)
Ali Asghar
Ali Asghar 2019-12-18
编辑: Adam Danz 2019-12-18
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 个评论
Adam Danz
Adam Danz 2019-12-18
编辑:Adam Danz 2019-12-18
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by