-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
47 lines (43 loc) · 1.34 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
import torch.nn as nn
import numpy as np
def make_input(query_vector, sup_doc_vector, inf_doc_vector):
"""
Make (query, document-pair) input for model.
"""
return np.array([query_vector, sup_doc_vector, inf_doc_vector], dtype=np.float32).flatten()
# Model definition
model = nn.Sequential(
nn.Linear(3*768, 256),
nn.ReLU(),
nn.Linear(256, 256),
nn.ReLU(),
nn.Linear(256, 256),
nn.ReLU(),
nn.Linear(256, 1),
nn.Sigmoid(),
)
learning_rate = 0.01
criterion = nn.BCELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
def train(pos_train_data, neg_train_data, num_epochs):
for epoch in range(num_epochs):
losses = []
# train positive pairs
for data in pos_train_data:
output = model(data)
loss = criterion(output, torch.tensor([1.0]))
optimizer.zero_grad()
loss.backward()
optimizer.step()
losses.append(loss.item())
# train negative pairs too
for data in neg_train_data:
output = model(data)
loss = criterion(output, torch.tensor([0.0]))
optimizer.zero_grad()
loss.backward()
optimizer.step()
losses.append(loss.item())
if (epoch + 1) == num_epochs:
print(f'Epoch [{epoch + 1}/{num_epochs}], Loss: {(sum(losses) / len(losses)):.4f}')