Last active
June 8, 2020 17:18
-
-
Save dpressel/3b4780bafcef14377085544f44183353 to your computer and use it in GitHub Desktop.
Highway layer using PyTorch
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 torch | |
| import torch.nn as nn | |
| class Highway(nn.Module): | |
| def __init__(self, input_size): | |
| super(Highway, self).__init__() | |
| self.proj = nn.Linear(input_size, input_size) | |
| self.transform = nn.Linear(input_size, input_size) | |
| self.transform.bias.data.fill_(-2.0) | |
| def forward(self, input): | |
| proj_result = nn.functional.relu(self.proj(input)) | |
| proj_gate = nn.functional.sigmoid(self.transform(input)) | |
| gated = (proj_gate * proj_result) + ((1 - proj_gate) * input) | |
| return gated |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tensorflow version is here: https://gist.github.com/dpressel/38cf85b4d3ed7991b4750202294f5220