This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 801
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Pull Request resolved: #829 Implements Gaussian Error Linear Units (GELUs) as an activation for Deep CNN representation. Also creates an interface to leverage different types of activation functions. Differential Revision: D16462672 fbshipit-source-id: 177dff360ef46f0e6041c76712e18eee89666b0a
- Loading branch information
1 parent
e8dca0f
commit 3a5675a
Showing
3 changed files
with
61 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved | ||
|
||
import math | ||
|
||
import torch | ||
import torch.nn as nn | ||
from pytext.config.module_config import Activation | ||
|
||
|
||
class GeLU(nn.Module): | ||
""" | ||
Implements Gaussian Error Linear Units (GELUs). Note: x * x * x is used | ||
instead of torch.pow(x, 3) due to issues with ONNX compatibility: | ||
https://github.com/pytorch/pytorch/issues/18475 | ||
Reference: | ||
Gaussian Error Linear Units (GELUs). Dan Hendrycks, Kevin Gimpel. | ||
Technical Report, 2017. https://arxiv.org/pdf/1606.08415.pdf | ||
""" | ||
|
||
def forward(self, x): | ||
return ( | ||
0.5 | ||
* x | ||
* (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * (x * x * x)))) | ||
) | ||
|
||
|
||
def get_activation(name): | ||
if name == Activation.RELU: | ||
return nn.ReLU() | ||
elif name == Activation.LEAKYRELU: | ||
return nn.LeakyReLU() | ||
elif name == Activation.TANH: | ||
return torch.tanh | ||
elif name == Activation.GELU: | ||
return GeLU() | ||
elif name == Activation.GLU: | ||
return nn.GLU(dim=1) | ||
else: | ||
raise RuntimeError(f"{name} is not supported") |