pythonpytorchreshapetensordimensions

What does `-1` of `view()` mean in PyTorch?


As the question says, what does -1 of view() do in PyTorch?

>>> a = torch.arange(1, 17)
>>> a
tensor([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,
         11.,  12.,  13.,  14.,  15.,  16.])

>>> a.view(1,-1)
tensor([[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,
          11.,  12.,  13.,  14.,  15.,  16.]])

>>> a.view(-1,1)
tensor([[  1.],
        [  2.],
        [  3.],
        [  4.],
        [  5.],
        [  6.],
        [  7.],
        [  8.],
        [  9.],
        [ 10.],
        [ 11.],
        [ 12.],
        [ 13.],
        [ 14.],
        [ 15.],
        [ 16.]])

Does -1 of view() in PyTorch generate additional dimension? Does -1 of view() in PyTorch behave the same as -1 of reshape() in NumPy?


Solution

  • Yes, it does behave like -1 in numpy.reshape(), i.e. the actual value for this dimension will be inferred so that the number of elements in the view matches the original number of elements.

    For instance:

    import torch
    
    x = torch.arange(6)
    
    print(x.view(3, -1))      # inferred size will be 2 as 6 / 3 = 2
    # tensor([[ 0.,  1.],
    #         [ 2.,  3.],
    #         [ 4.,  5.]])
    
    print(x.view(-1, 6))      # inferred size will be 1 as 6 / 6 = 1
    # tensor([[ 0.,  1.,  2.,  3.,  4.,  5.]])
    
    print(x.view(1, -1, 2))   # inferred size will be 3 as 6 / (1 * 2) = 3
    # tensor([[[ 0.,  1.],
    #          [ 2.,  3.],
    #          [ 4.,  5.]]])
    
    # print(x.view(-1, 5))    # throw error as there's no int N so that 5 * N = 6
    # RuntimeError: invalid argument 2: size '[-1 x 5]' is invalid for input with 6 elements
    
    print(x.view(-1, -1, 3))  # throw error as only one dimension can be inferred
    # RuntimeError: invalid argument 1: only one dimension can be inferred