本文整理匯總了Python中torch.Generator方法的典型用法代碼示例。如果您正苦於以下問題:Python torch.Generator方法的具體用法?Python torch.Generator怎麽用?Python torch.Generator使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch
的用法示例。
在下文中一共展示了torch.Generator方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __iter__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Generator [as 別名]
def __iter__(self):
# deterministically shuffle based on epoch
if self.shuffle:
g = torch.Generator()
g.manual_seed(self.epoch)
indices = torch.randperm(len(self.dataset), generator=g).tolist()
else:
indices = torch.arange(len(self.dataset)).tolist()
# add extra samples to make it evenly divisible
indices += indices[:(self.total_size - len(indices))]
assert len(indices) == self.total_size
# subsample
indices = indices[self.rank:self.total_size:self.num_replicas]
assert len(indices) == self.num_samples
return iter(indices)
示例2: __iter__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Generator [as 別名]
def __iter__(self):
if self.shuffle:
# deterministically shuffle based on epoch
g = torch.Generator()
g.manual_seed(self.epoch)
indices = torch.randperm(len(self.dataset), generator=g).tolist()
else:
indices = torch.arange(len(self.dataset)).tolist()
# add extra samples to make it evenly divisible
indices += indices[: (self.total_size - len(indices))]
assert len(indices) == self.total_size
# subsample
offset = self.num_samples * self.rank
indices = indices[offset : offset + self.num_samples]
assert len(indices) == self.num_samples
return iter(indices)
示例3: _get_epoch_indices
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Generator [as 別名]
def _get_epoch_indices(self, generator):
"""
Create a list of dataset indices (with repeats) to use for one epoch.
Args:
generator (torch.Generator): pseudo random number generator used for
stochastic rounding.
Returns:
torch.Tensor: list of dataset indices to use in one epoch. Each index
is repeated based on its calculated repeat factor.
"""
# Since repeat factors are fractional, we use stochastic rounding so
# that the target repeat factor is achieved in expectation over the
# course of training
rands = torch.rand(len(self._frac_part), generator=generator)
rep_factors = self._int_part + (rands < self._frac_part).float()
# Construct a list of indices in which we repeat images as specified
indices = []
for dataset_index, rep_factor in enumerate(rep_factors):
indices.extend([dataset_index] * int(rep_factor.item()))
return torch.tensor(indices, dtype=torch.int64)
示例4: __iter__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Generator [as 別名]
def __iter__(self):
if self.shuffle:
# deterministically shuffle based on epoch
g = torch.Generator()
g.manual_seed(self.epoch)
indices = self._get_epoch_indices(g)
randperm = torch.randperm(len(indices), generator=g).tolist()
indices = indices[randperm]
else:
g = torch.Generator()
g.manual_seed(self.epoch)
indices = self._get_epoch_indices(g)
# indices = torch.arange(len(self.dataset)).tolist()
# when balance len(indices) diff from dataset image_num
self.total_size = len(indices)
logging_rank('balance sample total_size: {}'.format(self.total_size), distributed=1, local_rank=self.rank)
# subsample
self.num_samples = int(len(indices) / self.num_replicas)
offset = self.num_samples * self.rank
indices = indices[offset: offset + self.num_samples]
assert len(indices) == self.num_samples
return iter(indices)
示例5: __iter__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Generator [as 別名]
def __iter__(self):
batches = self._generate_batches()
g = torch.Generator()
g.manual_seed(self._epoch)
indices = list(torch.randperm(len(batches), generator=g))
# add extra samples to make it evenly divisible
indices += indices[:(self.num_batches * self.num_replicas - len(indices))]
assert len(indices) == self.num_batches * self.num_replicas
# subsample
offset = self.num_batches * self.rank
indices = indices[offset:offset + self.num_batches]
assert len(indices) == self.num_batches
for idx in indices:
batch = sorted(batches[idx], key=lambda i: i["ar"])
batch = [i["id"] for i in batch]
yield batch
示例6: __iter__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Generator [as 別名]
def __iter__(self):
# deterministically shuffle based on epoch
g = torch.Generator()
g.manual_seed(self.epoch)
indices = list(torch.randperm(len(self.dataset), generator=g))
# add extra samples to make it evenly divisible
indices += indices[:(self.total_size - len(indices))]
assert len(indices) == self.total_size
# subsample
offset = self.num_samples * self.rank
indices = indices[offset:offset + self.num_samples]
assert len(indices) == self.num_samples
return iter(indices)
示例7: __iter__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Generator [as 別名]
def __iter__(self):
if self.shuffle:
# deterministically shuffle based on epoch
g = torch.Generator()
g.manual_seed(self.epoch)
indices = torch.randperm(len(self.dataset), generator=g).tolist()
else:
indices = torch.arange(len(self.dataset)).tolist()
# add extra samples to make it evenly divisible
indices += indices[: (self.total_size - len(indices))]
assert len(indices) == self.total_size
# subsample
offset = self.num_samples * self.rank
indices = indices[offset: offset + self.num_samples]
assert len(indices) == self.num_samples
return iter(indices)
示例8: generate_indices
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Generator [as 別名]
def generate_indices(self):
# deterministically shuffle based on epoch
g = torch.Generator()
g.manual_seed(self.epoch)
if self.shuffle:
indices = torch.randperm(len(self.dataset), generator=g).tolist()
else: # pragma: no cover
indices = list(range(len(self.dataset)))
# add extra samples to make it evenly divisible
indices += indices[:(self.total_size - len(indices))]
assert len(indices) == self.total_size
# subsample
indices = indices[self.rank:self.total_size:self.num_replicas]
assert len(indices) == self.num_samples
self.dataset.prefetch(indices)
return indices
示例9: __iter__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Generator [as 別名]
def __iter__(self):
# deterministically shuffle based on epoch
g = torch.Generator()
g.manual_seed(self.epoch)
indices = torch.randperm(len(self.dataset), generator=g).tolist()
# add extra samples to make it evenly divisible
if self.pad:
indices += indices[:(self.total_size - len(indices))]
assert len(indices) == self.total_size
# subsample
indices = indices[self.rank:self.total_size:self.num_replicas]
assert len(indices) == self.num_samples
return iter(indices)
示例10: pick_mixture_component
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Generator [as 別名]
def pick_mixture_component(w, seed=None):
'''Randomly choose mixture component indices with probability given by
the component weights w. Works on batches of component weights.
w: Weights of the mixture components, must be positive and sum to one
seed: Optional RNG seed for consistent decisions'''
w_thresholds = torch.cumsum(w, dim=1)
# Prepare local random number generator
rng = torch.Generator(device=w.device)
if isinstance(seed, int):
rng = rng.manual_seed(seed)
else:
rng.seed()
# Draw one uniform random number per batch row and compare against thresholds
u = torch.rand(w.shape[0], 1, device=w.device, generator=rng)
indices = torch.sum(u > w_thresholds, dim=1).int()
# Return mixture component indices
return indices
示例11: _add_noise
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Generator [as 別名]
def _add_noise(self, lrs, t):
if self.noise_range_t is not None:
if isinstance(self.noise_range_t, (list, tuple)):
apply_noise = self.noise_range_t[0] <= t < self.noise_range_t[1]
else:
apply_noise = t >= self.noise_range_t
if apply_noise:
g = torch.Generator()
g.manual_seed(self.noise_seed + t)
if self.noise_type == 'normal':
while True:
# resample if noise out of percent limit, brute force but shouldn't spin much
noise = torch.randn(1, generator=g).item()
if abs(noise) < self.noise_pct:
break
else:
noise = 2 * (torch.rand(1, generator=g).item() - 0.5) * self.noise_pct
lrs = [v + v * noise for v in lrs]
return lrs
示例12: _apply_noise
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Generator [as 別名]
def _apply_noise(self, epoch):
g = torch.Generator()
g.manual_seed(self.noise_seed + epoch)
if self.noise_type == 'normal':
while True:
# resample if noise out of percent limit, brute force but shouldn't spin much
noise = torch.randn(1, generator=g).item()
if abs(noise) < self.noise_pct:
break
else:
noise = 2 * (torch.rand(1, generator=g).item() - 0.5) * self.noise_pct
# apply the noise on top of previous LR, cache the old value so we can restore for normal
# stepping of base scheduler
restore_lr = []
for i, param_group in enumerate(self.optimizer.param_groups):
old_lr = float(param_group['lr'])
restore_lr.append(old_lr)
new_lr = old_lr + old_lr * noise
param_group['lr'] = new_lr
self.restore_lr = restore_lr