
trainnetwork error, how to solve it?
6 views (last 30 days)
Show older comments
lgraph = layerGraph();
tempLayers = [
sequenceInputLayer(1,'Name','seqinput','Normalization','zscore')
convolution1dLayer(7,32,"Name","conv1d","Padding","same")
maxPooling1dLayer(3,"Name","maxpool1d","Padding","same")
leakyReluLayer(0.6,"Name","leakyrelu_1")
fullyConnectedLayer(10,'Name','branch1')
];
lgraph = addLayers(lgraph,tempLayers);
layers = [
featureInputLayer(4,'Normalization','zscore')
functionLayer(@(X) cbtocbt(X),Formattable=true)
fullyConnectedLayer(10,'Name','featurelast')]
dlnet = dlnetwork(layers);
lgraph = addLayers(lgraph,layers);
tempLayers = [
additionLayer(2,"Name","addition")
fullyConnectedLayer(50,"Name","fc_1")
leakyReluLayer(0.6)
fullyConnectedLayer(1,"Name","fc")
regressionLayer("Name","regressionoutput")];
lgraph = addLayers(lgraph,tempLayers);
clear tempLayers;
lgraph = connectLayers(lgraph,"branch1","addition/in1");
lgraph = connectLayers(lgraph,"featurelast","addition/in2");
analyzeNetwork(lgraph);
function Y = cbtocbt(X)
idxC = finddim(X,"C");
idxB = finddim(X,"B");
sizeS = size(X,idxS);
sizeC = size(X,idxC);
if ~isempty(idxB)
numChannels = sizeC;
sizeB = size(X,idxB);
X = reshape(X,[numChannels sizeB 1]);
Y = dlarray(X,"CBT");
end
end
and I got this error message
"다음 사용 중 오류가 발생함: trainNetwork
행렬 곱셈의 차원이 잘못되었습니다. 첫 번째 행렬의 열 개수가 두 번째 행렬의 행 개수와 일치하는지 확인하십시오. 행렬의 각 요소에 대해 개별적으로 연산을 수행하려면 요소별 곱셈 연산에 TIMES(.*)를 사용하십시오.
0 Comments
Accepted Answer
Angelo Yeo
on 22 Jan 2024
The issue resides in your "addition" layer since this layer cannot back-propagate to "featurelast".
Let's think about the size of input and output around "addition" layer.

(1) In forward phase, in "addition" layer, output from "branch1" and "featurelast" are added. Hence, it would be something like this.
addition = rand(10, 100, 944) + rand(10, 100);
size(addition)
Although the dimension of the two matrices does not match, it is added thanks to MATLAB's implicit expansion.
(2) In backward phase, from "addition" to "featurelast", a derivative input from "addition" has size of "10x100x944" just like we have seen in (1). As you remember, the deriviative of a weight of a hidden layer can be described as "derivative input x transpose of input". Do you remember the size of the input for layer "addition"? It was "10x100".
In MATLAB, when multiplying multiple-dimensioned matrices, they are folded. So, what happens is:
dZf = rand(10, 100*944); % folded dZ temporarily filled with random entities
Xf = rand(10, 100); % folded X temporarily filled with random entities
dW = dZf * Xf' % derivative of weight
Again, the issue resides in the structure of your neural network. The "addition" layer cannot back-propagate to your "featurelast" layer.
4 Comments
Angelo Yeo
on 22 Jan 2024
(1) I discussed the issue with our development team, and it looks like a bug of additionLayer. To work around the issue, you can replace the additionLayer with something like below:
functionLayer(@(x,y) x+ y, Name = "addition")
I am attaching a full script with the workaround.
(2) You can reach out to our technical support (TS) team in order to verify the possibility of bug. Please follow the guidance below to contact TS.
--------------------------------------------------------
1) 매스웍스 홈페이지에서 로그인 후, 2) https://www.mathworks.co.kr/support/contact_us/index.html 로 이동 3) 서비스 요청하기 > 4) 기술 지원:... > 5) "제품 관련 도움, 버그, 제안 또는 문서 오류" > 빨간색 별표 항목을 모두 정확하게 채워주시고 문의하실 수 있습니다.
제일 간단하게는 MATLAB 메뉴 창의 지원 요청 (Request Support) 버튼을 이용하실 수 있습니다.
--------------------------------------------------------
More Answers (0)
See Also
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!