Torch7: Convenience function to add a singleton dimension to a torch tensor.

Created on 29 Oct 2016  路  4Comments  路  Source: torch/torch7

I'm annoyed that to add a singleton dimension to a tensor, I have to use two lines of code. Like this:

local numChannels, height, width = image:size(1), roadImage:size(2), roadImage:size(3)
image = image:view(1, numChannels, height, width)

I would really like a one-liner for this. Does anyone have any suggestions?

If not, how open are the maintainers to my adding a new function to the library to do this?

Most helpful comment

Amazing, thanks for letting me know about this.

I agree, a torch.unsqueeze() would be handy.

All 4 comments

There is a helper function for that in nn if you want.
It can be found in nn.utils.addSingletonDimension.
Given that there is a nn function called Unsqueeze that does exactly what you want (and no torch counterpart), I think it's reasonable to include a unsqueeze function in torch following the implementation of addSingletonDimension.

Amazing, thanks for letting me know about this.

I agree, a torch.unsqueeze() would be handy.

For anybody else coming accross this, the unsqueeze function now exists:
http://pytorch.org/docs/master/torch.html#torch.unsqueeze
EDIT: Well, it exists in pytorch, anyway. :)

~Yes, we also can use the numpy notation for adding new dimensions, like tensor[None, :, None].~ Oups, this is torch7 repo, no pytorch...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kaleem0002 picture kaleem0002  路  10Comments

visonpon picture visonpon  路  3Comments

Nate1874 picture Nate1874  路  3Comments

i55code picture i55code  路  4Comments

wyvern92 picture wyvern92  路  6Comments