I haven’t timed the code yet, but it should be faster than for loops:
x1 = torch.randn(30, 500, 50, 50)
x1_min = torch.min(x, dim=3, keepdim=True)[0].min(2, keepdim=True)[0]
x1_max = torch.max(x, dim=3, keepdim=True)[0].max(2, keepdim=True)[0]

x2 = (g-f)*(x1 - x1_min) / (x1_max - x1_min) + f