torch / torch7

http://torch.ch
Other
9k stars 2.38k forks source link

Convenience function to add a singleton dimension to a torch tensor. #824

Open galv opened 8 years ago

galv commented 8 years ago

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?

fmassa commented 8 years ago

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.

galv commented 8 years ago

Amazing, thanks for letting me know about this.

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

pbloem commented 7 years ago

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. :)

fmassa commented 7 years ago

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