本文整理汇总了Python中torch.diag方法的典型用法代码示例。如果您正苦于以下问题:Python torch.diag方法的具体用法?Python torch.diag怎么用?Python torch.diag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch
的用法示例。
在下文中一共展示了torch.diag方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fuse_conv_and_bn
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def fuse_conv_and_bn(conv, bn):
# https://tehnokv.com/posts/fusing-batchnorm-and-conv/
with torch.no_grad():
# init
fusedconv = torch.nn.Conv2d(conv.in_channels,
conv.out_channels,
kernel_size=conv.kernel_size,
stride=conv.stride,
padding=conv.padding,
bias=True)
# prepare filters
w_conv = conv.weight.clone().view(conv.out_channels, -1)
w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var)))
fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.size()))
# prepare spatial bias
if conv.bias is not None:
b_conv = conv.bias
else:
b_conv = torch.zeros(conv.weight.size(0))
b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps))
fusedconv.bias.copy_(b_conv + b_bn)
return fusedconv
示例2: _mix_rbf_kernel
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def _mix_rbf_kernel(X, Y, sigma_list):
assert(X.size(0) == Y.size(0))
m = X.size(0)
Z = torch.cat((X, Y), 0)
ZZT = torch.mm(Z, Z.t())
diag_ZZT = torch.diag(ZZT).unsqueeze(1)
Z_norm_sqr = diag_ZZT.expand_as(ZZT)
exponent = Z_norm_sqr - 2 * ZZT + Z_norm_sqr.t()
K = 0.0
for sigma in sigma_list:
gamma = 1.0 / (2 * sigma**2)
K += torch.exp(-gamma * exponent)
return K[:m, :m], K[:m, m:], K[m:, m:], len(sigma_list)
示例3: construct_diag
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def construct_diag(x: torch.Tensor):
"""
Constructs a diagonal matrix based on batched data. Solution found here:
https://stackoverflow.com/questions/47372508/how-to-construct-a-3d-tensor-where-every-2d-sub-tensor-is-a-diagonal-matrix-in-p
Do note that it only considers the last axis.
:param x: The tensor
"""
if x.dim() < 1:
return x
elif x.shape[-1] < 2:
return x.unsqueeze(-1)
elif x.dim() < 2:
return torch.diag(x)
b = torch.eye(x.size(-1), device=x.device)
c = x.unsqueeze(-1).expand(*x.size(), x.size(-1))
return c * b
示例4: test_UnscentedTransform2D
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def test_UnscentedTransform2D(self):
# ===== 2D model ===== #
mat = torch.eye(2)
scale = torch.diag(mat)
norm = Normal(0., 1.)
mvn = MultivariateNormal(torch.zeros(2), torch.eye(2))
mvnlinear = AffineProcess((fmvn, g), (mat, scale), mvn, mvn)
mvnoblinear = AffineObservations((fomvn, gomvn), (1.,), norm)
mvnmodel = StateSpaceModel(mvnlinear, mvnoblinear)
# ===== Perform unscented transform ===== #
uft = UnscentedFilterTransform(mvnmodel)
res = uft.initialize(3000)
p = uft.predict(res)
c = uft.correct(0., p)
assert isinstance(c.x_dist(), MultivariateNormal) and c.x_dist().mean.shape == torch.Size([3000, 2])
示例5: __init__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def __init__(self,in_channel):
super(InvConv,self).__init__()
weight=np.random.randn(in_channel,in_channel)
q,_=linalg.qr(weight)
w_p,w_l,w_u=linalg.lu(q.astype(np.float32))
w_s=np.diag(w_u)
w_u=np.triu(w_u,1)
u_mask=np.triu(np.ones_like(w_u),1)
l_mask=u_mask.T
self.register_buffer('w_p',torch.from_numpy(w_p))
self.register_buffer('u_mask',torch.from_numpy(u_mask))
self.register_buffer('l_mask',torch.from_numpy(l_mask))
self.register_buffer('l_eye',torch.eye(l_mask.shape[0]))
self.register_buffer('s_sign',torch.sign(torch.from_numpy(w_s)))
self.w_l=torch.nn.Parameter(torch.from_numpy(w_l))
self.w_s=torch.nn.Parameter(torch.log(1e-7+torch.abs(torch.from_numpy(w_s))))
self.w_u=torch.nn.Parameter(torch.from_numpy(w_u))
self.weight=None
self.invweight=None
return
示例6: forward
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def forward(self, input):
laplacian = input.exp() + self.eps
output = input.clone()
for b in range(input.size(0)):
lap = laplacian[b].masked_fill(
Variable(torch.eye(input.size(1)).cuda().ne(0)), 0)
lap = -lap + torch.diag(lap.sum(0))
# store roots on diagonal
lap[0] = input[b].diag().exp()
inv_laplacian = lap.inverse()
factor = inv_laplacian.diag().unsqueeze(1)\
.expand_as(input[b]).transpose(0, 1)
term1 = input[b].exp().mul(factor).clone()
term2 = input[b].exp().mul(inv_laplacian.transpose(0, 1)).clone()
term1[:, 0] = 0
term2[0] = 0
output[b] = term1 - term2
roots_output = input[b].diag().exp().mul(
inv_laplacian.transpose(0, 1)[0])
output[b] = output[b] + torch.diag(roots_output)
return output
示例7: __expm__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def __expm__(self, matrix, symmetric):
r"""Calculates matrix exponential.
Args:
matrix (Tensor): Matrix to take exponential of.
symmetric (bool): Specifies whether the matrix is symmetric.
:rtype: (:class:`Tensor`)
"""
if symmetric:
e, V = torch.symeig(matrix, eigenvectors=True)
diff_mat = V @ torch.diag(e.exp()) @ V.t()
else:
diff_mat_np = expm(matrix.cpu().numpy())
diff_mat = torch.Tensor(diff_mat_np).to(matrix.device)
return diff_mat
示例8: regularizer_orth2
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def regularizer_orth2(m):
"""
# ----------------------------------------
# Applies regularization to the training by performing the
# orthogonalization technique described in the paper
# This function is to be called by the torch.nn.Module.apply() method,
# which applies svd_orthogonalization() to every layer of the model.
# usage: net.apply(regularizer_orth2)
# ----------------------------------------
"""
classname = m.__class__.__name__
if classname.find('Conv') != -1:
w = m.weight.data.clone()
c_out, c_in, f1, f2 = w.size()
# dtype = m.weight.data.type()
w = w.permute(2, 3, 1, 0).contiguous().view(f1*f2*c_in, c_out)
u, s, v = torch.svd(w)
s_mean = s.mean()
s[s > 1.5*s_mean] = s[s > 1.5*s_mean] - 1e-4
s[s < 0.5*s_mean] = s[s < 0.5*s_mean] + 1e-4
w = torch.mm(torch.mm(u, torch.diag(s)), v.t())
m.weight.data = w.view(f1, f2, c_in, c_out).permute(3, 2, 0, 1) # .type(dtype)
else:
pass
示例9: get_loadings
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def get_loadings(self) -> np.ndarray:
"""Extract per-gene weights (for each Z, shape is genes by dim(Z)) in the linear decoder."""
# This is BW, where B is diag(b) batch norm, W is weight matrix
if self.use_batch_norm is True:
w = self.decoder.factor_regressor.fc_layers[0][0].weight
bn = self.decoder.factor_regressor.fc_layers[0][1]
sigma = torch.sqrt(bn.running_var + bn.eps)
gamma = bn.weight
b = gamma / sigma
bI = torch.diag(b)
loadings = torch.matmul(bI, w)
else:
loadings = self.decoder.factor_regressor.fc_layers[0][0].weight
loadings = loadings.detach().cpu().numpy()
if self.n_batch > 1:
loadings = loadings[:, : -self.n_batch]
return loadings
示例10: _regularizer
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def _regularizer(self, mu_z, std_z):
kld_z = self.z_prior_stdv.log() - std_z.log() + (std_z ** 2 + (mu_z.pow(2) - self.z_prior_mean)) /\
(2 * self.z_prior_stdv.pow(2)) - 0.5
regularizer_loss = kld_z.sum()
regularizer_loss = self.beta * regularizer_loss
cov_mu_z = self._get_covariance_mu_z(mu_z)
if self.mode == "i":
dipvae_regularizer_loss = self._get_dipvae_regularizer(cov_mu_z, self.lambda_offdiag, self.lambda_diag)
elif self.mode == "ii":
cov_z = cov_mu_z + torch.mean(torch.diag(std_z**2), dim=0)
dipvae_regularizer_loss = self._get_dipvae_regularizer(cov_z, self.lambda_offdiag, self.lambda_diag)
else:
raise NotImplementedError("Unsupported dipvae mode.")
return regularizer_loss + dipvae_regularizer_loss
示例11: __init__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def __init__(self, num_inputs):
super(LUInvertibleMM, self).__init__()
self.W = torch.Tensor(num_inputs, num_inputs)
nn.init.orthogonal_(self.W)
self.L_mask = torch.tril(torch.ones(self.W.size()), -1)
self.U_mask = self.L_mask.t().clone()
P, L, U = sp.linalg.lu(self.W.numpy())
self.P = torch.from_numpy(P)
self.L = nn.Parameter(torch.from_numpy(L))
self.U = nn.Parameter(torch.from_numpy(U))
S = np.diag(U)
sign_S = np.sign(S)
log_S = np.log(abs(S))
self.sign_S = torch.from_numpy(sign_S)
self.log_S = nn.Parameter(torch.from_numpy(log_S))
self.I = torch.eye(self.L.size(0))
示例12: forward
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def forward(self, inputs, cond_inputs=None, mode='direct'):
if str(self.L_mask.device) != str(self.L.device):
self.L_mask = self.L_mask.to(self.L.device)
self.U_mask = self.U_mask.to(self.L.device)
self.I = self.I.to(self.L.device)
self.P = self.P.to(self.L.device)
self.sign_S = self.sign_S.to(self.L.device)
L = self.L * self.L_mask + self.I
U = self.U * self.U_mask + torch.diag(
self.sign_S * torch.exp(self.log_S))
W = self.P @ L @ U
if mode == 'direct':
return inputs @ W, self.log_S.sum().unsqueeze(0).unsqueeze(
0).repeat(inputs.size(0), 1)
else:
return inputs @ torch.inverse(
W), -self.log_S.sum().unsqueeze(0).unsqueeze(0).repeat(
inputs.size(0), 1)
示例13: test_np
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def test_np():
npr.seed(0)
nx, nineq, neq = 4, 6, 7
Q = npr.randn(nx, nx)
G = npr.randn(nineq, nx)
A = npr.randn(neq, nx)
D = np.diag(npr.rand(nineq))
K_ = np.bmat((
(Q, np.zeros((nx, nineq)), G.T, A.T),
(np.zeros((nineq, nx)), D, np.eye(nineq), np.zeros((nineq, neq))),
(G, np.eye(nineq), np.zeros((nineq, nineq + neq))),
(A, np.zeros((neq, nineq + nineq + neq)))
))
K = block((
(Q, 0, G.T, A.T),
(0, D, 'I', 0),
(G, 'I', 0, 0),
(A, 0, 0, 0)
))
assert np.allclose(K_, K)
示例14: loss_l2
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def loss_l2(self, l2=0):
"""L2 loss centered around mu_init, scaled optionally per-source.
In other words, diagonal Tikhonov regularization,
||D(\mu-\mu_{init})||_2^2
where D is diagonal.
Args:
- l2: A float or np.array representing the per-source regularization
strengths to use
"""
if isinstance(l2, (int, float)):
D = l2 * torch.eye(self.d)
else:
D = torch.diag(torch.from_numpy(l2))
# Note that mu is a matrix and this is the *Frobenius norm*
return torch.norm(D @ (self.mu - self.mu_init)) ** 2
示例15: _set_class_balance
# 需要导入模块: import torch [as 别名]
# 或者: from torch import diag [as 别名]
def _set_class_balance(self, class_balance, Y_dev):
"""Set a prior for the class balance
In order of preference:
1) Use user-provided class_balance
2) Estimate balance from Y_dev
3) Assume uniform class distribution
"""
if class_balance is not None:
self.p = np.array(class_balance)
elif Y_dev is not None:
class_counts = Counter(Y_dev)
sorted_counts = np.array([v for k, v in sorted(class_counts.items())])
self.p = sorted_counts / sum(sorted_counts)
else:
self.p = (1 / self.k) * np.ones(self.k)
self.P = torch.diag(torch.from_numpy(self.p)).float()