如何在成本函数中包含参数向量之间的相关性?

问题描述

我有以下模型:

class Model(nn.Module):
    def __init__(self,dim_in,lambda_=.3):
        super(FeatExtractorGR,self).__init__()

       '''The linear transform layer'''
        self.lt = nn.Linear(in_features = dim_in,out_features = 10,bias=True)
    
    '''The encoder'''
    self.feature_extractor = \
        nn.Sequential(
            #nn.Linear(in_features = 10,out_features = 30,bias=True),nn.Batchnorm1d(10),nn.ReLU(),nn.Linear(in_features = 10,out_features = 20,nn.Batchnorm1d(20),nn.Linear(in_features = 20,nn.ReLU()
        )

def forward(self,x):
    transformed = self.lt(x)
    return self.feature_extractor(transformed)

我想强制线性变换层的权重向量不相关。我试图在成本函数的向量中包含点积:

        params=list(model.lt.parameters())[0]
        dotprod=torch.tensordot(params,params,dims=([1],[1])).abs().fill_diagonal_(0).sum()/2

        loss = other_losses + dotprod * weight

但这不起作用,即使 weight 非常高。 lt 层的权重向量仍然高度相关。我也尝试删除 other_losses,但没有效果。我做错了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)