-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
36 lines (30 loc) · 1017 Bytes
/
models.py
File metadata and controls
36 lines (30 loc) · 1017 Bytes
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
import torch
import torch.nn as nn
import torch.nn.functional as F
class CNN(nn.Module):
def __init__(self, embedding_dim, vocab, n_filters, filter_sizes):
super(CNN, self).__init__()
self.embed = nn.Embedding(len(vocab), embedding_dim)
self.embed.from_pretrained(vocab.vectors)
self.conv1 = nn.Sequential(
nn.Conv2d(1, n_filters, (filter_sizes[0], embedding_dim)),
nn.ReLU()
)
self.conv2 = nn.Sequential(
nn.Conv2d(1, n_filters, (filter_sizes[1], embedding_dim)),
nn.ReLU()
)
self.linear = nn.Sequential(
nn.Linear(100, 1)
)
def forward(self, x, length=None):
x = self.embed(x)
x = x.permute(1, 0, 2)
x = x.unsqueeze(1)
x_1 = self.conv1(x)
x_1, _ = torch.max(x_1, 2)
x_2 = self.conv2(x)
x_2, _ = torch.max(x_2, 2)
x = torch.cat([x_1, x_2], 1).squeeze()
x = self.linear(x)
return x.squeeze()