forked from songyouwei/ABSA-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ram.py
65 lines (58 loc) · 2.97 KB
/
ram.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
# -*- coding: utf-8 -*-
# file: ram.py
# author: songyouwei <[email protected]>
# Copyright (C) 2018. All Rights Reserved.
from layers.dynamic_rnn import DynamicLSTM
import torch
import torch.nn as nn
import torch.nn.functional as F
class RAM(nn.Module):
def locationed_memory(self, memory, memory_len, left_len, aspect_len):
u = torch.zeros(memory.size(0), memory.size(1), 1).to(self.opt.device)
for i in range(memory.size(0)):
for idx in range(memory_len[i]):
aspect_start = left_len[i]
if idx < aspect_start:
l = aspect_start - idx # l = absolute distance to the aspect
u[i][idx][0] = idx - aspect_start
elif idx < aspect_start + aspect_len[i]:
l = 0
else:
l = idx - aspect_start - aspect_len[i] + 1
u[i][idx][0] = idx - aspect_start - aspect_len[i] + 1
memory[i][idx] *= (1-float(l)/int(memory_len[i]))
memory = torch.cat([memory, u], dim=2)
return memory
def __init__(self, embedding_matrix, opt):
super(RAM, self).__init__()
self.opt = opt
self.embed = nn.Embedding.from_pretrained(torch.tensor(embedding_matrix, dtype=torch.float))
self.bi_lstm_context = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True, bidirectional=True)
self.att_linear = nn.Linear(opt.hidden_dim*2 + 1 + opt.embed_dim*2, 1)
self.gru_cell = nn.GRUCell(opt.hidden_dim*2 + 1, opt.embed_dim)
self.dense = nn.Linear(opt.embed_dim, opt.polarities_dim)
def forward(self, inputs):
text_raw_indices, aspect_indices, text_left_indices = inputs[0], inputs[1], inputs[2]
left_len = torch.sum(text_left_indices != 0, dim=-1)
memory_len = torch.sum(text_raw_indices != 0, dim=-1)
aspect_len = torch.sum(aspect_indices != 0, dim=-1)
nonzeros_aspect = aspect_len.float()
memory = self.embed(text_raw_indices)
memory, (_, _) = self.bi_lstm_context(memory, memory_len)
memory = self.locationed_memory(memory, memory_len, left_len, aspect_len)
aspect = self.embed(aspect_indices)
aspect = torch.sum(aspect, dim=1)
aspect = torch.div(aspect, nonzeros_aspect.unsqueeze(-1))
et = torch.zeros_like(aspect).to(self.opt.device)
batch_size = memory.size(0)
seq_len = memory.size(1)
for _ in range(self.opt.hops):
g = self.att_linear(torch.cat([memory,
torch.zeros(batch_size, seq_len, self.opt.embed_dim).to(self.opt.device) + et.unsqueeze(1),
torch.zeros(batch_size, seq_len, self.opt.embed_dim).to(self.opt.device) + aspect.unsqueeze(1)],
dim=-1))
alpha = F.softmax(g, dim=1)
i = torch.bmm(alpha.transpose(1, 2), memory).squeeze(1)
et = self.gru_cell(i, et)
out = self.dense(et)
return out