artificial neural network issue : Error using * and .*

조회 수: 2 (최근 30일)
MiMad
MiMad 2016년 3월 20일
답변: Greg Heath 2016년 3월 21일
hello, What's going with my code : l got back this error : *Error using * Inner matrix dimensions must agree. Error in ann_classifier (line 48) hidden_output = [1; sigmf(W1'*input_x, [beta 0])];
and when l replace * by .* l go the same error:
Error using .*
here is my code :
% load training set and testing set
clear all;
train_set = loadMNISTImages('/home/anelmad/Desktop/TER/mnist_ml2/MNIST_digit_recognition-master/load_data/train-images.idx3-ubyte');
train_label = loadMNISTLabels('/home/anelmad/Desktop/TER/mnist_ml2/MNIST_digit_recognition-master/load_data/train-labels.idx1-ubyte');
test_set = loadMNISTImages('/home/anelmad/Desktop/TER/mnist_ml2/MNIST_digit_recognition-master/load_data/t10k-images.idx3-ubyte');
test_label = loadMNISTLabels('/home/anelmad/Desktop/TER/mnist_ml2/MNIST_digit_recognition-master/load_data/t10k-labels.idx1-ubyte');
% parameter setting
alpha = 0.1; % learning rate
beta = 0.01; % scaling factor for sigmoid function
train_size = size(train_set);
N = train_size(1); % number of training samples
D = train_size(2); % dimension of feature vector
n_hidden = 300; % number of hidden layer units
K = 10; % number of output layer units
% initialize all weights between -1 and 1
W1 = 2*rand(1+D, n_hidden)-1; % weight matrix from input layer to hidden layer
W2 = 2*rand(1+n_hidden, K)-1; % weight matrix from hidden layer to ouput layer
max_iter = 100; % number of iterations
Y = eye(K); % output vector
% training
for i=1:max_iter
disp([num2str(i), ' iteration']);
for j=1:N
% propagate the input forward through the network
input_x = [1; train_set(j, :)'];
hidden_output = [1;sigmf(W1'*input_x, [beta 0])];
output = sigmf(W2'*hidden_output, [beta 0]);
% propagate the error backward through the network
% compute the error of output unit c
delta_c = (output-Y(:,train_label(j)+1)).*output.*(1-output);
% compute the error of hidden unit h
delta_h = (W2*delta_c).*(hidden_output).*(1-hidden_output);
delta_h = delta_h(2:end);
% update weight matrix
W1 = W1 - alpha*(input_x*delta_h');
W2 = W2 - alpha*(hidden_output*delta_c');
end
end
% testing
test_size = size(test_set);
num_correct = 0;
for i=1:test_size(1)
input_x = [1; test_set(i,:)'];
hidden_output = [1; sigmf(W1'*input_x, [beta 0])];
output = sigmf(W2'*hidden_output, [beta 0]);
[max_unit, max_idx] = max(output);
if(max_idx == test_label(i)+1)
num_correct = num_correct + 1;
end
end
% computing accuracy
accuracy = num_correct/test_size(1);
thank you
  댓글 수: 2
MiMad
MiMad 2016년 3월 20일
l tried .* instead of * the same error
MiMad
MiMad 2016년 3월 21일
both .* and * don't work

댓글을 달려면 로그인하십시오.

채택된 답변

Greg Heath
Greg Heath 2016년 3월 21일
If you get an inner dimension multiplication error for
C = A*B
Then check the inner dimensions via
whos A B
You can use * only if size(A,2) = size(B,1), i.e., the inner dimensions are equal.
Hope this helps.
Thank you for formally accepting my answer
P.S. Why didn't you point out which equation is number 48?

추가 답변 (1개)

Greg Heath
Greg Heath 2016년 3월 21일
The answer is relatively simple:
If the dimensions do not agree, then what are they
whos
and what should they be?
Hope this helps
Thank you for formally accepting my answer
Greg
  댓글 수: 1
MiMad
MiMad 2016년 3월 21일
l don't understand how to fix the problem with whos ?. All what l know is that : size(test_point) = 1 10000 size(train-point)= 1 60000
Cheers

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by