PyTorch torch.nn.LeakyReLU Function

PyTorch torch.nn 参考手册PyTorch torch.nn Reference Manual


torch.nn.LeakyReLUIt is the Leaky ReLU activation function in PyTorch.

It allows negative values to have a small positive gradient, avoiding the "dead neuron" problem.

Function Definition

torch.nn.LeakyReLU(negative_slope=0.01, inplace=False)

Parameters

  • negative_slope: slope for negative values, default 0.01

Formula

f(x) = x, x > 0
f(x) = negative_slope * x, x <= 0

Usage Examples

Example 1: Basic Usage

Example

import torch
import torch.nn as nn

lrelu = nn.LeakyReLU(negative_slope=0.1)

x = torch.tensor([-2.0, -1.0, 0.0, 1.0, 2.0])
output = lrelu(x)

print("Input:", x.tolist())
print("Output:", output.tolist())
print("Negative values have a small positive gradient")

Example 2: Usage in GAN

Example

import torch
import torch.nn as nn

# GAN generators commonly use LeakyReLU
generator = nn.Sequential(
    nn.Linear(100, 256),
    nn.LeakyReLU(0.2),
    nn.Linear(256, 512),
    nn.LeakyReLU(0.2),
    nn.Linear(512, 784),
    nn.Tanh()
)

z = torch.randn(4, 100)
output = generator(z)

print("Input:", z.shape, "-> Output:", output.shape)

Example 3: Preventing Dead Neurons

Example

import torch
import torch.nn as nn

# Compare ReLU and LeakyReLU
relu = nn.ReLU()
lrelu = nn.LeakyReLU()

# negative value input
x = torch.randn(10, 100) - 5

# ReLU outputs all 0
print("Non-zero ratio after ReLU:", (relu(x) != 0).float().mean().item())

# LeakyReLU has gradient
print("Non-zero ratio after LeakyReLU:", (lrelu(x) != 0).float().mean().item())

Use Cases

  • GAN: generator, discriminator
  • Preventing dead neurons
  • Sparse networks

PyTorch torch.nn 参考手册PyTorch torch.nn Reference Manual

Other Extensions