pytorch基础【3】torch运算

文章目录

torch运算

基本运算

  1. 创建张量:

    python 复制代码
    import torch
    
    # 直接从列表或数组创建张量
    x = torch.tensor([1, 2, 3])

    创建特定值的张量:

    python 复制代码
    # 全零张量
    zeros = torch.zeros(3, 3)
    
    # 全一张量
    ones = torch.ones(3, 3)
    
    # 单位张量(对角线为1,其余为0)
    eye = torch.eye(3)
    
    # 随机张量
    rand = torch.rand(3, 3)
    
    # 从均值为0,标准差为1的正态分布中抽取的随机张量
    randn = torch.randn(3, 3)

    创建等差数列张量:

    python 复制代码
    # 从0到10(不包括10)的等差数列,步长为2
    arange = torch.arange(0, 10, 2)

    创建特定间隔的数列张量:

    python 复制代码
    # 从0到10均匀分布的5个数
    linspace = torch.linspace(0, 10, 5)
  2. 加法和减法:

    python 复制代码
    # 加法
    z = x + y  # torch.add(x, y)
    print(z)  # 输出: tensor([5, 7, 9])
    
    # 减法
    z = x - y  # torch.sub(x, y)
    print(z)  # 输出: tensor([-3, -3, -3])
  3. 乘法和除法:

    python 复制代码
    # 元素乘法
    z = x * y  # torch.mul(x, y)
    print(z)  # 输出: tensor([ 4, 10, 18])
    
    # 元素除法
    z = x / y  # torch.div(x, y)
    print(z)  # 输出: tensor([0.2500, 0.4000, 0.5000])
  4. 矩阵乘法:

    python 复制代码
    a = torch.tensor([[1, 2], [3, 4]])
    b = torch.tensor([[5, 6], [7, 8]])
    
    # 矩阵乘法
    z = torch.matmul(a, b)  # 或者 a @ b
    print(z)  # 输出: tensor([[19, 22], [43, 50]])

形状操作

  1. 改变形状:view()

    python 复制代码
    a = torch.tensor([[1, 2, 3], [4, 5, 6]])
    
    # 改变形状
    b = a.view(3, 2)
    print(b)
    # 输出:
    # tensor([[1, 2],
    #         [3, 4],
    #         [5, 6]])
    自动调整size (参数-1)

    view中一个参数指定为-1,代表自动调整这个维度上的元素个数,以保证元素的总数不变。

    python 复制代码
    import torch
    x1 = torch.arange(0,16)
    print(x1)
    #a1: tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
    ------------------------------------------------------------------------------------------------------   
    x2 = x1.view(-1, 16)
    x3 = x1.view(-1, 8)
    x4 = x1.view(-1, 4)
    x5 = x1.view(-1, 2)
    
    
    print(x2)
    print(x3)
    print(x4)
    print(x5)
    
    
    x2: tensor([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15]])
    x3: tensor([[ 0,  1,  2,  3,  4,  5,  6,  7],
            [ 8,  9, 10, 11, 12, 13, 14, 15]])
    x4: tensor([[ 0,  1,  2,  3],
            [ 4,  5,  6,  7],
            [ 8,  9, 10, 11],
            [12, 13, 14, 15]])
    x5: tensor([[ 0,  1],
            [ 2,  3],
            [ 4,  5],
            [ 6,  7],
            [ 8,  9],
            [10, 11],
            [12, 13],
            [14, 15]])
  2. 拼接:

    python 复制代码
    x = torch.tensor([[1, 2], [3, 4]])
    y = torch.tensor([[5, 6], [7, 8]])
    
    # 沿着0轴拼接(垂直拼接)
    z = torch.cat((x, y), dim=0)
    print(z)
    # 输出:
    # tensor([[1, 2],
    #         [3, 4],
    #         [5, 6],
    #         [7, 8]])
    
    # 沿着1轴拼接(水平拼接)
    z = torch.cat((x, y), dim=1)
    print(z)
    # 输出:
    # tensor([[1, 2, 5, 6],
    #         [3, 4, 7, 8]])
  3. 切片:

    python 复制代码
    a = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    
    # 切片
    b = a[:, 1]  # 第二列
    print(b)  # 输出: tensor([2, 5, 8])

数学运算

  1. 求和:sum()

    python 复制代码
    x = torch.tensor([1, 2, 3, 4])
    
    # 求和
    sum_x = torch.sum(x)
    print(sum_x)  # 输出: tensor(10)
  2. 均值:mean()

    python 复制代码
    # 均值
    mean_x = torch.mean(x.float())  # 转换为浮点数类型
    print(mean_x)  # 输出: tensor(2.5000)
  3. 最大值和最小值:max(),min()

    python 复制代码
    # 最大值
    max_x = torch.max(x)
    print(max_x)  # 输出: tensor(4)
    
    # 最小值
    min_x = torch.min(x)
    print(min_x)  # 输出: tensor(1)

逻辑运算

  1. 比较运算:

    python 复制代码
    x = torch.tensor([1, 2, 3])
    y = torch.tensor([2, 2, 2])
    
    # 大于
    print(x > y)  # 输出: tensor([False, False,  True])
    
    # 小于
    print(x < y)  # 输出: tensor([ True, False, False])
  2. 逻辑与、或:

    python 复制代码
    a = torch.tensor([True, False, True])
    b = torch.tensor([False, False, True])
    
    # 逻辑与
    c = torch.logical_and(a, b)
    print(c)  # 输出: tensor([False, False,  True])
    
    # 逻辑或
    c = torch.logical_or(a, b)
    print(c)  # 输出: tensor([ True, False,  True])

常见的高级操作

  1. 广播机制:

    python 复制代码
    a = torch.tensor([1, 2, 3])
    b = torch.tensor([[1], [2], [3]])
    
    # 广播机制
    c = a + b
    print(c)
    # 输出:
    # tensor([[2, 3, 4],
    #         [3, 4, 5],
    #         [4, 5, 6]])
  2. 自动梯度计算:

    python 复制代码
    x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
    
    # 前向传播
    y = x + 2
    z = y * y * 2
    out = z.mean()
    
    # 反向传播
    out.backward()
    print(x.grad)  # 输出: tensor([4.6667, 6.0000, 7.3333])

随机数生成

  1. 从标准正态分布中生成随机张量:

    python 复制代码
    randn_tensor = torch.randn(3, 3) # 生成一个形状为 (3, 3) 的随机张量,服从标准正态分布
    print(randn_tensor)
    #tensor([[ 1.2335, -0.3941,  0.8990],
    #        [ 0.0470, -1.2671,  0.3248],
    #       [-0.4062, -0.6862,  0.1314]])
  2. 生成随机排列:

    python 复制代码
    randperm_tensor = torch.randperm(10)  # 生成一个从 0 到 9 的随机排列
    print(randperm_tensor)
    #tensor([2, 0, 5, 1, 8, 6, 3, 4, 7, 9])
  3. 生成等差数列张量:

    python 复制代码
    arange_tensor = torch.arange(0, 10, 2) # 生成从 0 到 10(不包括 10)的等差数列,步长为 2
    print(arange_tensor)
    #tensor([0, 2, 4, 6, 8])
相关推荐
__Watson__3 分钟前
【django项目使用easycython编译】Cannot convert Unicode string to ‘str‘ implicitly.
python·django
谢欣燕5 分钟前
基于轨迹信息的图像近距离可行驶区域方案验证
python·opencv·计算机视觉
45度看我6 分钟前
数据库-python SQLite3
数据库·python
X在敲AI代码9 分钟前
零基础入门 Ai 数据挖掘竞赛-速通 Baseline-1
人工智能·数据挖掘
编程刘明16 分钟前
学会拥抱Python六剑客,提高编程效率
开发语言·数据库·python·程序人生·职场和发展·学习方法
Cindy辛蒂16 分钟前
python自动化办公之matplotlib
python·自动化·matplotlib
不会编程的小孩子16 分钟前
python 基础综合应用——小开发
开发语言·python
唐天下文化17 分钟前
中俄汽车产业链合作前景广阔,东方经济论坛助力双边合作与创新
大数据·人工智能·汽车
scl、19 分钟前
AI学习与实践6_AI解场景Agent应用预研demo
人工智能·学习·agent·rag
冻感糕人~23 分钟前
转型AI产品经理前需要搞懂的9个问题
人工智能·llm·大语言模型·产品经理·ai大模型·计算机技术·大模型应用