PyTorch 入门学习(一)————什么是PyTorch?

mac2026-08-14  9

What is PyTorch? PyTorch是一个基于python的科学计算软件包,主要用于:

代替Numpy以使用GPU的功能提供最大灵活性和速度的深度学习研究平台

Tensors

张量类似于Numpy的ndarrays,并且可以在GPU上使用加速计算

from __future__ import print_function import torch

1、矩阵的创建

构建一个未初始化的5x3矩阵

x = torch.empty(5, 3) print(x)

输出

tensor([[1.0880e-19, 1.0874e-19, 2.5443e+30], [9.4727e+21, 2.4835e+27, 2.5428e+30], [1.0877e-19, 1.5163e+23, 2.2012e+12], [3.7899e+22, 5.2480e+05, 1.0175e+31], [9.7056e+24, 1.6283e+32, 3.7913e+22]])

构造一个随机初始化的矩阵

x = torch.rand(5, 3) print(x)

输出

tensor([[0.3274, 0.3683, 0.7389], [0.1115, 0.2912, 0.5122], [0.7146, 0.9849, 0.4620], [0.2165, 0.3735, 0.0335], [0.7342, 0.2523, 0.4083]])

构造一个填充0且dtpye long(长整型) 的矩阵

x = torch.zeros(5, 3, dtype=torch.long) print(x)

输出

tensor([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])

直接从数据构造矩阵

x = torch.tensor([5.5, 3]) print(x)

输出

tensor([5.5000, 3.0000])

基于现有张量创建张量,这些方法依然使用的是输入张量的属性,例如dtype,除非用户提供新值

x = x.new_ones(5, 3, dtype=torch.double) # new_* methods take in sizes print(x) x = torch.randn_like(x, dtype=torch.float) # override dtype! print(x)

输出

tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], dtype=torch.float64) tensor([[ 1.3116, -0.4424, -0.1813], [-1.3512, -1.5031, -0.5416], [-0.4603, 0.6212, -1.0390], [ 1.3685, 0.2972, 0.4173], [ 1.8396, 1.7736, 0.2708]])

获取矩阵的大小

print(x.size())

输出

torch.Size([5, 3]) torch.Size实际上是一个元组,因此他支持元组的所有操作

2、矩阵的运算

加法(+):语法1

y = torch.rand(5, 3) print(x + y)

输出

tensor([[ 1.9627, -0.1169, 0.6470], [-0.6775, -1.0125, -0.4355], [-0.2652, 0.7214, -0.0991], [ 1.5633, 0.4468, 0.8694], [ 2.6277, 2.5232, 0.5638]])

加法(+):语法2

print(torch.add(x, y))

输出

tensor([[ 1.9627, -0.1169, 0.6470], [-0.6775, -1.0125, -0.4355], [-0.2652, 0.7214, -0.0991], [ 1.5633, 0.4468, 0.8694], [ 2.6277, 2.5232, 0.5638]])

加法(+):提供输出张量作为参数

result = torch.empty(5, 3) torch.add(x, y, out=result) print(result)

输出

tensor([[ 1.9627, -0.1169, 0.6470], [-0.6775, -1.0125, -0.4355], [-0.2652, 0.7214, -0.0991], [ 1.5633, 0.4468, 0.8694], [ 2.6277, 2.5232, 0.5638]])

加法(+):附加

# adds x to y y.add_(x) print(y)

输出

tensor([[ 1.9627, -0.1169, 0.6470], [-0.6775, -1.0125, -0.4355], [-0.2652, 0.7214, -0.0991], [ 1.5633, 0.4468, 0.8694], [ 2.6277, 2.5232, 0.5638]]) 注意:任何使张量就地变化的操作都用用于固定的‘’,例如:x.copy(y), x.t_(), 将改变x。

张量的索引

对所有的张量都可以使用类似于Numpy的索引 print(x[:, 1])

输出

tensor([-0.4424, -1.5031, 0.6212, 0.2972, 1.7736])

张量大小的调整

使用 torch.view x = torch.randn(4, 4) y = x.view(16) z = x.view(-1, 8) # the size -1 is inferred from other dimensions print(x.size(), y.size(), z.size())

输出

torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

这里的-1表示一个不确定的数,如果你不确定要reshape成几行,但是你清楚你想要几列,这时候就可以用-1

假设x的长度为16

x.view(-1, 4)等价于x.view(4, 4) # 4 * 4 = 16 x.view(-1, 2)等价于x.view(82) # 8 * 2 = 16

如果有一个元素的张量,可以使用 .item()将该值作为python的数字获取

x = torch.randn(1) print(x) print(x.item())

输出

tensor([-0.3687]) -0.3686990439891815

更多张量的操作

3、张量和Numpy之间的转换

3.1 将torch张量转换成numpy数组

a = torch.ones(5) print(a)

输出

tensor([1., 1., 1., 1., 1.]) b = a.numpy() print(b)

输出

[1. 1. 1. 1. 1.]

查看numpy数组的值如何变化

a.add_(1) print(a) print(b)

输出

tensor([2., 2., 2., 2., 2.]) [2. 2. 2. 2. 2.] 改变a同时会改变b,因为torch tensor和numpy数组共享基础内存的位子,改变其中一个将改变另一个。

3.2 将numpy数组转换为torch 张量

import numpy as np a = np.ones(5) b = torch.from_numpy(a) np.add(a, 1, out=a) print(a) print(b)

输出

[2. 2. 2. 2. 2.] tensor([2., 2., 2., 2., 2.], dtype=torch.float64)

4、CUDA 张量

使用该 .to方法可以将张量移动到任何设备上 # let us run this cell only if CUDA is available 当CUDA有用时执行以下代码 # We will use ``torch.device`` objects to move tensors in and out of GPU 可以使用torch.device对象来移动张量的输入和输出在GPU中 if torch.cuda.is_available(): device = torch.device("cuda") # a CUDA device object y = torch.ones_like(x, device=device) # directly create a tensor on GPU x = x.to(device) # or just use strings ``.to("cuda")`` z = x + y print(z) print(z.to("cpu", torch.double)) # ``.to`` can also change dtype together!

输出

tensor([0.6313], device='cuda:0') tensor([0.6313], dtype=torch.float64)

更多CUDA语义

最新回复(0)