title: Pytorch Linear Layer now automatically reshape the input?
body: <p>I remember in the past, <code>nn.Linear</code> only accepts 2D tensors.</p>
  
  <p>But today, I discover that <code>nn.Linear</code> now accepts 3D, or even tensors with arbitrary dimensions.</p>
  
  <pre><code>X = torch.randn((20,20,20,20,10))
  linear_layer = nn.Linear(10,5)
  output = linear_layer(X)
  print(output.shape)
  &gt;&gt;&gt; torch.Size([20, 20, 20, 20, 5])
  </code></pre>
  
  <p>When I check the documentation for Pytorch, it does say that it now takes </p>
  
  <blockquote>
    <p>Input: :math:<code>(N, *, H_{in})</code> where :math:<code>*</code> means any number of
              additional dimensions and :math:<code>H_{in} = \text{in\_features}</code></p>
  </blockquote>
  
  <p>So it seems to me that Pytorch <code>nn.Linear</code> now reshape the input by <code>x.view(-1, input_dim)</code> automatically. </p>
  
  <p>But I cannot find any <code>x.shape</code> or <code>x.view</code> in the <a href="https://pytorch.org/docs/stable/_modules/torch/nn/modules/linear.html#Linear" rel="nofollow noreferrer">source code</a>:</p>
  
  <pre><code>class Linear(Module):
      __constants__ = ['bias']
  
      def __init__(self, in_features, out_features, bias=True):
          super(Linear, self).__init__()
          self.in_features = in_features
          self.out_features = out_features
          self.weight = Parameter(torch.Tensor(out_features, in_features))
          if bias:
              self.bias = Parameter(torch.Tensor(out_features))
          else:
              self.register_parameter('bias', None)
          self.reset_parameters()
  
      def reset_parameters(self):
          init.kaiming_uniform_(self.weight, a=math.sqrt(5))
          if self.bias is not None:
              fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
              bound = 1 / math.sqrt(fan_in)
              init.uniform_(self.bias, -bound, bound)
  
      @weak_script_method
      def forward(self, input):
          return F.linear(input, self.weight, self.bias)
  
      def extra_repr(self):
          return 'in_features={}, out_features={}, bias={}'.format(
              self.in_features, self.out_features, self.bias is not None
          )
  </code></pre>
  
  <p>Can anyone confirms this?</p>