《动手学深度学习 PyTorch版》学习笔记(一):数据操作

    科技2026-06-17  12

    一、数据操作

    在PyTorch中,torch.Tensor是存储和变换数据的主要工具。

    "tensor"这个单词一般可译作“张量”,张量可以看作是一个多维数组。 标量可以看作是0维张量,向量可以看作1维张量,矩阵可以看作是二维张量。

    1、torch.arange() 和torch.linspace

    # arange(s, e, step) => 从s到e,步长为step x8 = torch.arange(1, 10, 2) # print(x8) # linspace(s, e, steps) => 从s到e,均匀切分成steps份 x9 = torch.linspace(2,8,3) # print(x9)

    2、torch.range() 和torch.arange() 的区别

    >>> y=torch.range(1,6) >>> y tensor([1., 2., 3., 4., 5., 6.]) >>> y.dtype torch.float32 >>> z=torch.arange(1,6) >>> z tensor([1, 2, 3, 4, 5]) >>> z.dtype torch.int64

    3、torch.randn与torch.rand的区别

    randn

    torch.randn(*sizes, out=None) → Tensor

    返回一个包含了从标准正态分布中抽取的一组随机数的张量

    size:张量的形状

    out:结果张量

    rand

    torch.rand(*sizes, out=None) → Tensor

    返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数

    4、NumPy数组与Tensor的互相转换(共享内存)

    NumPy转Tensor:torch.from_numpy() Tensor转NumPy:numpy() 另:可以使用 torch.tensor() 将NumPy数组转换成Tensor,但不再共享内存

    5、Tensor on GPU

    if torch.cuda.is_available(): device = torch.device("cuda") # GPU y = torch.ones_like(x, device=device) # 直接创建一个在GPU上的Tensor x = x.to(device) # 等价于 .to("cuda") z = x + y print(z) print(z.to("cpu", torch.double)) # to()还可以同时更改数据类型

    6、索引

    索引出来的结果与元数据共享内存

    x = torch.tensor([[1, 2, 3], [4, 5, 6]]) print(x) y = x[0, :] # 取出第一行 print(y) y += 1 print(y) print(x[0, :]) # 源tensor也被改了

    输出

    tensor([[1, 2, 3], [4, 5, 6]]) tensor([1, 2, 3]) tensor([2, 3, 4]) tensor([2, 3, 4])

    7、广播机制

    当对两个形状不同的Tensor按元素运算时,可能会触发广播(broadcasting)机制:先适当复制元素使这两个Tensor形状相同后再按元素运算

    x = torch.arange(1, 3).view(1, 2) print(x) y = torch.arange(1, 4).view(3, 1) print(y) print(x + y)

    输出

    tensor([[1, 2]]) tensor([[1], [2], [3]]) tensor([[2, 3], [3, 4], [4, 5]])
    Processed: 0.015, SQL: 9