Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Class

nn.Linear

torch.nn.Linear is a trainable affine layer that maps each input vector from in_features to out_features. It preserves all leading dimensions and can optionally include a learnable bias.

How it works

The module stores a weight matrix and, by default, a bias vector. For an input whose final dimension matches in_features, it computes an affine transformation and changes only that final dimension to out_features; leading dimensions are preserved.

Syntax

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

Parameters

NameTypeDescription
in_featuresintSize of each input sample.
out_featuresintSize of each output sample.
biasboolWhether to learn an additive bias; defaults to True.
devicetorch.device, optionalOptional device on which to create the module.
dtypetorch.dtype, optionalOptional data type for the module’s parameters.

Returns

A tensor with shape (*, out_features), where * represents the input’s unchanged leading dimensions.

Key points

  • nn.Linear implements a trainable affine mapping rather than an activation function.
  • in_features specifies the width of each input sample.
  • out_features specifies the width produced for each sample.
  • For higher-rank inputs, the layer preserves every leading dimension.
  • The weight matrix has one row per output feature and one column per input feature.
  • When enabled, the bias contains one trainable value per output feature.

Common mistakes

  • The input may have arbitrary leading dimensions, but its final dimension must equal in_features.
  • Set bias=False when the affine offset is not wanted; the default is True.

Example: Apply a linear layer

import torch
from torch import nn

layer = nn.Linear(20, 30)
x = torch.randn(128, 20)
y = layer(x)
print(y.shape)  # torch.Size([128, 30])

Creates a trainable 20-to-30 affine layer and applies it to a batch whose final dimension has size 20.

When to use it

Feature projection

Transforming representations between neural-network stages.

Classification or regression head

Producing logits or scores from a learned representation.

Embedding or sequence projection

Changing the width of the final dimension in batched or sequence data.

Interview questions

QWhat transformation does nn.Linear perform, and what shapes does it use?

It applies y = xAᵀ + b, where the learnable weight has shape (out_features, in_features). The input’s last dimension must equal in_features, and the output’s last dimension becomes out_features.

Follow-up: What happens to a batched or higher-rank input? Every dimension except the final one is retained, while the final dimension is replaced by out_features.

QWhat do the constructor arguments mean?

in_features is the size of each input sample, while out_features is the size of each output sample. bias controls whether the module includes a learnable additive bias and defaults to True.

Follow-up: How would you create a layer without an offset? Construct it with bias=False.

QHow are the learnable parameters shaped and initialized?

The weight is initialized from a uniform distribution whose bounds depend on the reciprocal of in_features. The module exposes weight with shape (out_features, in_features) and bias with shape (out_features).

Follow-up: What parameter shape should be expected when connecting this layer to a previous representation of width n? The weight’s second dimension should be n, so construct the layer with in_features=n.

  • torch.nn.Module
  • torch.nn.LazyLinear
  • torch.nn.Identity
  • torch.nn.Bilinear

Source: Linear — PyTorch 2.8 documentation. Checked 2026-10-15.