[英]min-max normalization of a tensor in PyTorch
I want to perform min-max normalization on a tensor in PyTorch.我想对 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.我想使用一些
new_min
和new_max
对张量执行最小-最大归一化,而不遍历张量的所有元素。
>>>import torch
>>>x = torch.randn(5, 4)
>>>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
?有什么方法可以在两个值
new_min, new_max
之间对给定的张量进行 min-max 归一化?
Suppose I want to scale the tensor from new_min = -0.25
to new_max = 0.25
假设我想将张量从
new_min = -0.25
缩放到new_max = 0.25
Having defined v_min
, v_max
, new_min
, and new_max
as:将
v_min
、 v_max
、 new_min
和new_max
为:
>>> v_min, v_max = v.min(), v.max()
>>> new_min, new_max = -.25, .25
You can apply your formula element-wise:您可以按元素应用您的公式:
>>> 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:然后检查
v_p
统计信息:
>>> v_p.min(), v_p.max()
(tensor(-0.2500), tensor(0.2500))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.