-
Notifications
You must be signed in to change notification settings - Fork 0
/
roan_ded.py
111 lines (81 loc) · 4.42 KB
/
roan_ded.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
99
100
101
102
103
104
105
106
107
108
109
110
111
# Copyright (c) 2018-present, Royal Bank of Canada.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from params import Params
from dataset import Dataset
from relation_emb import Rel_time_emb
class RoAN_DED(torch.nn.Module):
def __init__(self, dataset, params):
super(RoAN_DED, self).__init__()
self.dataset = dataset
self.params = params
# Creating static embeddings.
self.ent_embs = nn.Embedding(dataset.numEnt(), params.s_emb_dim).cuda()
self.rel_embs = nn.Embedding(dataset.numRel(), params.emb_dim).cuda()
# Creating and initializing the temporal embeddings for the entities
self.create_time_embedds()
# Setting the non-linearity to be used for temporal part of the embedding
self.time_nl = torch.sin
nn.init.xavier_uniform_(self.ent_embs.weight)
nn.init.xavier_uniform_(self.rel_embs.weight)
def create_time_embedds(self):
# frequency embeddings for the entities
self.m_freq = nn.Embedding(self.dataset.numEnt(), self.params.t_emb_dim).cuda()
self.d_freq = nn.Embedding(self.dataset.numEnt(), self.params.t_emb_dim).cuda()
self.y_freq = nn.Embedding(self.dataset.numEnt(), self.params.t_emb_dim).cuda()
nn.init.xavier_uniform_(self.m_freq.weight)
nn.init.xavier_uniform_(self.d_freq.weight)
nn.init.xavier_uniform_(self.y_freq.weight)
# phi embeddings for the entities
self.m_phi = nn.Embedding(self.dataset.numEnt(), self.params.t_emb_dim).cuda()
self.d_phi = nn.Embedding(self.dataset.numEnt(), self.params.t_emb_dim).cuda()
self.y_phi = nn.Embedding(self.dataset.numEnt(), self.params.t_emb_dim).cuda()
nn.init.xavier_uniform_(self.m_phi.weight)
nn.init.xavier_uniform_(self.d_phi.weight)
nn.init.xavier_uniform_(self.y_phi.weight)
# amplitude embeddings for the entities
self.m_amp = nn.Embedding(self.dataset.numEnt(), self.params.t_emb_dim).cuda()
self.d_amp = nn.Embedding(self.dataset.numEnt(), self.params.t_emb_dim).cuda()
self.y_amp = nn.Embedding(self.dataset.numEnt(), self.params.t_emb_dim).cuda()
nn.init.xavier_uniform_(self.m_amp.weight)
nn.init.xavier_uniform_(self.d_amp.weight)
nn.init.xavier_uniform_(self.y_amp.weight)
self.Rel_emb = Rel_time_emb(self.dataset, self.params)
def get_time_embedd(self, entities, year, month, day):
y = self.y_amp(entities)*self.time_nl(self.y_freq(entities)*year + self.y_phi(entities))
m = self.m_amp(entities)*self.time_nl(self.m_freq(entities)*month + self.m_phi(entities))
d = self.d_amp(entities)*self.time_nl(self.d_freq(entities)*day + self.d_phi(entities))
return y+m+d
def getEmbeddings(self, batch, ent_type, train_or_test):
heads, rels, tails, years, months, days, yearsid, monthsid, daysid, hiss = batch
years = years.view(-1,1)
months = months.view(-1,1)
days = days.view(-1,1)
h,r,t = self.ent_embs(heads), self.rel_embs(rels), self.ent_embs(tails)
h_t = self.get_time_embedd(heads, years, months, days)
t_t = self.get_time_embedd(tails, years, months, days)
h = torch.cat((h,h_t), 1)
t = torch.cat((t,t_t), 1)
pre_rel_emb = self.Rel_emb.getRelEmbeddings(batch, ent_type, train_or_test)
r = (1-self.params.alp)*r + self.params.alp*pre_rel_emb
return h,r,t
def forward(self, batch1=None, batch2=None, train_or_test="train", ent_type="subs"):
if batch2 == None:
h_embs, r_embs, t_embs = self.getEmbeddings(batch1, ent_type, train_or_test)
scores = (h_embs * r_embs) * t_embs
else:
h_embs, r_embs, t_embs = self.getEmbeddings(batch1, "objs", train_or_test)
scores1 = (h_embs * r_embs) * t_embs
h_embs2, r_embs2, t_embs2 = self.getEmbeddings(batch2, "subs", train_or_test)
scores2 = (h_embs2 * r_embs2) * t_embs2
scores = torch.cat((scores1, scores2), 0)
scores = F.dropout(scores, p=self.params.dropout, training=self.training)
scores = torch.sum(scores, dim=1)
return scores