Based on this 18 answer on stackoverflow:
X_max = X.clone()
X_min = X.clone()
for dim in (2, 3):
    X_max = torch.max(input=X_max, dim=dim, keepdim=True)[0]
    X_min = torch.min(input=X_min, dim=dim, keepdim=True)[0]