trainnetwork error, how to solve it?

5 次查看(过去 30 天)
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')]
layers =
3×1 Layer array with layers: 1 '' Feature Input 4 features with 'zscore' normalization 2 '' Function @(X)cbtocbt(X) 3 'featurelast' Fully Connected 10 fully connected layer
dlnet = dlnetwork(layers);
Error using dlnetwork/initialize
Invalid network.

Error in dlnetwork (line 210)
net = initialize(net, dlX{:});

Caused by:
Layer 'layer': Error using the predict function in layer nnet.cnn.layer.FunctionLayer. The function threw an error and could not be executed.
Unrecognized function or variable 'idxS'.

Error in solution>cbtocbt (line 42)
sizeS = size(X,idxS);

Error in solution>@(X)cbtocbt(X) (line 16)
functionLayer(@(X) cbtocbt(X),Formattable=true)

Error in nnet.cnn.layer.FunctionLayer/predict (line 61)
[varargout{1:layer.NumOutputs}] = feval(layer.PredictFcn, varargin{:});
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(.*)를 사용하십시오.

采纳的回答

Angelo Yeo
Angelo Yeo 2024-1-22
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)
ans = 1×3
10 100 944
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
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.
Again, the issue resides in the structure of your neural network. The "addition" layer cannot back-propagate to your "featurelast" layer.
  4 个评论
Angelo Yeo
Angelo Yeo 2024-1-22
Hi @jingoo lee, I'm back.
(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) 버튼을 이용하실 수 있습니다.
--------------------------------------------------------
jingoo lee
jingoo lee 2024-1-22
Thank you so much, hope you have a good day !

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Deep Learning Toolbox 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!