min-max normalization of a tensor in PyTorch
I want to perform min-max normalization on a tensor in PyTorch.
The formula to obtain min-max normalization is

I want to perform min-max normalization on a tensor using some new_min and new_max without iterating through all elements of the tensor.
&gt;&gt;&gt;import torch
&gt;&gt;&gt;x = torch.randn(5, 4)
&gt;&gt;&gt;print(x)
tensor([[-0.8785, -1.6898,  2.2129, -0.8375],
        [ 1.2927, -1.3187, -0.7087, -2.1143],
        [-0.6162,  0.6836, -1.3342, -0.7889],
        [-0.2934, -1.2526, -0.3265,  1.1933],
        [ 1.2494, -1.2130,  1.5959,  1.4232]])

Is there any way to min-max normalize the given tensor between two values new_min, new_max?
Suppose I want to scale the tensor from new_min = -0.25 to new_max = 0.25

Having defined v_min, v_max, new_min, and new_max as:
&gt;&gt;&gt; v_min, v_max = v.min(), v.max()
&gt;&gt;&gt; new_min, new_max = -.25, .25

You can apply your formula element-wise:
&gt;&gt;&gt; v_p = (v - v_min)/(v_max - v_min)*(new_max - new_min) + new_min
tensor([[-0.1072, -0.2009,  0.2500, -0.1025],
        [ 0.1437, -0.1581, -0.0876, -0.2500],
        [-0.0769,  0.0733, -0.1599, -0.0969],
        [-0.0396, -0.1504, -0.0434,  0.1322],
        [ 0.1387, -0.1459,  0.1787,  0.1588]])

Then check v_p statistics:
&gt;&gt;&gt; v_p.min(), v_p.max()
(tensor(-0.2500), tensor(0.2500))

