-
Notifications
You must be signed in to change notification settings - Fork 92
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
Showing
11 changed files
with
256 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .data import SubTokenizedField, TokenBucket | ||
from .trees import ConllXDataset, ListOpsDataset | ||
|
||
__all__ = [SubTokenizedField, TokenBucket] | ||
__all__ = [SubTokenizedField, TokenBucket, ConllXDataset, ListOpsDataset] |
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,36 @@ | ||
import torchtext.data as data | ||
|
||
|
||
class ConllXDataset(data.Dataset): | ||
def __init__(self, path, fields, encoding="utf-8", separator="\t", **kwargs): | ||
examples = [] | ||
columns = [[], []] | ||
column_map = {1: 0, 6: 1} | ||
with open(path, encoding=encoding) as input_file: | ||
for line in input_file: | ||
line = line.strip() | ||
if line == "": | ||
if columns: | ||
examples.append(data.Example.fromlist(columns, fields)) | ||
columns = [[], []] | ||
else: | ||
for i, column in enumerate(line.split(separator)): | ||
if i in column_map: | ||
columns[column_map[i]].append(column) | ||
|
||
if columns: | ||
examples.append(data.Example.fromlist(columns, fields)) | ||
super(ConllXDataset, self).__init__(examples, fields, **kwargs) | ||
|
||
|
||
class ListOpsDataset(data.Dataset): | ||
def __init__(self, path, fields, encoding="utf-8", separator="\t", **kwargs): | ||
examples = [] | ||
with open(path, encoding=encoding) as input_file: | ||
for line in input_file: | ||
a, b = line.split("\t") | ||
label = a | ||
words = [w for w in b.split() if w not in "()"] | ||
|
||
examples.append(data.Example.fromlist((words, label), fields)) | ||
super(ListOpsDataset, self).__init__(examples, fields, **kwargs) |
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,66 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
# NeuralCFG From Kim et al | ||
class Res(nn.Module): | ||
def __init__(self, H): | ||
super().__init__() | ||
self.u1 = nn.Linear(H, H) | ||
self.u2 = nn.Linear(H, H) | ||
|
||
self.v1 = nn.Linear(H, H) | ||
self.v2 = nn.Linear(H, H) | ||
self.w = nn.Linear(H, H) | ||
|
||
def forward(self, y): | ||
y = self.w(y) | ||
y = y + torch.relu(self.v1(torch.relu(self.u1(y)))) | ||
return y + torch.relu(self.v2(torch.relu(self.u2(y)))) | ||
|
||
|
||
class NeuralCFG(torch.nn.Module): | ||
def __init__(self, V, T, NT, H): | ||
super().__init__() | ||
self.NT = NT | ||
self.V = V | ||
self.T = T | ||
self.word_emb = nn.Parameter(torch.Tensor(V, H)) | ||
self.term_emb = nn.Parameter(torch.Tensor(T, H)) | ||
self.nonterm_emb = nn.Parameter(torch.Tensor(NT, H)) | ||
self.nonterm_emb_c = nn.Parameter(torch.Tensor(NT + T, NT + T, H)) | ||
self.root_emb = nn.Parameter(torch.Tensor(NT, H)) | ||
self.s_emb = nn.Parameter(torch.Tensor(1, H)) | ||
self.mlp1 = Res(H) | ||
self.mlp2 = Res(H) | ||
for p in self.parameters(): | ||
if p.dim() > 1: | ||
torch.nn.init.xavier_uniform_(p) | ||
|
||
def forward(self, input): | ||
T, NT = self.T, self.NT | ||
|
||
def terms(words): | ||
return torch.einsum( | ||
"bnh,th->bnt", self.word_emb[words], self.mlp1(self.term_emb) | ||
).log_softmax(-2) | ||
|
||
def rules(b): | ||
return ( | ||
torch.einsum("sh,tuh->stu", self.nonterm_emb, self.nonterm_emb_c) | ||
.view(NT, -1) | ||
.log_softmax(-1) | ||
.view(1, NT, NT + T, NT + T) | ||
.expand(b, NT, NT + T, NT + T) | ||
) | ||
|
||
def roots(b): | ||
return ( | ||
torch.einsum("ah,th->t", self.s_emb, self.mlp2(self.root_emb)) | ||
.log_softmax(-1) | ||
.view(1, NT) | ||
.expand(b, NT) | ||
) | ||
|
||
batch = input.shape[0] | ||
return terms(input), rules(batch), roots(batch) |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .TreeLSTM import TreeLSTM | ||
from .TreeLSTM import TreeLSTMCell | ||
from .NeuralCFG import NeuralCFG | ||
|
||
__all__ = [TreeLSTM] | ||
__all__ = [TreeLSTMCell, NeuralCFG] |
Oops, something went wrong.