本文整理汇总了Python中torch.potrf方法的典型用法代码示例。如果您正苦于以下问题:Python torch.potrf方法的具体用法?Python torch.potrf怎么用?Python torch.potrf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch
的用法示例。
在下文中一共展示了torch.potrf方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import potrf [as 别名]
def __init__(self, nFeatures, args):
super().__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())
Q = 1e-8*torch.eye(nHidden)
Q[:nFeatures,:nFeatures] = torch.eye(nFeatures)
self.L = Variable(torch.potrf(Q))
self.D = Parameter(0.3*torch.randn(nFeatures-1, nFeatures))
# self.lam = Parameter(20.*torch.ones(1))
self.h = Variable(torch.zeros(nineq))
self.nFeatures = nFeatures
self.nHidden = nHidden
self.neq = neq
self.nineq = nineq
self.args = args
示例2: get_cov
# 需要导入模块: import torch [as 别名]
# 或者: from torch import potrf [as 别名]
def get_cov(self, ix=None):
if ix is None:
ix = torch.arange(0, self.N)
return torch.potrf(self.kernel(self.X[ix])
+ torch.eye(ix.numel())
*transform_forward(self.variance),
upper=False)
示例3: pre_factor_kkt
# 需要导入模块: import torch [as 别名]
# 或者: from torch import potrf [as 别名]
def pre_factor_kkt(Q, G, A):
""" Perform all one-time factorizations and cache relevant matrix products"""
nineq, nz, neq, _ = get_sizes(G, A)
# S = [ A Q^{-1} A^T A Q^{-1} G^T ]
# [ G Q^{-1} A^T G Q^{-1} G^T + D^{-1} ]
U_Q = torch.potrf(Q)
# partial cholesky of S matrix
U_S = torch.zeros(neq + nineq, neq + nineq).type_as(Q)
G_invQ_GT = torch.mm(G, torch.potrs(G.t(), U_Q))
R = G_invQ_GT
if neq > 0:
invQ_AT = torch.potrs(A.t(), U_Q)
A_invQ_AT = torch.mm(A, invQ_AT)
G_invQ_AT = torch.mm(G, invQ_AT)
# TODO: torch.potrf sometimes says the matrix is not PSD but
# numpy does? I filed an issue at
# https://github.com/pytorch/pytorch/issues/199
try:
U11 = torch.potrf(A_invQ_AT)
except:
U11 = torch.Tensor(np.linalg.cholesky(
A_invQ_AT.cpu().numpy())).type_as(A_invQ_AT)
# TODO: torch.trtrs is currently not implemented on the GPU
# and we are using gesv as a workaround.
U12 = torch.gesv(G_invQ_AT.t(), U11.t())[0]
U_S[:neq, :neq] = U11
U_S[:neq, neq:] = U12
R -= torch.mm(U12.t(), U12)
return U_Q, U_S, R
示例4: factor_kkt
# 需要导入模块: import torch [as 别名]
# 或者: from torch import potrf [as 别名]
def factor_kkt(U_S, R, d):
""" Factor the U22 block that we can only do after we know D. """
nineq = R.size(0)
U_S[-nineq:, -nineq:] = torch.potrf(R + torch.diag(1 / d.cpu()).type_as(d))
示例5: factor_solve_kkt
# 需要导入模块: import torch [as 别名]
# 或者: from torch import potrf [as 别名]
def factor_solve_kkt(Q, D, G, A, rx, rs, rz, ry):
nineq, nz, neq, _ = get_sizes(G, A)
if neq > 0:
H_ = torch.cat([torch.cat([Q, torch.zeros(nz, nineq).type_as(Q)], 1),
torch.cat([torch.zeros(nineq, nz).type_as(Q), D], 1)], 0)
A_ = torch.cat([torch.cat([G, torch.eye(nineq).type_as(Q)], 1),
torch.cat([A, torch.zeros(neq, nineq).type_as(Q)], 1)], 0)
g_ = torch.cat([rx, rs], 0)
h_ = torch.cat([rz, ry], 0)
else:
H_ = torch.cat([torch.cat([Q, torch.zeros(nz, nineq).type_as(Q)], 1),
torch.cat([torch.zeros(nineq, nz).type_as(Q), D], 1)], 0)
A_ = torch.cat([G, torch.eye(nineq).type_as(Q)], 1)
g_ = torch.cat([rx, rs], 0)
h_ = rz
U_H_ = torch.potrf(H_)
invH_A_ = torch.potrs(A_.t(), U_H_)
invH_g_ = torch.potrs(g_.view(-1, 1), U_H_).view(-1)
S_ = torch.mm(A_, invH_A_)
U_S_ = torch.potrf(S_)
t_ = torch.mv(A_, invH_g_).view(-1, 1) - h_
w_ = -torch.potrs(t_, U_S_).view(-1)
v_ = torch.potrs(-g_.view(-1, 1) - torch.mv(A_.t(), w_), U_H_).view(-1)
return v_[:nz], v_[nz:], w_[:nineq], w_[nineq:] if neq > 0 else None
示例6: cholesky
# 需要导入模块: import torch [as 别名]
# 或者: from torch import potrf [as 别名]
def cholesky(self, A, lower=True, warn=False, correct=True):
return torch.potrf(A, upper=not lower)
# Tensorflow interface
示例7: U_UBi_Shb
# 需要导入模块: import torch [as 别名]
# 或者: from torch import potrf [as 别名]
def U_UBi_Shb(self, Vs, vs):
# compute U and V
V = torch.cat([torch.sqrt(vs[i]) * V for i, V in enumerate(Vs)], 1)
U = V / torch.sqrt(vs[-1])
eye = torch.eye(U.shape[1]).cuda()
B = torch.mm(torch.transpose(U, 0, 1), U) + eye
# cholB = torch.potrf(B, upper=False)
# Bi = torch.potri(cholB, upper=False)
Ub, Shb, Vb = torch.svd(B)
# Bi = (Vb / Shb).mm(torch.transpose(Vb, 0, 1))
Bi = torch.inverse(B)
UBi = torch.mm(U, Bi)
return U, UBi, Shb