Neural-network activations
Add nonlinear transformations between linear or convolutional layers.
PyTorch · Class
nn.ReLU applies the rectified linear unit element-wise, replacing negative values with zero and retaining nonnegative values. It supports inputs of any dimensionality and returns the same shape.
ReLU processes every element independently using (x) = max(0, x). It preserves positive values, replaces negative values with zero, and returns a tensor with the same shape as its input. The optional inplace setting allows the operation to modify the input storage.
torch.nn.ReLU(inplace=False)| Name | Type | Description |
|---|---|---|
inplace | bool | Whether the operation may be performed in-place; defaults to False. |
A tensor with the same shape as the input.
import torch
from torch import nn
relu = nn.ReLU()
x = torch.tensor([-2.0, 0.0, 3.0])
y = relu(x)
print(y) # tensor([0., 0., 3.])Creates a ReLU module and applies it element-wise to a tensor; negative values become zero while nonnegative values remain unchanged.
Add nonlinear transformations between linear or convolutional layers.
Build activation sequences with torch.nn.Sequential.
Construct CReLU by applying ReLU to x and -x before concatenation.
ReLU applies the rectified linear function element-wise: max(0, x). It maps negative inputs to zero and leaves nonnegative inputs unchanged.
Follow-up: What shape does ReLU return? It returns the same shape as its input, regardless of the number of input dimensions.
The inplace argument controls whether the operation can be performed in-place. Its default is False, so the module does not request in-place processing unless explicitly configured.
Follow-up: Why might inplace=True require care? Because modifying the input can conflict with later uses of that tensor or with autograd's saved values.
ReLU accepts inputs with any number of dimensions. Its output has the same shape as the input, because the transformation is applied independently to each element.
Follow-up: How could ReLU be used in a CReLU construction? Apply it to both x and -x, then concatenate the two resulting tensors.
torch.nn.ReLU6torch.nn.LeakyReLUtorch.nn.PReLUtorch.nn.RReLU