models.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:optnet 作者: locuslab 项目源码 文件源码
def __init__(self, nFeatures, args):
        super(OptNet, self).__init__()

        nHidden, neq, nineq = 2*nFeatures-1,0,2*nFeatures-2
        assert(neq==0)

        self.fc1 = nn.Linear(nFeatures, nHidden)
        self.M = Variable(torch.tril(torch.ones(nHidden, nHidden)).cuda())

        if args.tvInit:
            Q = 1e-8*torch.eye(nHidden)
            Q[:nFeatures,:nFeatures] = torch.eye(nFeatures)
            self.L = Parameter(torch.potrf(Q))

            D = torch.zeros(nFeatures-1, nFeatures)
            D[:nFeatures-1,:nFeatures-1] = torch.eye(nFeatures-1)
            D[:nFeatures-1,1:nFeatures] -= torch.eye(nFeatures-1)
            G_ = block((( D, -torch.eye(nFeatures-1)),
                        (-D, -torch.eye(nFeatures-1))))
            self.G = Parameter(G_)
            self.s0 = Parameter(torch.ones(2*nFeatures-2)+1e-6*torch.randn(2*nFeatures-2))
            G_pinv = (G_.t().mm(G_)+1e-5*torch.eye(nHidden)).inverse().mm(G_.t())
            self.z0 = Parameter(-G_pinv.mv(self.s0.data)+1e-6*torch.randn(nHidden))

            lam = 21.21
            W_fc1, b_fc1 = self.fc1.weight, self.fc1.bias
            W_fc1.data[:,:] = 1e-3*torch.randn((2*nFeatures-1, nFeatures))
            # W_fc1.data[:,:] = 0.0
            W_fc1.data[:nFeatures,:nFeatures] += -torch.eye(nFeatures)
            # b_fc1.data[:] = torch.zeros(2*nFeatures-1)
            b_fc1.data[:] = 0.0
            b_fc1.data[nFeatures:2*nFeatures-1] = lam
        else:
            self.L = Parameter(torch.tril(torch.rand(nHidden, nHidden)))
            self.G = Parameter(torch.Tensor(nineq,nHidden).uniform_(-1,1))
            self.z0 = Parameter(torch.zeros(nHidden))
            self.s0 = Parameter(torch.ones(nineq))

        self.nFeatures = nFeatures
        self.nHidden = nHidden
        self.neq = neq
        self.nineq = nineq
        self.args = args
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号