本文整理匯總了Python中torch.ones_like方法的典型用法代碼示例。如果您正苦於以下問題:Python torch.ones_like方法的具體用法?Python torch.ones_like怎麽用?Python torch.ones_like使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch
的用法示例。
在下文中一共展示了torch.ones_like方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: initialize
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def initialize(self, parameters: Tuple[Parameter, ...], *args):
stacked = stacker(parameters, lambda u: u.t_values)
self._mean = torch.zeros(stacked.concated.shape[1:], device=stacked.concated.device)
self._log_std = torch.ones_like(self._mean)
for p, msk in zip(parameters, stacked.mask):
try:
self._mean[msk] = p.bijection.inv(p.distr.mean)
except NotImplementedError:
pass
self._mean.requires_grad_(True)
self._log_std.requires_grad_(True)
return self
示例2: test_Stacker
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def test_Stacker(self):
# ===== Define a mix of parameters ====== #
zerod = Parameter(Normal(0., 1.)).sample_((1000,))
oned_luring = Parameter(Normal(torch.tensor([0.]), torch.tensor([1.]))).sample_(zerod.shape)
oned = Parameter(MultivariateNormal(torch.zeros(2), torch.eye(2))).sample_(zerod.shape)
mu = torch.zeros((3, 3))
norm = Independent(Normal(mu, torch.ones_like(mu)), 2)
twod = Parameter(norm).sample_(zerod.shape)
# ===== Stack ===== #
params = (zerod, oned, oned_luring, twod)
stacked = stacker(params, lambda u: u.t_values, dim=1)
# ===== Verify it's recreated correctly ====== #
for p, m, ps in zip(params, stacked.mask, stacked.prev_shape):
v = stacked.concated[..., m]
if len(p.c_shape) != 0:
v = v.reshape(*v.shape[:-1], *ps)
assert (p.t_values == v).all()
示例3: test_MultiDimensional
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def test_MultiDimensional(self):
mu = torch.zeros(2)
scale = torch.ones_like(mu)
shape = 1000, 100
mvn = Independent(Normal(mu, scale), 1)
mvn = AffineProcess((f, g), (1., 1.), mvn, mvn)
# ===== Initialize ===== #
x = mvn.i_sample(shape)
# ===== Propagate ===== #
num = 100
samps = [x]
for t in range(num):
samps.append(mvn.propagate(samps[-1]))
samps = torch.stack(samps)
self.assertEqual(samps.size(), torch.Size([num + 1, *shape, *mu.shape]))
# ===== Sample path ===== #
path = mvn.sample_path(num + 1, shape)
self.assertEqual(samps.shape, path.shape)
示例4: tforward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def tforward(self, disp0, im, std=None):
self.pattern = self.pattern.to(disp0.device)
self.uv0 = self.uv0.to(disp0.device)
uv0 = self.uv0.expand(disp0.shape[0], *self.uv0.shape[1:])
uv1 = torch.empty_like(uv0)
uv1[...,0] = uv0[...,0] - disp0.contiguous().view(disp0.shape[0],-1)
uv1[...,1] = uv0[...,1]
uv1[..., 0] = 2 * (uv1[..., 0] / (self.im_width-1) - 0.5)
uv1[..., 1] = 2 * (uv1[..., 1] / (self.im_height-1) - 0.5)
uv1 = uv1.view(-1, self.im_height, self.im_width, 2).clone()
pattern = self.pattern.expand(disp0.shape[0], *self.pattern.shape[1:])
pattern_proj = torch.nn.functional.grid_sample(pattern, uv1, padding_mode='border')
mask = torch.ones_like(im)
if std is not None:
mask = mask*std
diff = torchext.photometric_loss(pattern_proj.contiguous(), im.contiguous(), 9, self.loss_type, self.loss_eps)
val = (mask*diff).sum() / mask.sum()
return val, pattern_proj
示例5: test_sample_and_log_prob_with_context
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def test_sample_and_log_prob_with_context(self):
num_samples = 10
context_size = 20
input_shape = [2, 3, 4]
context_shape = [2, 3, 4]
dist = discrete.ConditionalIndependentBernoulli(input_shape)
context = torch.randn(context_size, *context_shape)
samples, log_prob = dist.sample_and_log_prob(num_samples, context=context)
self.assertIsInstance(samples, torch.Tensor)
self.assertIsInstance(log_prob, torch.Tensor)
self.assertEqual(samples.shape, torch.Size([context_size, num_samples] + input_shape))
self.assertEqual(log_prob.shape, torch.Size([context_size, num_samples]))
self.assertFalse(torch.isnan(log_prob).any())
self.assertFalse(torch.isinf(log_prob).any())
self.assert_tensor_less_equal(log_prob, 0.0)
self.assertFalse(torch.isnan(samples).any())
self.assertFalse(torch.isinf(samples).any())
binary = (samples == 1.0) | (samples == 0.0)
self.assertEqual(binary, torch.ones_like(binary))
示例6: weighted_cross_entropy_loss
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def weighted_cross_entropy_loss(preds, edges):
""" Calculate sum of weighted cross entropy loss. """
# Reference:
# hed/src/caffe/layers/sigmoid_cross_entropy_loss_layer.cpp
# https://github.com/s9xie/hed/issues/7
mask = (edges > 0.5).float()
b, c, h, w = mask.shape
num_pos = torch.sum(mask, dim=[1, 2, 3], keepdim=True).float() # Shape: [b,].
num_neg = c * h * w - num_pos # Shape: [b,].
weight = torch.zeros_like(mask)
#weight[edges > 0.5] = num_neg / (num_pos + num_neg)
#weight[edges <= 0.5] = num_pos / (num_pos + num_neg)
weight.masked_scatter_(edges > 0.5,
torch.ones_like(edges) * num_neg / (num_pos + num_neg))
weight.masked_scatter_(edges <= 0.5,
torch.ones_like(edges) * num_pos / (num_pos + num_neg))
# Calculate loss.
# preds=torch.sigmoid(preds)
losses = F.binary_cross_entropy_with_logits(
preds.float(), edges.float(), weight=weight, reduction='none')
loss = torch.sum(losses) / b
return loss
示例7: test_parallel_transport0_preserves_inner_products
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def test_parallel_transport0_preserves_inner_products(a, k):
man = lorentz.Lorentz(k=k)
a = man.projx(a)
v_0 = torch.rand_like(a) + 1e-5
u_0 = torch.rand_like(a) + 1e-5
zero = torch.ones_like(a)
d = zero.size(1) - 1
zero = torch.cat(
(zero.narrow(1, 0, 1) * torch.sqrt(k), zero.narrow(1, 1, d) * 0.0), dim=1
)
v_0 = man.proju(zero, v_0) # project on tangent plane
u_0 = man.proju(zero, u_0) # project on tangent plane
v_a = man.transp0(a, v_0)
u_a = man.transp0(a, u_0)
vu_0 = man.inner(v_0, u_0, keepdim=True)
vu_a = man.inner(v_a, u_a, keepdim=True)
np.testing.assert_allclose(vu_a, vu_0, atol=1e-5, rtol=1e-5)
示例8: test_parallel_transport0_back
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def test_parallel_transport0_back(a, b, k):
man = lorentz.Lorentz(k=k)
a = man.projx(a)
b = man.projx(b)
v_0 = torch.rand_like(a) + 1e-5
v_0 = man.proju(a, v_0) # project on tangent plane
zero = torch.ones_like(a)
d = zero.size(1) - 1
zero = torch.cat(
(zero.narrow(1, 0, 1) * torch.sqrt(k), zero.narrow(1, 1, d) * 0.0), dim=1
)
v_t = man.transp0back(a, v_0)
v_t = man.transp0(b, v_t)
v_s = man.transp(a, zero, v_0)
v_s = man.transp(zero, b, v_s)
np.testing.assert_allclose(v_t, v_s, atol=1e-5, rtol=1e-5)
示例9: test_zero_point_ops
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def test_zero_point_ops(a, k):
man = lorentz.Lorentz(k=k)
a = man.projx(a)
zero = torch.ones_like(a)
d = zero.size(1) - 1
zero = torch.cat(
(zero.narrow(1, 0, 1) * torch.sqrt(k), zero.narrow(1, 1, d) * 0.0), dim=1
)
inner_z = man.inner0(a)
inner = man.inner(None, a, zero)
np.testing.assert_allclose(inner, inner_z, atol=1e-5, rtol=1e-5)
lmap_z = man.logmap0back(a)
lmap = man.logmap(a, zero)
np.testing.assert_allclose(lmap, lmap_z, atol=1e-5, rtol=1e-5)
示例10: __getitem__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def __getitem__(self, idx):
'''
:param idx: Index of the image file
:return: returns the image and corresponding label file.
'''
image_name = self.imList[idx]
label_name = self.labelList[idx]
image = cv2.imread(image_name)
label = cv2.imread(label_name, 0)
label_bool = 255 * ((label > 200).astype(np.uint8))
if self.transform:
[image, label] = self.transform(image, label_bool)
if self.edge:
np_label = 255 * label.data.numpy().astype(np.uint8)
kernel = np.ones((self.kernel_size , self.kernel_size ), np.uint8)
erosion = cv2.erode(np_label, kernel, iterations=1)
dilation = cv2.dilate(np_label, kernel, iterations=1)
boundary = dilation - erosion
edgemap = 255 * torch.ones_like(label)
edgemap[torch.from_numpy(boundary) > 0] = label[torch.from_numpy(boundary) > 0]
return (image, label, edgemap)
else:
return (image, label)
示例11: forward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def forward(self, input, adj):
h = torch.mm(input, self.W)
N = h.size()[0]
f_1 = torch.matmul(h, self.a1)
f_2 = torch.matmul(h, self.a2)
e = self.leakyrelu(f_1 + f_2.transpose(0,1))
zero_vec = -9e15*torch.ones_like(e)
attention = torch.where(adj > 0, e, zero_vec)
attention = F.softmax(attention, dim=1)
attention = F.dropout(attention, self.dropout, training=self.training)
h_prime = torch.matmul(attention, h)
if self.concat:
return F.elu(h_prime)
else:
return h_prime
示例12: forward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def forward(self, x):
"""
Forward pass through adaptation network.
:param x: (torch.tensor) Input representation to network (task level representation z).
:return: (list::dictionaries) Dictionary for every block in layer. Dictionary contains all the parameters
necessary to adapt layer in base network. Base network is aware of dict structure and can pull params
out during forward pass.
"""
x = self.shared_layer(x)
block_params = []
for block in range(self.num_blocks):
block_param_dict = {
'gamma1': self.gamma1_processors[block](x).squeeze() * self.gamma1_regularizers[block] +
torch.ones_like(self.gamma1_regularizers[block]),
'beta1': self.beta1_processors[block](x).squeeze() * self.beta1_regularizers[block],
'gamma2': self.gamma2_processors[block](x).squeeze() * self.gamma2_regularizers[block] +
torch.ones_like(self.gamma2_regularizers[block]),
'beta2': self.beta2_processors[block](x).squeeze() * self.beta2_regularizers[block]
}
block_params.append(block_param_dict)
return block_params
示例13: forward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def forward(self, x):
if (not self.training or self.keep_prob==1): #set keep_prob=1 to turn off dropblock
return x
if self.gamma is None:
self.gamma = self.calculate_gamma(x)
if x.type() == 'torch.cuda.HalfTensor': #TODO: not fully support for FP16 now
FP16 = True
x = x.float()
else:
FP16 = False
p = torch.ones_like(x) * (self.gamma)
mask = 1 - torch.nn.functional.max_pool2d(torch.bernoulli(p),
self.kernel_size,
self.stride,
self.padding)
out = mask * x * (mask.numel()/mask.sum())
if FP16:
out = out.half()
return out
示例14: loss_discriminator
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def loss_discriminator(
self, z, batch_index, predict_true_class=True, return_details=True
):
n_classes = self.gene_dataset.n_batches
cls_logits = torch.nn.LogSoftmax(dim=1)(self.discriminator(z))
if predict_true_class:
cls_target = one_hot(batch_index, n_classes)
else:
one_hot_batch = one_hot(batch_index, n_classes)
cls_target = torch.zeros_like(one_hot_batch)
# place zeroes where true label is
cls_target.masked_scatter_(
~one_hot_batch.bool(), torch.ones_like(one_hot_batch) / (n_classes - 1)
)
l_soft = cls_logits * cls_target
loss = -l_soft.sum(dim=1).mean()
return loss
示例15: _load_from_state_dict
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import ones_like [as 別名]
def _load_from_state_dict(
self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs
):
version = local_metadata.get("version", None)
if version is None or version < 2:
# No running_mean/var in early versions
# This will silent the warnings
if prefix + "running_mean" not in state_dict:
state_dict[prefix + "running_mean"] = torch.zeros_like(self.running_mean)
if prefix + "running_var" not in state_dict:
state_dict[prefix + "running_var"] = torch.ones_like(self.running_var)
if version is not None and version < 3:
# logger = logging.getLogger(__name__)
logging.info("FrozenBatchNorm {} is upgraded to version 3.".format(prefix.rstrip(".")))
# In version < 3, running_var are used without +eps.
state_dict[prefix + "running_var"] -= self.eps
super()._load_from_state_dict(
state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs
)