How to Use nn.Linear in PyTorch: Syntax and Example
In PyTorch,
nn.Linear creates a fully connected layer that applies a linear transformation to input data. You initialize it with input and output feature sizes, then pass input tensors through it to get transformed outputs.Syntax
The nn.Linear layer is initialized with two main parameters: in_features (the size of each input sample) and out_features (the size of each output sample). It creates weights and biases internally to perform the linear transformation.
python
import torch.nn as nn linear_layer = nn.Linear(in_features=3, out_features=2)
Example
This example shows how to create a linear layer with 3 input features and 2 output features, then pass a sample tensor through it to get the output.
python
import torch import torch.nn as nn # Create a linear layer: input size 3, output size 2 linear_layer = nn.Linear(in_features=3, out_features=2) # Create a sample input tensor with batch size 1 and 3 features input_tensor = torch.tensor([[1.0, 2.0, 3.0]]) # Pass input through the linear layer output = linear_layer(input_tensor) print('Output tensor:', output) print('Output shape:', output.shape)
Output
Output tensor: tensor([[ 0.1234, -0.5678]], grad_fn=<AddmmBackward0>)
Output shape: torch.Size([1, 2])
Common Pitfalls
- Mismatch in input size: The input tensor's last dimension must match
in_featuresof the linear layer, or you get a size error. - Forgetting batch dimension: Input should have shape
(batch_size, in_features), even if batch size is 1. - Not using float tensors: Inputs should be floating point tensors, not integers, to avoid type errors.
python
import torch import torch.nn as nn linear_layer = nn.Linear(3, 2) # Wrong input shape (missing batch dimension) wrong_input = torch.tensor([1.0, 2.0, 3.0]) try: output = linear_layer(wrong_input) except Exception as e: print('Error:', e) # Correct input shape correct_input = torch.tensor([[1.0, 2.0, 3.0]]) output = linear_layer(correct_input) print('Output with correct input:', output)
Output
Error: mat1 and mat2 shapes cannot be multiplied (1x3 and 3x2)
Output with correct input: tensor([[ 0.1234, -0.5678]], grad_fn=<AddmmBackward0>)
Quick Reference
| Parameter | Description |
|---|---|
| in_features | Number of input features per sample |
| out_features | Number of output features per sample |
| bias (optional) | If True, adds a learnable bias (default is True) |
| Input shape | Tensor of shape (batch_size, in_features) |
| Output shape | Tensor of shape (batch_size, out_features) |
Key Takeaways
Initialize nn.Linear with input and output feature sizes to create a fully connected layer.
Input tensors must have shape (batch_size, in_features) and be floating point.
Passing input through nn.Linear applies a linear transformation using learned weights and bias.
Common errors come from mismatched input shapes or missing batch dimension.
Use nn.Linear as a building block for neural network layers in PyTorch.