What's inverse function of tensor.unfold?

I use unfold to extract patches from a tensor, after that, I want to reconstruct the tensor from patches. How should I do?
Can I use something like

def _patch2tensor(x, h,w):
    ph,pw = x.size(2), x.size(3)
    tensor_size = x.size()
    p, c, ph, pw = tensor_size
    sh = h/ph
    sw = h/pw
    p = p/(sh*sw)
    x = x.view(p, sh, sw, c, ph, pw)
    x = x.permute(0, 3, 1, 4, 2, 5).contiguous()
    x = x.view(p, c, h, w)
    return x
1 Like