本文整理匯總了Python中torch.diag_embed方法的典型用法代碼示例。如果您正苦於以下問題:Python torch.diag_embed方法的具體用法?Python torch.diag_embed怎麽用?Python torch.diag_embed使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch
的用法示例。
在下文中一共展示了torch.diag_embed方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: init_action_pd
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def init_action_pd(ActionPD, pdparam):
'''
Initialize the action_pd for discrete or continuous actions:
- discrete: action_pd = ActionPD(logits)
- continuous: action_pd = ActionPD(loc, scale)
'''
if 'logits' in ActionPD.arg_constraints: # discrete
action_pd = ActionPD(logits=pdparam)
else: # continuous, args = loc and scale
if isinstance(pdparam, list): # split output
loc, scale = pdparam
else:
loc, scale = pdparam.transpose(0, 1)
# scale (stdev) must be > 0, use softplus with positive
scale = F.softplus(scale) + 1e-8
if isinstance(pdparam, list): # split output
# construct covars from a batched scale tensor
covars = torch.diag_embed(scale)
action_pd = ActionPD(loc=loc, covariance_matrix=covars)
else:
action_pd = ActionPD(loc=loc, scale=scale)
return action_pd
示例2: __init__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def __init__(self, nnodes, nfeat, nhid, nclass, gamma=1.0, beta1=5e-4, beta2=5e-4, lr=0.01, dropout=0.6, device='cpu'):
super(RGCN, self).__init__()
self.device = device
# adj_norm = normalize(adj)
# first turn original features to distribution
self.lr = lr
self.gamma = gamma
self.beta1 = beta1
self.beta2 = beta2
self.nclass = nclass
self.nhid = nhid // 2
# self.gc1 = GaussianConvolution(nfeat, nhid, dropout=dropout)
# self.gc2 = GaussianConvolution(nhid, nclass, dropout)
self.gc1 = GGCL_F(nfeat, nhid, dropout=dropout)
self.gc2 = GGCL_D(nhid, nclass, dropout=dropout)
self.dropout = dropout
# self.gaussian = MultivariateNormal(torch.zeros(self.nclass), torch.eye(self.nclass))
self.gaussian = MultivariateNormal(torch.zeros(nnodes, self.nclass),
torch.diag_embed(torch.ones(nnodes, self.nclass)))
self.adj_norm1, self.adj_norm2 = None, None
self.features, self.labels = None, None
示例3: from_log_cholesky
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def from_log_cholesky(cls,
log_diag: torch.Tensor,
off_diag: torch.Tensor,
**kwargs) -> 'Covariance':
assert log_diag.shape[:-1] == off_diag.shape[:-1]
batch_dim = log_diag.shape[:-1]
rank = log_diag.shape[-1]
L = torch.diag_embed(torch.exp(log_diag))
idx = 0
for i in range(rank):
for j in range(i):
L[..., i, j] = off_diag[..., idx]
idx += 1
out = cls(size=batch_dim + (rank, rank))
if kwargs:
out = out.to(**kwargs)
perm_shape = tuple(range(len(batch_dim))) + (-1, -2)
out[:] = L.matmul(L.permute(perm_shape))
return out
示例4: init_action_pd
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def init_action_pd(ActionPD, pdparam):
'''
Initialize the action_pd for discrete or continuous actions:
- discrete: action_pd = ActionPD(logits)
- continuous: action_pd = ActionPD(loc, scale)
'''
args = ActionPD.arg_constraints
if 'logits' in args: # discrete
# for relaxed discrete dist. with reparametrizable discrete actions
pd_kwargs = {'temperature': torch.tensor(1.0)} if hasattr(ActionPD, 'temperature') else {}
action_pd = ActionPD(logits=pdparam, **pd_kwargs)
else: # continuous, args = loc and scale
if isinstance(pdparam, list): # split output
loc, scale = pdparam
else:
loc, scale = pdparam.transpose(0, 1)
# scale (stdev) must be > 0, log-clamp-exp
scale = torch.clamp(scale, min=-20, max=2).exp()
if 'covariance_matrix' in args: # split output
# construct covars from a batched scale tensor
covars = torch.diag_embed(scale)
action_pd = ActionPD(loc=loc, covariance_matrix=covars)
else:
action_pd = ActionPD(loc=loc, scale=scale)
return action_pd
示例5: __local_curvatures
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def __local_curvatures(self, module, g_inp, g_out):
if self.derivatives.hessian_is_zero():
return []
if not self.derivatives.hessian_is_diagonal():
raise NotImplementedError
def positive_part(sign, H):
return clamp(sign * H, min=0)
def diag_embed_multi_dim(H):
"""Convert [N, C_in, H_in, ...] to [N, C_in * H_in * ...,],
embed into [N, C_in * H_in * ..., C_in * H_in = V], convert back
to [V, N, C_in, H_in, ..., V]."""
feature_shapes = H.shape[1:]
V, N = prod(feature_shapes), H.shape[0]
H_diag = diag_embed(H.view(N, V))
# [V, N, C_in, H_in, ...]
shape = (V, N, *feature_shapes)
return einsum("nic->cni", H_diag).view(shape)
def decompose_into_positive_and_negative_sqrt(H):
return [
[diag_embed_multi_dim(positive_part(sign, H).sqrt_()), sign]
for sign in [self.PLUS, self.MINUS]
]
H = self.derivatives.hessian_diagonal(module, g_inp, g_out)
return decompose_into_positive_and_negative_sqrt(H)
示例6: _sqrt_hessian
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def _sqrt_hessian(self, module, g_inp, g_out):
self._check_2nd_order_parameters(module)
probs = self._get_probs(module)
tau = torchsqrt(probs)
V_dim, C_dim = 0, 2
Id = diag_embed(ones_like(probs), dim1=V_dim, dim2=C_dim)
Id_tautau = Id - einsum("nv,nc->vnc", tau, tau)
sqrt_H = einsum("nc,vnc->vnc", tau, Id_tautau)
if module.reduction == "mean":
N = module.input0.shape[0]
sqrt_H /= sqrt(N)
return sqrt_H
示例7: get_laplacian_nuc_norm
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def get_laplacian_nuc_norm(self, A: 'N x C x S'):
N, C, _ = A.size()
# print(A)
AAT = torch.bmm(A, A.permute(0, 2, 1))
ones = torch.ones((N, C, 1), device='cuda')
D = torch.bmm(AAT, ones).view(N, C)
D = torch.diag_embed(D)
return nuclear_norm(D - AAT, sym=True).sum() / N
示例8: evaluate
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def evaluate(self, state, action):
action_mean = self.actor(state)
action_var = self.action_var.expand_as(action_mean)
cov_mat = torch.diag_embed(action_var).to(device)
dist = MultivariateNormal(action_mean, cov_mat)
action_logprobs = dist.log_prob(action)
dist_entropy = dist.entropy()
state_value = self.critic(state)
return action_logprobs, torch.squeeze(state_value), dist_entropy
示例9: evaluate_lazy_tensor
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def evaluate_lazy_tensor(self, lazy_tensor):
diag = lazy_tensor._diag_tensor._diag
tensor = lazy_tensor._lazy_tensor.tensor
return tensor + torch.diag_embed(diag, dim1=-2, dim2=-1)
示例10: evaluate
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def evaluate(self):
if self._diag.dim() == 0:
return self._diag
return torch.diag_embed(self._diag)
示例11: _eval_corr_matrix
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def _eval_corr_matrix(self):
tnc = self.task_noise_corr
fac_diag = torch.ones(*tnc.shape[:-1], self.num_tasks, device=tnc.device, dtype=tnc.dtype)
Cfac = torch.diag_embed(fac_diag)
Cfac[..., self.tidcs[0], self.tidcs[1]] = self.task_noise_corr
# squared rows must sum to one for this to be a correlation matrix
C = Cfac / Cfac.pow(2).sum(dim=-1, keepdim=True).sqrt()
return C @ C.transpose(-1, -2)
示例12: _create_marginal_input
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def _create_marginal_input(self, batch_shape=torch.Size()):
mat = torch.randn(*batch_shape, 5, 5)
eye = torch.diag_embed(torch.ones(*batch_shape, 5))
return MultivariateNormal(torch.randn(*batch_shape, 5), mat @ mat.transpose(-1, -2) + eye)
示例13: matrix
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def matrix(self):
"""Matrix form of the butterfly matrix
"""
if not self.complex:
return (torch.diag(self.diag)
+ torch.diag(self.subdiag, -self.diagonal)
+ torch.diag(self.superdiag, self.diagonal))
else: # Use torch.diag_embed (available in Pytorch 1.0) to deal with complex case.
return (torch.diag_embed(self.diag.t(), dim1=0, dim2=1)
+ torch.diag_embed(self.subdiag.t(), -self.diagonal, dim1=0, dim2=1)
+ torch.diag_embed(self.superdiag.t(), self.diagonal, dim1=0, dim2=1))
示例14: _get_test_posterior
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def _get_test_posterior(shape, device, dtype, interleaved=True, lazy=False):
mean = torch.rand(shape, device=device, dtype=dtype)
n_covar = shape[-2:].numel()
diag = torch.rand(shape, device=device, dtype=dtype)
diag = diag.view(*diag.shape[:-2], n_covar)
a = torch.rand(*shape[:-2], n_covar, n_covar, device=device, dtype=dtype)
covar = a @ a.transpose(-1, -2) + torch.diag_embed(diag)
if lazy:
covar = NonLazyTensor(covar)
if shape[-1] == 1:
mvn = MultivariateNormal(mean.squeeze(-1), covar)
else:
mvn = MultitaskMultivariateNormal(mean, covar, interleaved=interleaved)
return GPyTorchPosterior(mvn)
示例15: test_lognorm_to_norm
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import diag_embed [as 別名]
def test_lognorm_to_norm(self):
for dtype in (torch.float, torch.double):
# independent case
mu = torch.tensor([0.25, 0.5, 1.0], device=self.device, dtype=dtype)
diag = torch.tensor([0.5, 2.0, 1.0], device=self.device, dtype=dtype)
Cov = torch.diag_embed((math.exp(1) - 1) * diag)
mu_n, Cov_n = lognorm_to_norm(mu, Cov)
mu_n_expected = torch.tensor(
[-2.73179, -2.03864, -0.5], device=self.device, dtype=dtype
)
diag_expected = torch.tensor(
[2.69099, 2.69099, 1.0], device=self.device, dtype=dtype
)
self.assertTrue(torch.allclose(mu_n, mu_n_expected))
self.assertTrue(torch.allclose(Cov_n, torch.diag_embed(diag_expected)))
# correlated case
Z = torch.zeros(3, 3, device=self.device, dtype=dtype)
Z[0, 2] = math.sqrt(math.exp(1)) - 1
Z[2, 0] = math.sqrt(math.exp(1)) - 1
mu = torch.ones(3, device=self.device, dtype=dtype)
Cov = torch.diag_embed(mu * (math.exp(1) - 1)) + Z
mu_n, Cov_n = lognorm_to_norm(mu, Cov)
mu_n_expected = -0.5 * torch.ones(3, device=self.device, dtype=dtype)
Cov_n_expected = torch.tensor(
[[1.0, 0.0, 0.5], [0.0, 1.0, 0.0], [0.5, 0.0, 1.0]],
device=self.device,
dtype=dtype,
)
self.assertTrue(torch.allclose(mu_n, mu_n_expected, atol=1e-4))
self.assertTrue(torch.allclose(Cov_n, Cov_n_expected, atol=1e-4))