2
 minibatch = torch.Tensor(5, 2, 3,5)
 m = nn.View(-1):setNumInputDims(1)

 m:forward(minibatch)

gives a tensor of size

 30x5

 m = nn.View(-1):setNumInputDims(3)
 m:forward(minibatch)

gives a tensor of size

 5 x 30

 m = nn.View(-1):setNumInputDims(2)
 m:forward(minibatch)

gives a tensor of size

 10 x 15

What is going on? I don't understand why I'm getting the dimensions I am. The reason I don' think I understand it is that I'm thinking that the View m is expecting n dims as the input. So if n = 1, then we take 5 as the 1st dim and 30 as the 2nd dim, which is what seems to be happening when the numInputDims is set to 2.

lars
  • 1,786
  • 3
  • 27
  • 43

1 Answers1

2

As its name indicates, View(-1):setNumInputDims(n) is to set the number of input dimensions of View(-1).

To understand the role of View(-1), please refer to How view() method works for tensor in torch

If there is any situation that you don't know how many rows you want but are sure of the number of columns then you can mention it as -1(You can extend this to tensors with more dimensions. Only one of the axis value can be -1). This is a way of telling the library; give me a tensor that has these many columns and you compute the appropriate number of rows that is necessary to make this happen.

So View(-1) converts the input to a two-dimensional matrix. Note View(-1) corresponds to the columns of this matrix. Hence its input dimension is the latter half of the complete input. Its number of dimensions means how many dimensions are "allocated" for the columns, and any dimensions before these dimensions are used for the rows.

Therefore in your example:

minibatch = torch.Tensor(5, 2, 3,5)
m = nn.View(-1):setNumInputDims(2)

It allocates the last two dimensions (3*5) to the columns and the first two dimensions (5*2) to the rows. The result tensor is then 10*15.

Shaohua Li
  • 335
  • 2
  • 7