import torch
import torch.nn as nn
import torch.nn.functional as F
import pandas as pd
import numpy as np
import torch.utils.data as data
import torch.autograd as autograd
from MyHAN.utils import matrix_mul, element_wise_mul
import sklearn.metrics as metrics
from math import sqrt
class selfAttn(nn.Module):
def __init__(self, dim_in: int, dim_k: int, dim_v: int):
super(selfAttn, self).__init__()
self.dim_in = dim_in
self.dim_k = dim_k
self.dim_v = dim_v
self.linear_q = nn.Linear(dim_in, dim_k, bias=False)
self.linear_k = nn.Linear(dim_in, dim_k, bias=False)
self.linear_v = nn.Linear(dim_in, dim_v, bias=False)
self._norm_fact = 1 / sqrt(dim_k)
def forward(self, x):
width, height = x.size()
dim_in = self.dim_in
q = self.linear_q(x)
k = self.linear_k(x)
v = self.linear_v(x)
dist = torch.bmm(q.unsqueeze(0), k.unsqueeze(0).transpose(1, 2)) * self._norm_fact
dist = F.softmax(dist, dim=-1)
attn = torch.bmm(dist, v.unsqueeze(0))
return attn
class HierAttnNet(nn.Module):
def __init__(self, word_hidden_size, sent_hidden_size, batch_size, num_classes, embed_size
求大佬帮忙,神经网络loss不变0.06931,准确率二分类为0.5
最新推荐文章于 2024-08-14 16:39:36 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)