Created
August 7, 2018 20:17
-
-
Save llSourcell/7322f41690cc6e8d99ad74dc28190ea3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import math | |
| import torch | |
| class NeuralArithmeticLogicUnitCell(nn.Module): | |
| #NALU code in PyTorch! | |
| def __init__(self, in_dim, out_dim): | |
| super().__init__() | |
| self.in_dim = in_dim | |
| self.out_dim = out_dim | |
| self.eps = 1e-10 | |
| self.G = Parameter(torch.Tensor(out_dim, in_dim)) | |
| self.nac = NeuralAccumulatorCell(in_dim, out_dim) | |
| self.register_parameter('bias', None) | |
| init.kaiming_uniform_(self.G, a=math.sqrt(5)) | |
| def forward(self, input): | |
| a = self.nac(input) | |
| g = F.sigmoid(F.linear(input, self.G, self.bias)) | |
| add_sub = g * a | |
| log_input = torch.log(torch.abs(input) + self.eps) | |
| m = torch.exp(self.nac(log_input)) | |
| mul_div = (1 - g) * m | |
| y = add_sub + mul_div | |
| return y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment