Table of Contents
Shortcuts

# Linear

class torch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None)

  Applies a linear transformation to the incoming data: y=x A T+b y = xA^T + b y=x A T+b

  This module supports TensorFloat32.

  On certain ROCm devices, when using float16 inputs this module will use different precision for backward.

  Parameters:

    - in_features (int) – size of each input sample

    - out_features (int) – size of each output sample

    - bias (bool) – If set to `False`, the layer will not learn an additive bias. Default: `True`

  Shape:

    - Input: (∗,H i n)(*, H_{in})(∗,H in​) where ∗*∗ means any number of dimensions including none and H i n=in_features H_{in} = \text{in\_features}H in​=in_features.

    - Output: (∗,H o u t)(*, H_{out})(∗,H o u t​) where all but the last dimension are the same shape as the input and H o u t=out_features H_{out} = \text{out\_features}H o u t​=out_features.

  Variables:

    - weight (torch.Tensor) – the learnable weights of the module of shape (out_features,in_features)(\text{out\_features}, \text{in\_features})(out_features,in_features). The values are initialized from U(−k,k)\mathcal{U}(-\sqrt{k}, \sqrt{k})U(−k
      ​,k
      ​), where k=1 in_features k = \frac{1}{\text{in\_features}}k=in_features 1​

    - bias – the learnable bias of the module of shape (out_features)(\text{out\_features})(out_features). If `bias` is `True`, the values are initialized from U(−k,k)\mathcal{U}(-\sqrt{k}, \sqrt{k})U(−k
      ​,k
      ​) where k=1 in_features k = \frac{1}{\text{in\_features}}k=in_features 1​

  Examples:

      >>> m = nn.Linear(20, 30)
      >>> input = torch.randn(128, 20)
      >>> output = m(input)
      >>> print(output.size())
      torch.Size([128, 30])

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.