from torch._six import container_abcs
from itertools import repeat
from typing import List
def _ntuple(n):
def parse(x):
if isinstance(x, container_abcs.Iterable):
return x
return tuple(repeat(x, n))
return parse
_single = _ntuple(1)
_pair = _ntuple(2)
_triple = _ntuple(3)
_quadruple = _ntuple(4)
这是pytorch源码,用于生成迭代器。
_single本身是一个函数,在调用的时候会将输入的int或者迭代器重复1次。