Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
3 次查看(过去 30 天)
显示 更早的评论
Hi, I got an error and I don't know why it happened.
The error is "Unable to perform assignment because the size of the left side is 6-by-7-by-1-by-128 and the size of the right side is 768-by-7".
Here are the details of my network:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1416859/image.png)
I find that the error occurred at the 7th layer ("new_reshape_layer")
This layer is a functionLayer:
new_reshape_layer = functionLayer(@(X) dlarray(reshape(X,[],7,1,1),"SSCB"),Name='new_reshape_layer',Formattable=true,Acceleratable=true);
I also try to use
features = activations(net,X,layer)
to get the output of each layer, and the output is
No.1 layer: 14 x 150
No.2 layer: 128 x 150
No.3 layer: 128 x 150
No.4 layer: 128 x 150
No.5 layer: 128 x 150
No.6 layer: 42 x 150
No.7 layer: error happened:
Unable to perform assignment because the size of the left side is 6-by-7-by-1-by-128 and the size of the right side is 768-by-7.
Error in DAGNetwork/calculateActivations>iDoInference (line 156)
Y(indices{:}) = reshapeFcn(Yb, numel(info.MiniBatchIdx));
Error in DAGNetwork/calculateActivations (line 90)
Y = iDoInference(dispatcher, numObservations, predictNetwork, gpuShouldBeUsed, ...
Error in DAGNetwork/activations (line 257)
Y = this.calculateActivations(X, layerIndex, layerOutputIndex, nameValuePairs{:});
Error in DNNTest_reshape67_2_functionlayer (line 61)
activations_after7 = activations(new_net, xtest, 'new_reshape_layer');
I don't know why this error happened and how to get into the network to debug it.
0 个评论
采纳的回答
Joss Knight
2023-6-22
编辑:Joss Knight
2023-6-22
If you click on the line number in the editor next to where you create your function layer, you can put a breakpoint at the entry point to the function itself, which will allow you to debug.
Your function layer is not correct because it assumes the batch size is 1 when it reshapes. So the output array is pre-allocated as 6-by-7-by-1-by-numObservations; but your first batch of 128 observations is getting reshaped to 768-by-7-by-1-by-1.
Because this network includes a fully connected layer, it cannot have an output that varies in size in the spatial dimensions. Perhaps you intended your function layer to be defined instead as:
new_reshape_layer = functionLayer(@(X) dlarray(reshape(X,6,7,1,[]),"SSCB"),Name='new_reshape_layer',Formattable=true,Acceleratable=true);
0 个评论
更多回答(1 个)
Richard
2023-6-22
The functionLayer you created will only work with inputs that have a batch size of 1 - it is reshaping the input to K x 7 x 1 x 1. You are then using the network with a batch size of 128.
You need to use a resize shape that keeps the batch size from the input: Try this instead:
new_reshape_layer = functionLayer(@(X) dlarray(reshape(X,size(X,1)/7,7,1,size(X, 2)),"SSCB"),Name='new_reshape_layer',Formattable=true,Acceleratable=true);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!