本文整理汇总了Python中torch.Tensor.repeat方法的典型用法代码示例。如果您正苦于以下问题:Python Tensor.repeat方法的具体用法?Python Tensor.repeat怎么用?Python Tensor.repeat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.Tensor
的用法示例。
在下文中一共展示了Tensor.repeat方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sample_regions
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import repeat [as 别名]
def sample_regions(lb: Tensor, ub: Tensor, K: int, depth: int) -> Tuple[Tensor, Tensor]:
""" Uniformly sample K sub-regions with fixed width boundaries for each sub-region.
:param lb: Lower bounds, batched
:param ub: Upper bounds, batched
:param K: how many pieces to sample
:param depth: bisecting original region width @depth times for sampling
"""
assert valid_lb_ub(lb, ub)
assert K >= 1 and depth >= 1
repeat_dims = [1] * (len(lb.size()) - 1)
base = lb.repeat(K, *repeat_dims) # repeat K times in the batch, preserving the rest dimensions
orig_width = ub - lb
try:
piece_width = orig_width / (2 ** depth)
# print('Piece width:', piece_width)
avail_width = orig_width - piece_width
except RuntimeError as e:
print('Numerical error at depth', depth)
raise e
piece_width = piece_width.repeat(K, *repeat_dims)
avail_width = avail_width.repeat(K, *repeat_dims)
coefs = torch.rand_like(base)
lefts = base + coefs * avail_width
rights = lefts + piece_width
return lefts, rights
示例2: sample_points
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import repeat [as 别名]
def sample_points(lb: Tensor, ub: Tensor, K: int) -> Tensor:
""" Uniformly sample K points for each region.
:param lb: Lower bounds, batched
:param ub: Upper bounds, batched
:param K: how many pieces to sample
"""
assert valid_lb_ub(lb, ub)
assert K >= 1
repeat_dims = [1] * (len(lb.size()) - 1)
base = lb.repeat(K, *repeat_dims) # repeat K times in the batch, preserving the rest dimensions
width = (ub - lb).repeat(K, *repeat_dims)
coefs = torch.rand_like(base)
pts = base + coefs * width
return pts