如何在 PyTorch 中计算张量的直方图?
在了解如何计算张量的直方图之前,我们应该有一些基础知识。张量只是可以用来描述物理属性的数学对象,就像标量和向量一样。直方图是将一组数据点组织成用户指定范围的图形表示。
Syntax: torch.histc(input,bins,min,max)
Parameters:
- input – This is the tensor that we will be passing as an input,
- bins – The towers or bars of a histogram are called bins. The height of each bin shows how many values from that data fall into that range.
- min – This is the minimum value of the bars i.e bin
- max – This is the maximum value of the bars i.e bin
计算张量直方图的步骤
第 1 步:导入所需的库。在所有示例中,所需的Python库是 Matplotlib 和 torch。
If not installed, install them using ‘ pip install matplotlib ‘ and ‘ pip install torch ‘ .
从Python模块导入库的语法:
import torch
import matplotlib.pyplot as plt #here plt is a alias
第 2 步:创建一个具有随机值的张量并打印它。
Pytorch 张量与 NumPy 数组相同,它对深度学习或计算图或梯度一无所知,它只是一个用于数值计算的 n 维数组。
创建张量的语法:
Python3
# torch.tensor(data) creates a
# torch.Tensor object with the
# given data.
V_data = [1., 2., 3.]
V = torch.tensor(V_data)
print(V)
# Creates a tensor of matrix
M_data = [[1., 2., 3.], [4., 5., 6]]
M = torch.tensor(M_data)
print(M)
# Create a 3D tensor of size 2x2x2.
T_data = [[[1., 2.], [3., 4.]],
[[5., 6.], [7., 8.]]]
T = torch.tensor(T_data)
print(T)
Python3
# Use Google Colab to run these programs
import torch
GFG = torch.Tensor([1, 7, 1, 4, 1, 4, 3, 4, 1, 7, 2, 4])
hist = torch.histc(GFG, bins=5, min=0, max=4, *, out=None)
# Printing the histogram of tensor
print("GeeksforGeeks")
print("GFG tensor", hist)
Python3
import torch
import matplotlib.pyplot as plt
GFG = torch.Tensor([1, 7, 1, 4, 1, 4, 3, 4, 1, 7, 2, 4])
hist = torch.histc(GFG, bins=5, min=0, max=4, out=None)
# Printing the histogram of tensor
print("GeeksforGeeks")
print("GFG tensor", hist)
bins = 5
x = range(bins)
plt.bar(x, hist, align='center', color=['forestgreen'])
plt.xlabel('Bins')
plt.ylabel('Frequency')
plt.show()
Python3
# example 1
import torch
import matplotlib.pyplot as plt
# Create a tensor
T = torch.Tensor([1, 5, 1, 4, 2, 4, 3,
3, 1, 4, 2, 4])
print("Original Tensor T:\n", T)
# Calculate the histogram of the above
# created tensor
hist = torch.histc(T, bins=5, min=0, max=4)
print("Histogram of T:\n", hist)
Python3
# example 2
import torch
import matplotlib.pyplot as plt
# Create a tensor
T = torch.Tensor([1, 5, 1, 4, 2, 4, 3,
3, 1, 4, 2, 4])
print("Original Tensor T:\n", T)
# Calculate the histogram of the above
# created tensor
hist = torch.histc(T, bins=5, min=0, max=4)
# Visualize above calculated histogram
# as bar diagram
bins = 5
x = range(bins)
plt.bar(x, hist, align='center', color=['forestgreen'])
plt.xlabel('Bins')
plt.ylabel('Frequency')
plt.show()
输出 :
tensor([1., 2., 3.])
tensor([[1., 2., 3.],
[4., 5., 6.]])
tensor([[[1., 2.],
[3., 4.]],
[[5., 6.],
[7., 8.]]])
第 3 步:计算 torch.histc(input, 5, min=0, max=4),根据需要将 bin、min 和 max 设置为适当的值。
句法 :
torch.histc(input, bins=100, min=0, max=0, *, out=None) → Tensor
计算张量的直方图。元素被分类到 min 和 max 之间的等宽 bin 中。如果 min 和 max 都为零,则使用数据的最小值和最大值。低于 min 和高于 max 的元素将被忽略。
Parameters:
- input (Tensor) – the input tensor.
- bins (int) – number of histogram bins
- min (int) – the lower end of the range (inclusive)
- max (int) – upper end of the range (inclusive)
Keyword used: out (Tensor, optional) – the output tensor.
Returns: Histogram represented as a tensor
第 4 步:打印由函数torch.histc( ) 创建的直方图。
使用简单的打印函数打印直方图的张量。
Python3
# Use Google Colab to run these programs
import torch
GFG = torch.Tensor([1, 7, 1, 4, 1, 4, 3, 4, 1, 7, 2, 4])
hist = torch.histc(GFG, bins=5, min=0, max=4, *, out=None)
# Printing the histogram of tensor
print("GeeksforGeeks")
print("GFG tensor", hist)
输出 :
GeeksforGeeks
GFG tensor tensor([0., 4., 1., 1., 4.])
第 5 步:将直方图可视化为条形图
为了可视化直方图,我们将使用 matplotlib 库。
plt.bar(x/y, var_name, align=’center/left/right’, color = [‘anycolor’])
上面的代码块用于将直方图绘制为条形图。
参数:
- axis x/y:表示柱状图是沿着x轴还是y轴
- var_name:它是给张量的变量名
- 对齐:居中,左,右
- 颜色:任何颜色
Python3
import torch
import matplotlib.pyplot as plt
GFG = torch.Tensor([1, 7, 1, 4, 1, 4, 3, 4, 1, 7, 2, 4])
hist = torch.histc(GFG, bins=5, min=0, max=4, out=None)
# Printing the histogram of tensor
print("GeeksforGeeks")
print("GFG tensor", hist)
bins = 5
x = range(bins)
plt.bar(x, hist, align='center', color=['forestgreen'])
plt.xlabel('Bins')
plt.ylabel('Frequency')
plt.show()
输出:
GeeksforGeeks
GFG tensor tensor([0., 4., 1., 1., 4.])
让我们再看几个例子以便更好地理解。
示例 1:
Python3
# example 1
import torch
import matplotlib.pyplot as plt
# Create a tensor
T = torch.Tensor([1, 5, 1, 4, 2, 4, 3,
3, 1, 4, 2, 4])
print("Original Tensor T:\n", T)
# Calculate the histogram of the above
# created tensor
hist = torch.histc(T, bins=5, min=0, max=4)
print("Histogram of T:\n", hist)
输出
Original Tensor T:
tensor([1., 5., 1., 4., 2., 4., 3., 3., 1., 4., 2., 4.])
Histogram of T:
tensor([0., 3., 2., 2., 4.])
示例 2:
Python3
# example 2
import torch
import matplotlib.pyplot as plt
# Create a tensor
T = torch.Tensor([1, 5, 1, 4, 2, 4, 3,
3, 1, 4, 2, 4])
print("Original Tensor T:\n", T)
# Calculate the histogram of the above
# created tensor
hist = torch.histc(T, bins=5, min=0, max=4)
# Visualize above calculated histogram
# as bar diagram
bins = 5
x = range(bins)
plt.bar(x, hist, align='center', color=['forestgreen'])
plt.xlabel('Bins')
plt.ylabel('Frequency')
plt.show()
输出
Original Tensor T:
tensor([1., 5., 1., 4., 2., 4., 3., 3., 1., 4., 2., 4.])