-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmodel.py
99 lines (76 loc) · 2.91 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import torch
import torch.nn as nn
import torch.nn.functional as F
import os
import numpy as np
device = torch.device("cpu")
Feature_DIM = 64
class EncoderRNN(nn.Module):
def __init__(self, hidden_size):
super(EncoderRNN, self).__init__()
self.hidden_size = hidden_size
self.gru = nn.GRU(Feature_DIM, hidden_size)
def forward(self, input_, hidden):
input_ = input_.view(1, 1, -1)
output, hidden = self.gru(input_, hidden)
return output, hidden
def initHidden(self):
return torch.zeros(1, 1, self.hidden_size, device=device)
class DecoderRNN(nn.Module):
def __init__(self, embedding, hidden_size, output_size):
super(DecoderRNN, self).__init__()
hidden_size = hidden_size
# self.embedding = nn.Embedding(output_size, hidden_size)
self.embedding = embedding
self.gru = nn.GRU(50, hidden_size)
self.out = nn.Linear(hidden_size, output_size)
self.softmax = nn.LogSoftmax(dim=1)
self.dropout = nn.Dropout(0.2)
def forward(self, input_, hidden):
output = self.embedding(input_).view(1, 1, -1)
output = self.dropout(output)
# output = F.relu(output)
output, hidden = self.gru(output, hidden)
output = self.softmax(self.out(output[0]))
return output, hidden
class OutPutLayer(nn.Module):
def __init__(self, hidden_size, output_size):
super(OutPutLayer, self).__init__()
self.dropout = nn.Dropout(0.2)
self.out = nn.Linear(hidden_size * 2, output_size)
def forward(self, input_):
input_ = input_.view(1, -1)
input_ = self.dropout(input_)
# output = F.relu(input_)
output = F.log_softmax(self.out(input_), dim=1)
return output
def initHidden(self):
return torch.zeros(1, 1, self.hidden_size, device=device)
def load_word_embeddings(file_name, dim):
term_ids = {}
we_matrix = [] # a term_num * dim matrix for word embeddings
term_ids['NULL'] = 0
term_by_id = ['NULL']
we_matrix.append([0] * dim)
term_num = 1
with open(file_name) as FileObj:
for line in FileObj:
line = line.split()
term_ids[line[0].strip()] = term_num
term_by_id.append(line[0].strip())
norm = 1
we_matrix.append([float(i) / norm for i in line[-50:]])
term_num += 1
return term_ids, term_by_id, we_matrix
def get_glove_embedding(classes, filename):
print("loading glove embedding.....")
term_to_id, id_to_term, we_matrix = load_word_embeddings(f"glove/{filename}", 50)
embedding_matrix = np.random.rand(classes, 50)
for i in range(81):
if str(i) in term_to_id:
tid = term_to_id[str(i)]
embedding_matrix[i] = we_matrix[tid]
print("embedding loaded.")
return torch.FloatTensor(embedding_matrix)
if not os.path.exists("checkpoints"):
os.mkdir("checkpoints")