I would like to use expand to expand tensors. For example, I would like to expand tensor A with size 256x15 to size 256x15x24. So I use A.expand(256, 15, 24). It works when I use pytorch 0.1.12. However, I get the following error when I use this in pytorch 0.2.
Traceback (most recent call last):
File “”, line 1, in
RuntimeError: The expanded size of the tensor (24) must match the existing size (15) at non-singleton dimension 2. at /opt/conda/conda-bld/pytorch_1503963423183/work/torch/lib/TH/generic/THTensor.c:308
Any help on this issue?
Thanks in advance.
The problem is that you are adding a dimension at the end.
expand now only adds new dimensions up front (http://pytorch.org/docs/master/tensors.html#torch.Tensor.expand), being consistent with numpy’s broadcasting rules. What you can do is to first
unsqueeze to add a dimension, and then expand. (At least) two ways to do this:
- Numpy-style broadcasting:
A[:, :, None].expand(256, 15, 24)
- Explicit unsqueeze:
A.unsqueeze(-1).expand(256, 15, 24)
This might help you:
x = torch.randn(256, 15)
x.expand(256, 15, 24)
Thanks for the quick response!