如何在pytorch中实现线性层的对角线数据

How to implement a diagonal data for a linear layer in pytorch

我想在 pytorch 中有一个只缩放数据的网络。

我要求的数学符号是:

这意味着如果我的输入是 [1, 2] 而我的输出是 [2, 6]。 那么线性层将如下所示:

[ [ 2, 0],
  [ 0, 3] ].

我用 pytorch 编写了这个网络:

class ScalingNetwork(nn.Module):
    def __init__(self, input_shape, output_shape):
        super().__init__()
        self.linear_layer = nn.Linear(in_features=input_shape, out_features=output_shape)
        self.mask = torch.diag(torch.ones(input_shape))
        self.linear_layer.weight.data = self.linear_layer.weight * self.mask
        self.linear_layer.weight.requires_grad = True

    def get_tranformation_matrix(self):
        return self.linear_layer.weight


    def forward(self, X):
        X = self.linear_layer(X)
        return X

但是在训练结束时,我的self.linear不是对角线的。 我做错了什么?

这里似乎有一个明显的限制,即 self.linear_layer 必须是平方矩阵。您可以使用对角矩阵 self.mask 将正向传递中的所有非对角元素归零:

class ScalingNetwork(nn.Module):
    def __init__(self, in_features):
        super().__init__()
        self.linear = nn.Linear(in_features, in_features, bias=False)
        self.mask = torch.eye(in_features, dtype=bool)

    def forward(self, x):
        self.linear.weight.data *= self.mask
        print(self.linear.weight)
        x = self.linear(x)
        return x

例如:

>>> m = ScalingNetwork(5)

>>> m(torch.rand(1,5))
Parameter containing:
tensor([[-0.2987, -0.0000, -0.0000, -0.0000, -0.0000],
        [ 0.0000, -0.1042, -0.0000, -0.0000, -0.0000],
        [-0.0000,  0.0000, -0.4267,  0.0000, -0.0000],
        [ 0.0000, -0.0000, -0.0000,  0.1758,  0.0000],
        [ 0.0000,  0.0000,  0.0000, -0.0000, -0.3208]], requires_grad=True)
tensor([[-0.1032, -0.0087, -0.1709,  0.0035, -0.1496]], grad_fn=<MmBackward0>)