本文整理汇总了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