Pytorch遇到报错Expected more than 1 value per channel when training, got input size torch.Size

Pytorch遇到
Expected more than 1 value per channel when training, got input size torch.Size([1, 256, 1, 1])

楼主遇到这个报错,通过pycharm定位到断言的位置

def _verify_batch_size(size: List[int]) -> None:
    # XXX: JIT script does not support the reduce from functools, and mul op is a
    # builtin, which cannot be used as a value to a func yet, so rewrite this size
    # check to a simple equivalent for loop
    #
    # TODO: make use of reduce like below when JIT is ready with the missing features:
    # from operator import mul
    # from functools import reduce
    #
    #   if reduce(mul, size[2:], size[0]) == 1
    size_prods = size[0]
    for i in range(len(size) - 2):
        size_prods *= size[i + 2]
    if size_prods == 1:
        raise ValueError("Expected more than 1 value per channel when training, got input size {}".format(size))

看上去是因为 BN层里,遇到了batch=1的情况

果然是因为batch=1引起的

解决方案

设置 batch_size>1, 且 drop_last=True

 DataLoader(train_set, batch_size=args.train_batch_size,
           num_workers=args.num_workers, shuffle=(train_sampler is None), 
           drop_last=True, sampler = train_sampler)