-
Notifications
You must be signed in to change notification settings - Fork 101
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
6d3c94a
commit ac912ce
Showing
23 changed files
with
51,739 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
from torch.nn.modules.rnn import RNNCellBase | ||
|
||
|
||
class LayerNorm(nn.Module): | ||
|
||
def __init__(self, features, eps=1e-6): | ||
super(LayerNorm, self).__init__() | ||
self.gamma = nn.Parameter(torch.ones(features)) | ||
self.beta = nn.Parameter(torch.zeros(features)) | ||
self.eps = eps | ||
|
||
def forward(self, x): | ||
mean = x.mean(-1, keepdim=True) | ||
std = x.std(-1, keepdim=True) | ||
return self.gamma * (x - mean) / (std + self.eps) + self.beta | ||
|
||
|
||
class LSTMCell(RNNCellBase): | ||
|
||
def __init__(self, input_size, hidden_size, dropout=0): | ||
super(LSTMCell, self).__init__() | ||
self.input_size = input_size | ||
self.hidden_size = hidden_size | ||
|
||
self.ih = nn.Sequential(nn.Linear(input_size, 3 * hidden_size, bias=True), LayerNorm(3 * hidden_size)) | ||
self.hh = nn.Sequential(nn.Linear(hidden_size, 3 * hidden_size, bias=True), LayerNorm(3 * hidden_size)) | ||
|
||
self.c_norm = LayerNorm(hidden_size) | ||
self.drop = nn.Dropout(dropout) | ||
|
||
self.dst = nn.Sequential(nn.Linear(hidden_size + input_size, hidden_size), | ||
# LayerNorm(1), | ||
nn.Softmax(dim=-1)) | ||
|
||
def forward(self, input, hidden, rmask): | ||
hx, cx = hidden | ||
|
||
input = self.drop(input) | ||
hx = hx * rmask | ||
gates = self.ih(input) + self.hh(hx) #+ self.bias | ||
|
||
cell, ingate, outgate = gates.chunk(3, 1) | ||
|
||
dst = self.dst(torch.cat([input, hx], dim=-1)) | ||
fgate = torch.cumsum(dst, dim=-1) | ||
|
||
distance = fgate.sum(dim=-1) / self.hidden_size | ||
|
||
ingate = F.sigmoid(ingate) * fgate | ||
forgetgate = (1 - ingate) | ||
cell = F.tanh(cell) | ||
outgate = F.sigmoid(outgate) | ||
|
||
cy = forgetgate * cx + ingate * cell | ||
hy = outgate * F.tanh(self.c_norm(cy)) | ||
|
||
return hy, cy, distance | ||
|
||
def init_hidden(self, bsz): | ||
weight = next(self.parameters()).data | ||
return weight.new(bsz, self.hidden_size).zero_(), \ | ||
weight.new(bsz, self.hidden_size).zero_() |
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,70 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
from torch.nn.modules.rnn import RNNCellBase | ||
|
||
|
||
class LayerNorm(nn.Module): | ||
|
||
def __init__(self, features, eps=1e-6): | ||
super(LayerNorm, self).__init__() | ||
self.gamma = nn.Parameter(torch.ones(features)) | ||
self.beta = nn.Parameter(torch.zeros(features)) | ||
self.eps = eps | ||
|
||
def forward(self, x): | ||
mean = x.mean(-1, keepdim=True) | ||
std = x.std(-1, keepdim=True) | ||
return self.gamma * (x - mean) / (std + self.eps) + self.beta | ||
|
||
|
||
class LSTMCell(RNNCellBase): | ||
|
||
def __init__(self, input_size, hidden_size, dropout=0): | ||
super(LSTMCell, self).__init__() | ||
self.input_size = input_size | ||
self.hidden_size = hidden_size | ||
|
||
self.ih = nn.Sequential(nn.Linear(input_size, 5 * hidden_size, bias=True), LayerNorm(5 * hidden_size)) | ||
self.hh = nn.Sequential(nn.Linear(hidden_size, 5 * hidden_size, bias=True), LayerNorm(5 * hidden_size)) | ||
|
||
self.c_norm = LayerNorm(hidden_size) | ||
self.drop = nn.Dropout(dropout) | ||
|
||
self.dst = nn.Sequential(nn.Linear(hidden_size + input_size, hidden_size), | ||
# LayerNorm(1), | ||
nn.Softmax(dim=-1)) | ||
|
||
def forward(self, input, hidden, rmask): | ||
hx, cx = hidden | ||
|
||
input = self.drop(input) | ||
hx = hx * rmask | ||
gates = self.ih(input) + self.hh(hx) #+ self.bias | ||
|
||
cell, ingate, forgetgate, updategate, outgate = gates.chunk(5, 1) | ||
|
||
# dst = self.dst(torch.cat([input, hx], dim=-1)) | ||
# fgate = torch.cumsum(dst, dim=-1) | ||
|
||
def cumsoftmax(x): | ||
return torch.cumsum(F.softmax(x, dim=-1), dim=-1) | ||
|
||
ingate = cumsoftmax(ingate) | ||
forgetgate = 1. - cumsoftmax(forgetgate) | ||
updategate = F.sigmoid(updategate) | ||
cell = F.tanh(cell) | ||
outgate = F.sigmoid(outgate) | ||
|
||
distance = (1. - forgetgate).sum(dim=-1) / self.hidden_size | ||
# distance = ingate.sum(dim=-1) / self.hidden_size | ||
|
||
cy = forgetgate * updategate * cx + ingate * (1. - updategate) * cell | ||
hy = outgate * F.tanh(self.c_norm(cy)) | ||
|
||
return hy, cy, distance | ||
|
||
def init_hidden(self, bsz): | ||
weight = next(self.parameters()).data | ||
return weight.new(bsz, self.hidden_size).zero_(), \ | ||
weight.new(bsz, self.hidden_size).zero_() |
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,58 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
from torch.nn.modules.rnn import RNNCellBase | ||
|
||
|
||
class LayerNorm(nn.Module): | ||
|
||
def __init__(self, features, eps=1e-6): | ||
super(LayerNorm, self).__init__() | ||
self.gamma = nn.Parameter(torch.ones(features)) | ||
self.beta = nn.Parameter(torch.zeros(features)) | ||
self.eps = eps | ||
|
||
def forward(self, x): | ||
mean = x.mean(-1, keepdim=True) | ||
std = x.std(-1, keepdim=True) | ||
return self.gamma * (x - mean) / (std + self.eps) + self.beta | ||
|
||
|
||
class LSTMCell(RNNCellBase): | ||
|
||
def __init__(self, input_size, hidden_size, dropout=0): | ||
super(LSTMCell, self).__init__() | ||
self.input_size = input_size | ||
self.hidden_size = hidden_size | ||
|
||
self.ih = nn.Sequential(nn.Linear(input_size, 4 * hidden_size, bias=True), LayerNorm(4 * hidden_size)) | ||
self.hh = nn.Sequential(nn.Linear(hidden_size, 4 * hidden_size, bias=True), LayerNorm(4 * hidden_size)) | ||
|
||
self.c_norm = LayerNorm(hidden_size) | ||
self.drop = nn.Dropout(dropout) | ||
|
||
def forward(self, input, hidden, rmask): | ||
hx, cx = hidden | ||
|
||
input = self.drop(input) | ||
hx = hx * rmask | ||
gates = self.ih(input) + self.hh(hx) #+ self.bias | ||
|
||
cell, ingate, forgetgate, outgate = gates.chunk(4, 1) | ||
|
||
distance = forgetgate.sum(dim=-1) / self.hidden_size | ||
|
||
ingate = F.sigmoid(ingate) | ||
forgetgate = F.sigmoid(forgetgate) | ||
cell = F.tanh(cell) | ||
outgate = F.sigmoid(outgate) | ||
|
||
cy = forgetgate * cx + ingate * cell | ||
hy = outgate * F.tanh(self.c_norm(cy)) | ||
|
||
return hy, cy, distance | ||
|
||
def init_hidden(self, bsz): | ||
weight = next(self.parameters()).data | ||
return weight.new(bsz, self.hidden_size).zero_(), \ | ||
weight.new(bsz, self.hidden_size).zero_() |
Oops, something went wrong.