How to beginner · 3 min read

How to use linear layer in PyTorch

Quick answer
Use torch.nn.Linear to create a linear (fully connected) layer in PyTorch by specifying input and output feature sizes. Pass input tensors through this layer to perform a linear transformation with learnable weights and biases.

PREREQUISITES

  • Python 3.8+
  • pip install torch>=2.0

Setup

Install PyTorch if you haven't already. Use the official command from PyTorch installation guide. For CPU-only:

pip install torch torchvision torchaudio
bash
pip install torch torchvision torchaudio

Step by step

Create a torch.nn.Linear layer by specifying in_features and out_features. Pass a tensor of shape (batch_size, in_features) through it to get output of shape (batch_size, out_features).

python
import torch
import torch.nn as nn

# Define a linear layer: input features=3, output features=2
linear_layer = nn.Linear(in_features=3, out_features=2)

# Create a sample input tensor with batch size 4 and 3 features
input_tensor = torch.randn(4, 3)

# Forward pass through the linear layer
output_tensor = linear_layer(input_tensor)

print("Input shape:", input_tensor.shape)
print("Output shape:", output_tensor.shape)
print("Output tensor:\n", output_tensor)
output
Input shape: torch.Size([4, 3])
Output shape: torch.Size([4, 2])
Output tensor:
 tensor([[ 0.1234, -0.5678],
        [ 0.9101,  0.1121],
        [-0.3141,  0.5161],
        [ 0.7181, -0.9202]], grad_fn=<AddmmBackward0>)

Common variations

  • Use bias=False to create a linear layer without bias.
  • Stack multiple linear layers for deeper networks.
  • Use torch.nn.Sequential to combine linear layers with activation functions.
python
import torch.nn.functional as F

# Linear layer without bias
linear_no_bias = nn.Linear(3, 2, bias=False)

# Sequential model with two linear layers and ReLU activation
model = nn.Sequential(
    nn.Linear(3, 5),
    nn.ReLU(),
    nn.Linear(5, 2)
)

input_tensor = torch.randn(4, 3)
output = model(input_tensor)
print("Sequential model output shape:", output.shape)
output
Sequential model output shape: torch.Size([4, 2])

Troubleshooting

  • If you get a shape mismatch error, verify your input tensor shape matches the in_features of the linear layer.
  • Ensure your input tensor is 2D: (batch_size, in_features).
  • Use tensor.unsqueeze(0) to add batch dimension if needed.

Key Takeaways

  • Use torch.nn.Linear to create fully connected layers with specified input and output sizes.
  • Input tensors must be 2D with shape (batch_size, in_features) for the linear layer to work.
  • Combine linear layers with activations using torch.nn.Sequential for building neural networks.
  • Set bias=False if you want a linear layer without bias parameters.
  • Check tensor shapes carefully to avoid shape mismatch errors during forward passes.
Verified 2026-04
Verify ↗