-
Notifications
You must be signed in to change notification settings - Fork 12
/
nlp_models.py
59 lines (41 loc) · 1.79 KB
/
nlp_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch_utils import ScaledEmbedding
class FirstModel(nn.Module):
def __init__(self,
embedding_dim=30,vocab_size = 1,seq_len = 1):
super(FirstModel, self).__init__()
self._seq_len = seq_len
self._embedding_dim = embedding_dim
#self.embeddings = nn.Embedding(vocab_size, embedding_dim)
self.embeddings = ScaledEmbedding(vocab_size, embedding_dim)
self.fc1 = nn.Linear(seq_len*embedding_dim,100)
self.fc2 = nn.Linear(100,1)
def forward(self, words_id):
words_embedding = self.embeddings(words_id).view(-1,self._seq_len*self._embedding_dim)
words_embedding = words_embedding.squeeze()
x = F.relu(self.fc1(words_embedding))
x = self.fc2(F.dropout(x,0.7))
return F.sigmoid(x)
class ConvModel(nn.Module):
def __init__(self,embedding_dim = 30, vocab_size = 1, seq_len = 1):
super(ConvModel,self).__init__()
self._seq_len = seq_len
self._embedding_dim = embedding_dim
self.embeddings = ScaledEmbedding(vocab_size, embedding_dim)
self.conv = nn.Conv1d(embedding_dim,64,5,padding=2)
self.fc1 = nn.Linear(32*seq_len,100)
self.mp = nn.MaxPool1d(2)
self.fc2 = nn.Linear(100,1)
def forward(self,words_id):
words_embedding = self.embeddings(words_id).permute(0,2,1)
#words_embedding.data.squeeze_()
#words_embedding.data.unsqueeze_(1)
x = F.dropout(words_embedding,0.2)
x = F.relu(self.conv(x))
x = self.mp(F.dropout(x,0.2))
x = x.view(-1,self._seq_len*32)
x = F.dropout(self.fc1(x),0.7)
return F.sigmoid(self.fc2(x))