-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsigmoid.py
89 lines (71 loc) · 2.23 KB
/
sigmoid.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import torch
class Sigmoid(torch.autograd.Function):
"""The Sigmoid activation function."""
@staticmethod
def forward(ctx, data: torch.Tensor) -> torch.Tensor:
"""Performs a forward pass."""
neg_mask = data < 0
pos_mask = ~neg_mask
zs = torch.empty_like(data)
zs[neg_mask] = data[neg_mask].exp()
zs[pos_mask] = (-data[pos_mask]).exp()
res = torch.ones_like(data)
res[neg_mask] = zs[neg_mask]
result = res / (1 + zs)
ctx.save_for_backward(result)
return result
@staticmethod
def backward(ctx, grad_output: torch.Tensor) -> torch.Tensor:
"""Performs a backpropagation."""
(result,) = ctx.saved_tensors
grad = result * (1 - result)
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 Sigmoid(nn.Module):
# """The Sigmoid activation function."""
#
# def __init__(self) -> None:
# """Inherits from `nn.Module`."""
#
# super().__init__()
#
# def forward(self, data: torch.Tensor) -> torch.Tensor:
# """Performs a forward pass."""
#
# neg_mask = data < 0
# pos_mask = ~neg_mask
#
# zs = torch.empty_like(data)
# zs[neg_mask] = data[neg_mask].exp()
# zs[pos_mask] = (-data[pos_mask]).exp()
#
# res = torch.ones_like(data)
# res[neg_mask] = zs[neg_mask]
#
# result = res / (1 + zs)
#
# return result
#
# }}}
# Testing (gradcheck) {{{
if __name__ == "__main__":
# Sets the manual seed for reproducible experiments
torch.manual_seed(0)
sigmoid = Sigmoid.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(sigmoid, data, eps=1e-8, atol=1e-7):
print("gradcheck successful")
else:
print("gradcheck unsuccessful")
# }}}