PyTorch 张量运算
PyTorch 是一种开源的机器学习框架,被广泛应用于学术研究和工业领域。它提供了丰富的工具和库,用于高效地构建和训练神经网络。
PyTorch 中的张量
在 PyTorch 中,张量是一个多维数组,可以用于表示机器学习模型的数据。维度可以是 1D、2D、3D 等,完全取决于它们所表示的数据的复杂性。
举个例子,一个 1D 张量可以用来表示一系列数值,比如时间序列数据,而一个 2D 张量可以用来表示图像,其中张量的每个元素对应图像中的一个像素。
张量运算
张量运算是对张量进行数学运算以操纵和转换其值的操作。PyTorch 提供了广泛的张量运算,可以用于对张量进行基本运算,如算术运算、统计运算和逻辑运算。
这些运算被实现为函数,接受一个或多个张量作为输入,并返回一个新的张量作为输出。
示例
在 PyTorch 中,我们通过以下示例来说明如何创建一个张量,它将接受一个值的列表或元组作为输入。
import torch
# create a 2D tensor using a nested list
a = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]])
# print the tensor
print(a)
输出
tensor([[1, 2, 3, 4],
[5, 6, 7, 8]])
加法
张量加法可以使用torch.add()函数或+运算符执行。
示例
import torch
# create a 2D tensor using a nested list
a = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]])
b = torch.add(a, 5)
c = a + b
print(b)
print(c)
输出
tensor([[6, 7, 8, 9],
[10, 11, 12, 13]])
减法
可以使用torch.sub()函数或-运算符进行张量减法运算。
示例
# subtract a scalar value of 2 from each element of the tensor to create a new tensor
d = torch.sub(a, 2)
# subtract the modified tensor from the original tensor to create a new tensor
e = a - d
print(d)
print(e)
输出
tensor([[-1, 0, 1, 2], [3, 4, 5, 6]])
tensor([[2, 2, 2, 2], [2, 2, 2, 2]])
乘法
可以使用torch.mul()函数或*运算符执行张量乘法。
示例
# multiply each element of the tensor by a scalar value of 2 to create a new tensor
f = torch.mul(a, 2)
g = a * f
print(f)
print(g)
输出
tensor([[2, 4, 6, 8],
[10, 12, 14, 16]])
tensor([[2, 8, 18, 32],
[50, 72, 98,128]])
除法
可以使用torch.div()函数或/运算符执行张量除法。
示例
# divide each element of the tensor by a scalar value of 2 to create a new tensor
h = torch.div(a, 2)
# divide the original tensor element-wise by the modified tensor to create a new tensor
i = a / h
print(h)
print(i)
输出
tensor([[0.5000, 1.0000, 1.5000, 2.0000],
[2.5000, 3.0000, 3.5000, 4.0000]])
tensor([[2., 2., 2., 2.],
[2., 2., 2., 2.]])
高级张量操作
高级张量操作包括矩阵乘法、转置、改变形状和连接,主要处理2D张量。
矩阵乘法
我们可以使用torch.mm()函数或@运算符执行矩阵乘法。
示例
# create two 2D tensors
A = torch.tensor([[1, 2], [3, 4]])
B = torch.tensor([[5, 6], [7, 8]])
# perform matrix multiplication using torch.mm() function
C = torch.mm(A, B)
# we can use the @ operator for matrix multiplication
D = A @ B
print(C)
print(D)
输出
tensor([[19, 22],
[43, 50]])
tensor([[19, 22],
[43, 50]])
转置
在张量运算中,转置是指翻转张量的轴的过程。它涉及交换2D张量的行和列,或者更一般地,交换任意维度的张量的轴。
我们可以使用torch.t()函数来执行转置操作。
示例
# create a 2D tensor
E = torch.tensor([[1, 2], [3, 4], [5, 6]])
# transpose the tensor using torch.t() function
F = torch.t(E)
print(E)
print(F)
输出
tensor([[1, 2],
[3, 4],
[5, 6]])
tensor([[1, 3, 5],
[2, 4, 6]])
重塑
在张量操作中,重塑是改变张量形状或维度的过程,同时保持其基础数据不变。它涉及重新排列张量的元素以适应新的形状,而不改变元素的总数。
我们可以使用torch.reshape()函数或.view()方法来执行重塑操作。
示例
# create a 2D tensor
G = torch.tensor([[1, 2, 3], [4, 5, 6]])
# reshape the tensor using torch.reshape() function
H = torch.reshape(G, (3, 2))
# reshape the tensor using the .view() method
I = G.view(3, 2)
输出
tensor([[1, 2, 3],
[4, 5, 6]])
tensor([[1, 2],
[3, 4],
[5, 6]])
tensor([[1, 2],
[3, 4],
[5, 6]])
连接
张量操作中的连接是将两个或多个张量沿着特定维度连接起来形成一个更大的张量的过程。结果张量具有一个新的维度,该维度是输入张量的原始维度的连接。
可以使用torch.cat()函数进行连接操作。
示例
# create two tensors
J = torch.tensor([[1, 2], [3, 4]])
K = torch.tensor([[5, 6]])
# concatenate the tensors along the first axis
L = torch.cat((J, K), dim=0)
print(J)
print(K)
print(L)
输出
tensor([[1, 2],
[3, 4]])
tensor([[5, 6]])
tensor([[1, 2],
[3, 4],
[5, 6]])
结论
PyTorch应用领域包括计算机视觉、自然语言处理、语音识别、推荐系统和自动驾驶。
Facebook人工智能研究院(FAIR)广泛使用PyTorch来进行最先进的深度学习模型的研究和开发。NVIDIA、特斯拉和IBM也在开发自动驾驶系统、各种计算机视觉应用以及自然语言处理模型时使用PyTorch。