deep-learning - 网络值通过线性层变为 0
问题描述
我设计了图注意力网络。
然而,在层内的操作过程中,特征的值变得相等。
class GraphAttentionLayer(nn.Module):
## in_features = out_features = 1024
def __init__(self, in_features, out_features, dropout):
super(GraphAttentionLayer, self).__init__()
self.dropout = dropout
self.in_features = in_features
self.out_features = out_features
self.W = nn.Parameter(torch.zeros(size=(in_features, out_features)))
self.a1 = nn.Parameter(torch.zeros(size=(out_features, 1)))
self.a2 = nn.Parameter(torch.zeros(size=(out_features, 1)))
nn.init.xavier_normal_(self.W.data, gain=1.414)
nn.init.xavier_normal_(self.a1.data, gain=1.414)
nn.init.xavier_normal_(self.a2.data, gain=1.414)
self.leakyrelu = nn.LeakyReLU()
def forward(self, input, adj):
h = torch.mm(input, self.W)
a_input1 = torch.mm(h, self.a1)
a_input2 = torch.mm(h, self.a2)
a_input = torch.mm(a_input1, a_input2.transpose(1, 0))
e = self.leakyrelu(a_input)
zero_vec = torch.zeros_like(e)
attention = torch.where(adj > 0, e, zero_vec) # most of values is close to 0
attention = F.softmax(attention, dim=1) # all values are 0.0014 which is 1/707 (707^2 is the dimension of attention)
attention = F.dropout(attention, self.dropout)
return attention
“注意力”的维度是(707 x 707),我观察到在 softmax 之前注意力的值接近 0。
在 softmax 之后,所有值都是 0.0014,即 1/707。
我想知道如何保持值标准化并防止这种情况。
谢谢
解决方案
既然你说这发生在训练期间,我会假设它是在开始时。通过随机初始化,您通常会在训练过程开始时在网络末端获得接近相同的值。
当所有值或多或少相等时,softmax 的输出将1/num_elements
适用于每个元素,因此它们在您选择的维度上的总和为 1。因此,在您的情况下,您将获得1/707
所有值,在我看来,您的权重是新初始化的,并且在此阶段输出大多是随机的。
我会让它训练一段时间,然后观察它是否会发生变化。
推荐阅读
- javascript - 阿贾克斯。语法错误。无效字符
- bash - 列的总和,直到确定计数
- reactjs - NavLink 强制更新活动路线?
- mysql - 错误:#1932 无法访问 localhost xampp 中的数据库
- oracle - 存储过程中的循环总是永远打印最后一个值
- java - 如何为 JTOpen 指定本地自定义 SSL 信任库?
- javascript - 设计适合移动设备的网站有哪些最佳实践?
- laravel - Laravel 分页获取下一页
- react-native - 防止终端上的 Require Cycle 警告
- javascript - 如何在Javascript中每月取消一项工作