代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
# file: ian.py
# author: songyouwei <youwei0314@gmail.com>
# 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 IAN(nn.Module):
def __init__(self, embedding_matrix, opt):
super(IAN, self).__init__()
self.opt = opt
self.embed = nn.Embedding.from_pretrained(torch.tensor(embedding_matrix, dtype=torch.float))
self.lstm_context = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True)
self.lstm_aspect = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True)
self.attention_aspect = Attention(opt.hidden_dim, score_function='bi_linear')
self.attention_context = Attention(opt.hidden_dim, score_function='bi_linear')
self.dense = nn.Linear(opt.hidden_dim*2, opt.polarities_dim)
def forward(self, inputs):
text_raw_indices, aspect_indices = inputs[0], inputs[1]
text_raw_len = torch.sum(text_raw_indices != 0, dim=-1)
aspect_len = torch.sum(aspect_indices != 0, dim=-1)
context = self.embed(text_raw_indices)
aspect = self.embed(aspect_indices)
context, (_, _) = self.lstm_context(context, text_raw_len)
aspect, (_, _) = self.lstm_aspect(aspect, aspect_len)
aspect_len = torch.tensor(aspect_len, dtype=torch.float).to(self.opt.device)
aspect_pool = torch.sum(aspect, dim=1)
aspect_pool = torch.div(aspect_pool, aspect_len.view(aspect_len.size(0), 1))
text_raw_len = torch.tensor(text_raw_len, dtype=torch.float).to(self.opt.device)
context_pool = torch.sum(context, dim=1)
context_pool = torch.div(context_pool, text_raw_len.view(text_raw_len.size(0), 1))
aspect_final, _ = self.attention_aspect(aspect, context_pool)
aspect_final = aspect_final.squeeze(dim=1)
context_final, _ = self.attention_context(context, aspect_pool)
context_final = context_final.squeeze(dim=1)
x = torch.cat((aspect_final, context_final), dim=-1)
out = self.dense(x)
return out
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。