-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb6e6f6
commit 94a45d2
Showing
9 changed files
with
89 additions
and
63 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
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,39 @@ | ||
|
||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
|
||
from pie import initialization | ||
|
||
|
||
class Highway(nn.Module): | ||
""" | ||
Highway network | ||
""" | ||
def __init__(self, in_features, num_layers, act='relu'): | ||
self.in_features = in_features | ||
|
||
self.act = act | ||
super().__init__() | ||
|
||
self.layers = nn.ModuleList( | ||
[nn.Linear(in_features, in_features*2) for _ in range(num_layers)]) | ||
|
||
self.init() | ||
|
||
def init(self): | ||
for layer in self.layers: | ||
initialization.init_linear(layer) | ||
# bias gate to let information go untouched | ||
nn.init.constant_(layer.bias[self.in_features:], 1.) | ||
|
||
def forward(self, inp): | ||
current = inp | ||
for layer in self.layers: | ||
inp, gate = layer(current).chunk(2, dim=-1) | ||
inp, gate = getattr(F, self.act)(inp), F.sigmoid(gate) | ||
current = gate * current + (1 - gate) * inp | ||
|
||
return current | ||
|
||
|
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
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
Thanks for this correction. I should have proposed a PR for that a long time ago...