Thanks, but I am confused about the second part.  I can’t find ‘fold’ function in pytorch. 'fold' in dir(torch) or 'fold' in dir(torch.Tensor()))  is False.  I guess you mean unfold function?
For example, I want to get the second tensor using the first one as the following figure shows. What should I do without for loop(extremely slow)?