Unrecognized function or variable 'newShape' in auto-generated function pyUnsqueeze.m

4 views (last 30 days)
I'm trying to import a custom pytorch model in matlab using "importNetworkFromPyTorch", but I'm having some issues with the matlab autonomously generated function "pyUnsqueeze"
My model receives as imput an array with shape [1 10 8] and ouputs one shaped [1,10,1].
I import my net as
net = importNetworkFromPyTorch("path_to_my_model", 'PyTorchInputSizes', [1,10,8])
then to initialze it I run as prompted by the warning on the command window:
dlX1 = dlarray(rand([1,10,8]), 'UUU');
and then
net = initialize(net, dlX1);
upon which I get the error:
Error using dlnetwork/initialize (line 600)
Invalid network.
Caused by:
Layer 'TopLevelModule': Invalid network.
Layer 'ATEN3': Error using the predict function in layer empty_model_traced.TopLevelModule_ATEN3. The function threw an
error and could not be executed.
Unrecognized function or variable 'newShape'.
Error in empty_model_traced.ops.pyUnsqueeze (line 41)
Yval = reshape(Xval, newShape);
^^^^^^^^
Error in empty_model_traced.TopLevelModule_ATEN3/tracedPyTorchFunction (line 91)
[unsqueeze_input_1] = empty_model_traced.ops.pyUnsqueeze(arange_38, Constant_30);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in empty_model_traced.TopLevelModule_ATEN3/predict (line 46)
[unsqueeze_input_1] = tracedPyTorchFunction(obj,size_batchu_1,false,"predict");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The pyUnsqueeze.m function is:
function Y = pyUnsqueeze(X, dim)
%PYUNSQUEEZE Inserts a singleton dimension at the position given by dim.
% at::Tensor at::unsqueeze(const at::Tensor &self, int64_t dim)
% Copyright 2022-2023 The MathWorks, Inc.
import empty_model_traced.ops.*
dim = dim.value;
Xval = X.value;
Xrank = X.rank;
% Convert dim to reverse-pytorch
if (dim<0)
dim = -dim;
else
dim = Xrank - dim + 1;
end
% Reshape the data, inserting a singleton dim
Yrank = Xrank + 1;
if Yrank == 1
newShape = size(Xval);
elseif Yrank == 2
if dim==0
% X is a 1D vector, which will be a col in MATLAB ([N 1]).
% The new singleton dim should be inserted at the front if dim=0
newShape = flip(size(Xval));
elseif dim==1
% The new singleton dim should be inserted at the end if dim=1.
% X is already [N 1], so no need to flip it.
newShape = size(Xval);
end
else
newShape = ones(1, Yrank);
knownSizes = setdiff(1:Yrank, dim);
newShape(knownSizes) = size(Xval, 1:numel(knownSizes));
end
Yval = reshape(Xval, newShape);
Yval = dlarray(Yval, repmat('U', 1, max(2,Yrank)));
Y = struct('value', Yval, 'rank', Yrank);
end
With some debugging i get that Yrank = 2, and dim = 2, for which there is no case in the if-else list, hence newshape is never defined.
I can track back this to the python code:
pos = torch.arange(0, t, dtype=torch.long, device=device).unsqueeze(0) # shape (1, t)
Now, is this the correct behavior of the matlab function, and I'm doing something wrong in my code or is this a bug in the auto-generated function?
  1 Comment
Alessandro
Alessandro on 6 Jun 2025
It looks like the issue is with the "convert dim to reverese-pythorch" if-else block
instead of
if (dim<0)
dim = -dim;
else
dim = Xrank - dim + 1;
end
it should be
if (dim<0)
dim = Xrank + dim + 1;
end
to correctly implement the negative python index, as highlighted in the torch.unsqueeze docs

Sign in to comment.

Answers (0)

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!