PyTorch张量操作


PyTorch是一个开源机器学习框架,广泛应用于学术研究和工业界。它提供了丰富的工具和库,可以有效地构建和训练神经网络。

PyTorch中的张量

在PyTorch中,张量是一个多维数值数组,可用于表示机器学习模型的数据。维度可以是1D、2D、3D等等,完全取决于它们表示的数据的复杂性。

例如,一维张量可以用来表示一系列值,例如时间序列数据,而二维张量可以用来表示图像,其中张量的每个元素对应图像中的一个像素。

张量操作

张量操作是在张量上执行的数学运算,用于操作和转换其值。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]])

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

加法

可以使用`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.]])

高级张量操作

高级张量操作包括矩阵乘法、转置、重塑和连接,这些操作主要处理二维张量。

矩阵乘法

可以使用`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]])

转置

在张量操作中,转置是翻转张量轴的过程。它涉及交换二维张量的行和列,或者更一般地,交换任何维度的张量的轴。

可以使用`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 来研究和开发最先进的深度学习模型。英伟达、特斯拉和 IBM 也使用 PyTorch 来开发自动驾驶系统、各种计算机视觉应用程序以及自然语言处理模型。

更新于:2023年8月7日

浏览量:389

启动你的职业生涯

通过完成课程获得认证

开始学习
广告