-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleaky_relu.py
65 lines (51 loc) · 1.85 KB
/
leaky_relu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import torch
class LeakyReLU(torch.autograd.Function):
"""The Leaky Rectified Linear Unit (Leaky ReLU) activation function."""
@staticmethod
def forward(ctx, data: torch.Tensor, alpha: float = 1e-2) -> torch.Tensor:
"""Performs a forward pass."""
ctx.save_for_backward(data, torch.tensor(alpha).double())
return torch.where(data < 0.0, alpha * data, data)
@staticmethod
def backward(ctx, grad_output: torch.Tensor) -> torch.Tensor:
"""Performs a backpropagation."""
data, alpha = ctx.saved_tensors
grad = torch.where(data <= 0.0, alpha, 1.0)
return grad_output * grad
# Alternative impl {{{
#
# Can your activation function be expressed as a combination of existing PyTorch functions?
# If yes, no need to implement the `backward` method.
#
# import torch
# import torch.nn as nn
#
#
# class LeakyReLU(nn.Module):
# """The Rectified Linear Unit (ReLU) activation function."""
#
# def __init__(self) -> None:
# """Inherits from `nn.Module`."""
#
# super().__init__()
#
# def forward(self, data: torch.Tensor, alpha: float = 1e-2) -> torch.Tensor:
# """Performs a forward pass."""
#
# return torch.where(data < 0.0, alpha * data, data)
#
# }}}
# Testing (gradcheck) {{{
if __name__ == "__main__":
# Sets the manual seed for reproducible experiments
torch.manual_seed(0)
relu = LeakyReLU.apply
data = torch.randn(4, dtype=torch.double, requires_grad=True)
# `torch.autograd.gradcheck` takes a tuple of tensors as input, check if your gradient evaluated
# with these tensors are close enough to numerical approximations and returns `True` if they all
# verify this condition.
if torch.autograd.gradcheck(relu, data, eps=1e-8, atol=1e-7):
print("gradcheck successful")
else:
print("gradcheck unsuccessful")
# }}}