forked from AlbertoPaz/Dan-ABSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ram.py
49 lines (42 loc) · 2.13 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
# -*- coding: utf-8 -*-
# file: ram.py
# author: songyouwei <[email protected]>
# Copyright (C) 2018. All Rights Reserved.
from layers.dynamic_rnn import DynamicLSTM
from layers.attention import Attention
import torch
import torch.nn as nn
class RAM(nn.Module):
def locationed_memory(self, memory, memory_len):
# here we just simply calculate the location vector in Model2's manner
for i in range(memory.size(0)):
for idx in range(memory_len[i]):
memory[i][idx] *= (1-float(idx)/int(memory_len[i]))
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.bi_lstm_aspect = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True, bidirectional=True)
self.attention = Attention(opt.hidden_dim*2, score_function='mlp')
self.gru_cell = nn.GRUCell(opt.hidden_dim*2, opt.hidden_dim*2)
self.dense = nn.Linear(opt.hidden_dim*2, opt.polarities_dim)
def forward(self, inputs):
text_raw_indices, aspect_indices = inputs[0], inputs[1]
memory_len = torch.sum(text_raw_indices != 0, dim=-1)
aspect_len = torch.sum(aspect_indices != 0, dim=-1)
nonzeros_aspect = torch.tensor(aspect_len, dtype=torch.float).to(self.opt.device)
memory = self.embed(text_raw_indices)
memory, (_, _) = self.bi_lstm_context(memory, memory_len)
# memory = self.locationed_memory(memory, memory_len)
aspect = self.embed(aspect_indices)
aspect, (_, _) = self.bi_lstm_aspect(aspect, aspect_len)
aspect = torch.sum(aspect, dim=1)
aspect = torch.div(aspect, nonzeros_aspect.view(nonzeros_aspect.size(0), 1))
et = aspect
for _ in range(self.opt.hops):
it_al = self.attention(memory, et).squeeze(dim=1)
et = self.gru_cell(it_al, et)
out = self.dense(et)
return out