Q: How do I solve error: Tensor object has not atrribute 'fold' I have a method that divides an image into patches and change the colour of a specified patch. I tried merging the patches together  after the manipulation and I got the error: AttributeError: 'Tensor' object has no attribute 'fold'
def perturb_patch(img, patch_idx, patch_size, stride):
    img = img.unsqueeze(0)
    patches = img.unfold(2, patch_size, stride).unfold(3, patch_size, stride)
    patches = patches.reshape(1, 3, -1, patch_size, patch_size)
    patches = patches.squeeze(0).permute(1, 0, 2, 3)
    patches[patch_idx][0, :, :] = 0.09803922
    patches[patch_idx][1,:, :] = 0.21333333
    patches[patch_idx][2,:, :] = 0.61176471

    merged_patches = patches.fold(img.shape[-2:], kernel_size=16, stride=16, padding=0)
    return merged_patches

When I tried returning patches instead of merged_patches with  new_img = perturb_patch(img, 6, 16, 16), I could visualize the patches and the manipulated patch was noticeable. How do I merge this patches together to form the original image which of size (3, 224, 224)?

A: So, I was able to find an alternative to merge the patches together with the pytorch's view method here.
updated code:
def perturb_patch(img, patch_idx, patch_size, stride):

    img = img.unsqueeze(0)
    patches = img.unfold(2, patch_size, stride).unfold(3, patch_size, stride)
    patches = patches.reshape(1, 3, -1, patch_size, patch_size)
    patches = patches.squeeze(0).permute(1, 0, 2, 3)

    patches[patch_idx][0, :, :] = 0.09803922
    patches[patch_idx][1,:, :] = 0.21333333
    patches[patch_idx][2,:, :] = 0.61176471

    unsqueezed_patch = patches.unsqueeze(0)
    grid_size = (14, 14)
    batch_size, num_patches, c, height, width = unsqueezed_patch.size()
    image = unsqueezed_patch.view(batch_size, grid_size[0], grid_size[1], c, height, width)
    output_height = grid_size[0] * height
    output_width = grid_size[1] * width

    image = image.permute(0, 3, 1, 4, 2, 5).contiguous()
    image = image.view(batch_size, c, output_height, output_width)

    return image

