2025-04-23 Python&深度学习3——Tensor

本文环境:

  • Pycharm 2025.1
  • Python 3.12.9
  • Pytorch 2.6.0+cu124

1 张量

1.1 数学定义

  • 张量是多维数组的泛化形式,涵盖标量(0维)、向量(1维)、矩阵(2维)及更高维结构。
  • 示例:
    • RGB 图像用三维张量表示,维度为 (高度, 宽度, 通道数),其中通道对应红、绿、蓝三色。
image-20250422113009830

1.2 PyTorch中的张量

​ PyTorch 中的张量不仅是多维数组,还是自动求导(Autograd)的核心组件。PyTorch 0.4.0 之前,Variable类型封装张量以实现自动求导;0.4.0后合并到Tensor中,简化了API。

Variable

​ Variable 是 torch.autograd 中的数据类型,主要用于封装 Tensor,进行自动求导。

  • data:被包装的 Tensor。
  • grad:data 的梯度。
  • grad_fn:创建 Tensor 的 Function,是自动求导的关键。
  • requires_grad:指示是否需要梯度。
  • is leaf:指示是否是叶子结点(张量)。
image-20250422113219061

​ PyTorch0.4.0 版开始,Variable 并入 Tensor。

  • dtype:张量的数据类型,如 torch.FloatTensortorch.cuda.FloatTensor
  • shape:张量的形状,如 (64, 3, 224, 224)。
  • device:张量所在设备,GPU/CPU,是加速的关键。
image-20250422113342736

数据类型分类

  • 浮点型:torch.float16torch.float32(常用)、torch.float64
  • 整型:torch.int8torch.uint8torch.int16torch.int32torch.int64(标签常用)。
  • 布尔型:torch.bool

2 创建 Tensor

2.1 直接创建

torch.tensor()

image-20250422114533426

​ 功能:直接创建张量。

  • data:输入数据(列表或 NumPy 数组)。
  • dtype:数据类型(默认与输入一致)。
  • device:设备(如cuda:0)。
  • requires_grad:是否需要梯度(默认False)。
  • pin_memory:是否锁页内存(通常False)。
import torch
import numpy as np

arr = np.ones((3, 3))
t = torch.tensor(arr, device='cuda:0', requires_grad=True)

t
image-20250422114441567

torch.from_numpy()

image-20250422115234275

​ 功能:从 numpy 创建 tensor。

​ 特点:与 NumPy 数组共享内存,修改一方会影响另一方。

image-20250422114702598
arr = np.array([[1, 2, 3], [4, 5, 6]])
t = torch.from_numpy(arr)

arr, t
image-20250422114920132

​ 修改 arr 中的数据后,t 中的数据也发生变化。

arr[0, 0] = -1

arr, t
image-20250422114954710

2.2 依据数值创建

torch.zeros() / torch.zeros_like()

image-20250422115801857 image-20250422120030828

​ 功能:依 size 创建全 0 张量 / 依 input 形状创建全 0 张量。

  • torch.zeros()

    • size:张量的形状,如(3,3)、(3, 224, 224)。

    • out:将输出张量赋值给哪个张量。

    • layout:内存中布局形式,有 strided,sparse_coo 等。

  • torch.zeros_like()

    • input:创建与 input 同形状的全 0 张量。
    • memory_format:张量的内存格式,默认为 torch.preserve_format
out_t = torch.tensor([1])
t = torch.zeros((3, 3), out=out_t)  # out_t会被覆盖为全零

t, out_t, id(t), id(out_t)  # out_t和t内存一致
image-20250422115440671

torch.ones() / torch.ones_like()

​ 功能:依 size 创建全 1 张量 / 依 input 形状创建全 1 张量。

​ 与 torch.zeros() / torch.zeros_like() 类似。

torch.full() / torch.full_like()

image-20250422120651090

​ 功能:依 size 创建全 fill_value 张量 / 依 input 形状创建全 fill_value 张量。

  • size:张量的形状,如 (3, 3)。
  • fill_value:张量的值。

torch.arange() / torch.linspace

image-20250423152230993 image-20250423152712595
  • torch.arange

    创建等差数列,区间为 [start, end),公差为 step

    torch.arange(2, 10, 2)
    
    image-20250423152806179
  • torch.linspace

    创建均分数列,区间为 [start, end],个数为 steps

    torch.linspace(2, 10, 9)
    
image-20250423152833851

torch.eye()

image-20250423153028584

​ 功能:创建单位对角矩阵(2维张量),默认为方阵。

  • n:每阵行数。
  • m:矩阵列数。
torch.eye(3, 5)
image-20250423153058152

2.3 依概率分布创建张量

torch.normal()

image-20250423154413627

​ 功能:生成正态分布(高斯分布)

  • mean:均值。
  • std:标准差。

依据 meanstd 的类型,可分为四种模式:

  1. mean为标量,std为标量(需指定size

    torch.normal(0, 1, size=(4, 1))
    
    image-20250423154002271
  2. mean为标量,std为张量(输出形状与std一致)

    mean = 1
    std = torch.arange(1, 5, dtype=torch.float)
    t = torch.normal(mean, std)
    
    mean, std, t
    
    image-20250423154251810
  3. mean为张量,std为标量(输出形状与mean一致)

    mean = torch.arange(1, 5, dtype=torch.float)
    std = 1
    t = torch.normal(mean, std)
    
    mean, std, t
    
    image-20250423154157605
  4. mean为张量,std为张量(每个元素从不同分布采样)

    mean = torch.arange(1, 5, dtype=torch.float)
    std = torch.arange(1, 5, dtype=torch.float)
    t = torch.normal(mean, std)
    
    mean, std, t
    
    image-20250423153711349

torch.randn() / torch.randn_like()

image-20250423154807318

​ 功能:生成标准正态分布 / 依 input 形状生成标准正态分布(均值为 0,方差为 1)。

  • size:张量的形状。
  • generator:用于采样的为随机数生成器。
torch.randn((3, 2))
image-20250423155502756

torch.rand() / torch.rand_like()

image-20250423155600645

​ 功能:在区间 [0, 1) 上,生成均匀分布。

torch.rand((3, 2))
image-20250423155534966

torch.randint() / torch.randint_like()

image-20250423155736068

​ 功能:区间 [low, high) 生成整数均匀分布。

  • low:区间左值。

  • high:区间右值。

  • size:张量的形状。

torch.randint(0, 10, (3, 2))
image-20250423155852375

torch.randperm()

image-20250423160121915

​ 功能:生成生成从 0 到 n-1 的随机排列。

  • n:张量的长度。
torch.randperm(8)
image-20250423160100807

torch.bernoulli()

image-20250423160237544

​ 功能:以 input 为概率,生成伯努力分布(0-1 分布,两点分布)

  • input:表示概率值的张量。
t, torch.bernoulli(t)
image-20250423160224724

3 张量操作

3.1 拼接

torch.cat()

image-20250423161029534

​ 功能:将张量按维度 dim 进行拼接,需要保证其他维度大小相同。

  • tensors:张量序列。
  • dim:要拼接的维度。
t = torch.ones((2, 3))

t_0 = torch.cat([t, t], dim=0)
t_1 = torch.cat([t, t], dim=1)

t_0, t_1
image-20250423161009976

torch.stack()

image-20250423161830867

​ 功能:在新创建的维度 dim 上进行堆叠,所有输入张量的形状必须一致。

  • tensors:张量序列。
  • dim:要堆叠的维度。
t1 = torch.tensor([1, 2])
t2 = torch.tensor([3, 4])
t_stack = torch.stack([t1, t2], dim=0)
t_stack, t_stack.shape
image-20250423161814422

3.2 切分

torch.chunk()

image-20250423162040909

​ 功能:将张量按维度 dim 进行平均切分。

​ 返回值:张量列表。

​ 注意事项:若不能整除,最后一份张量小于其他张量。

  • input:要切分的张量。
  • chunks:要切分的份数。
  • dim:要切分的维度。
t = torch.ones((2, 5))
list_t = torch.chunk(t, 2, dim=1)
list_t
image-20250423162102727

torch.split()

image-20250423162403459

​ 功能:将张量按维度 dim 进行切分。

​ 返回值:张量列表。

  • tensor:要切分的张量。
  • split_size_or_sections
    • 为 int 时,表示每一份的长度;
    • 为 list 时,list 中每个元素表示切割的长度,list 中元素之和与维度不等时报错。
  • dim:要切分的维度。
t = torch.ones((2, 5))
list_t = torch.split(t, 2, dim=1)
list_t
image-20250423162343432

3.3 索引

torch.index_select()

image-20250423162741366

​ 功能:在维度 dim 上,按 index 索引数据。

​ 返回值:依 index 索引数据拼接的张量。

  • input:要索引的张量。
  • dim:要索引的维度。
  • index:要索引数据的序号,类型需要为 long。
t = torch.randint(0, 9, size=(3, 3))
t_index = torch.tensor([0, 2])
t_select = torch.index_select(t, dim=1, index=t_index)

t, t_select
image-20250423162726699

torch.masked_select()

image-20250423163607734

​ 功能:按 mask 中的 True 进行索引。

​ 返回值:一维张量。

  • input:要索引的张量。
  • mask:与 input 同形状的布尔类型张量。
t = torch.randint(0, 9, size=(3, 3))
t_mask = t.ge(5)  # ge is mean greater than or equal
t_select = torch.masked_select(t, mask=t_mask)

t_mask, t_select
image-20250423163251997

3.4 变换

torch.reshape()

image-20250423163854660

​ 功能:变换张量形状。

​ 注意事项:当张量在内存中是连续时,新张量与 input 共享数据内存。

  • input:要变换的张量。
  • shape:新张量的形状。
t = torch.randperm(8)

t, t.reshape((2, -1))  # -1 表示自动计算,(2, -1) 等价于 (2, 4)
image-20250423163834368

torch.transpose()

image-20250423164713625

​ 功能:交换张量的两个维度。

  • input:要变换的张量。
  • dim0:要交换的维度。
  • dim1:要交换的维度。
t = torch.rand((2, 3, 4))
t_transpose = torch.transpose(t, dim0=1, dim1=2)

t, t_transpose
image-20250423164640828

torch.t()

image-20250423165304285

​ 功能:2 维张量转置,对矩阵而言,等价于 torch.transpose(input, 0, 1)

torch.squeeze()

image-20250423165757804

​ 功能:压缩长度为 1 的维度(轴)

  • dim
    • 若为 None,移除所有长度为 1 的轴;
    • 若指定维度,当且仅当该轴长度为 1 时,可以被移除。
t = torch.rand((1, 2, 3, 1))
t_sq = torch.squeeze(t)
t_sq0 = torch.squeeze(t, dim=0)
t_sq1 = torch.squeeze(t, dim=1)

t.shape, t_sq.shape, t_sq0.shape, t_sq1.shape
image-20250423165653890

torch.unsqueeze()

image-20250423165825448

​ 功能:依据 dim 扩展维度。

  • dim:扩展的维度。

4 线性回归案例

import torch
import matplotlib.pyplot as plt

# 生成随机数据点,x为0-10之间的随机数
x = torch.rand(20) * 10
# y基于x线性生成,并添加一些噪声
y = 2 * x + torch.randn(20) * 3

# 初始化权重w和偏置b,requires_grad=True表示它们在后续计算中需要梯度
w = torch.zeros(1, requires_grad=True)
b = torch.zeros(1, requires_grad=True)

# 定义学习率
lr = 0.01

# 迭代200次进行梯度下降
for i in range(200):
    # 计算预测值
    y_pred = w * x + b
    # 计算损失,均方误差的一半
    loss = torch.mean(0.5 * (y_pred - y) ** 2) / len(x)
    # 反向传播计算梯度
    loss.backward()
    # 更新权重w和偏置b,注意使用.no_grad()确保更新操作不被跟踪
    w.data -= lr * w.grad
    b.data -= lr * b.grad
    # 清零梯度,为下一次迭代做准备
    w.grad.zero_()
    b.grad.zero_()

    # 每20次迭代显示一次当前的数据分布和拟合直线
    if (i + 1) % 20 == 0:
        plt.rcParams['figure.figsize'] = (2.0, 1.6)
        # 绘制散点图
        plt.scatter(x.numpy(), y.numpy(), s=3)
        # 绘制当前拟合的直线
        plt.plot(x.numpy(), y_pred.detach().numpy(), 'r-', lw=0.5)
        # 显示当前的损失
        plt.text(2, 15, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 8, 'color': 'red'})
        plt.title('Iteration: {:d}'.format(i + 1))
        # 暂停0.5秒,以便观察变化
        plt.pause(0.5)

image-20250423220954285
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔗理苦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值