-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
47 lines (42 loc) · 1.24 KB
/
model.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
import torch.nn as nn
class CBOW_Model(nn.Module):
"""
Implementation of CBOW model described in paper:
https://arxiv.org/abs/1301.3781
"""
def __init__(self, vocab_size: int):
super(CBOW_Model, self).__init__()
self.embeddings = nn.Embedding(
num_embeddings=vocab_size,
embedding_dim=300,
max_norm=1,
)
self.linear = nn.Linear(
in_features=300,
out_features=vocab_size,
)
def forward(self, inputs_):
x = self.embeddings(inputs_)
x = x.mean(axis=1)
x = self.linear(x)
return x
class SkipGram_Model(nn.Module):
"""
Implementation of Skip-Gram model described in paper:
https://arxiv.org/abs/1301.3781
"""
def __init__(self, vocab_size: int, embedding_dim: int):
super(SkipGram_Model, self).__init__()
self.embeddings = nn.Embedding(
num_embeddings=vocab_size,
embedding_dim=embedding_dim,
max_norm=1,
)
self.linear = nn.Linear(
in_features=embedding_dim,
out_features=vocab_size,
)
def forward(self, inputs_):
x = self.embeddings(inputs_)
x = self.linear(x)
return x