Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

4 views (last 30 days)
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:
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.

Accepted Answer

Joss Knight
Joss Knight on 22 Jun 2023
Edited: Joss Knight on 22 Jun 2023
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);

More Answers (1)

Richard
Richard on 22 Jun 2023
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);

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!