PyTorch基本使用-张量的创建

文章目录

    • [1. 张量的创建](#1. 张量的创建)
      • [1.1 张量的基本类型](#1.1 张量的基本类型)
      • [1.2 张量的基本创建方式](#1.2 张量的基本创建方式)
      • [1.3 创建线性和随机张量](#1.3 创建线性和随机张量)
      • [1.4 创建 0-1张量](#1.4 创建 0-1张量)
      • [1.5 张量的类型转换](#1.5 张量的类型转换)

1. 张量的创建

1.1 张量的基本类型

0维张量:标量(scalar)

python 复制代码
scalar = torch.tensor(7)
print(scalar.ndim)
>>> 0

1维张量:向量(vector)

python 复制代码
vector = torch.tensor([7,7])
print(vector.ndim)
>>> 1

2维张量:矩阵(matrix)

python 复制代码
matrix = torch.tensor([
    [1,2],
    [2,3]
])
print(matrix.ndim)
>>> 2

多维张量

python 复制代码
t = torch.tensor([
    [
    [1,2,3],
    [4,5,6],
    [7,8,9]
    ]
])
print(t.ndim)
>>> 3

1.2 张量的基本创建方式

  1. torch.tensor() 根据指定数据创建张量

    python 复制代码
    # 1. 创建张量标量
    data = torch.tensor(10)
    print(data)
    # 2. numpy 数组,由于 data 为 float64, 下面代码也使用该类型
    data = np.random.rand(2,3)
    data = torch.tensor(data)
    print(data)
    # 3. 列表, 下面代码使用默认元素类型 float32
    data = [
        [10,20,30],
        [40,50,60],
        [70,80,90]
    ]
    data = torch.tensor(data)
    print(data)
  2. torch.Tensor()根据指定形状创建张量,也可以用来创建指定数据的张量

    python 复制代码
    # 1. 创建2行3列的张量, 默认 dtype 为 float32
    data = torch.Tensor(2,3)
    print(data)
    # 注意,如果传递列表,则创建包含指定元素的张量
    data = torch.Tensor([10])
    print(data)
    data = torch.Tensor([10,20,30])
    print(data)
  3. torch.IntTensor()、torch.FloatTensor()、torch.DoubleTensor() 创建指定类型的张量

    python 复制代码
    data = torch.IntTensor([10,20,30])
    print(data)
    # 如果传递的类型不对,会自动转换
    data = torch.IntTensor([10.2,20.8,30.9])
    print(data)
    # 其他类型的有
    data = torch.ShortTensor([10,20,30])
    print(data)
    data = torch.LongTensor([10,20,30])
    print(data)
    data = torch.FloatTensor([10,20,30])
    print(data)
    data = torch.DoubleTensor([10,20,30])
    print(data)

1.3 创建线性和随机张量

  • torch.arange 和 torch.linspace 创建线性张量

    python 复制代码
    # 1. 在指定区间按照步长生成元素 [start, end, step)
    data = torch.arange(0,10,2)
    print(data)
    # 2. 在指定区间按照元素个数生成 [start, end, steps]
    data = torch.linspace(0,11,10)
    print(data)

    输出结果:

    tex 复制代码
    tensor([0, 2, 4, 6, 8])
    tensor([ 0.0000,  1.2222,  2.4444,  3.6667,  4.8889,  6.1111,  7.3333,  8.5556,
             9.7778, 11.0000])
  • torch.random.init_seed 和 torch.random.manual_seed 随机种子设置,torch.randn 创建随机张量

    python 复制代码
    # 1. 创建随机张量
    data = torch.randn(2,3) # 创建2行3列张量
    print(data)
    # 查看随机种子
    print('随机种子:',torch.random.initial_seed())
    # 2. 随机种子设置
    torch.random.manual_seed(100)
    data = torch.randn(2,3)
    print(data)
    print('随机种子:',torch.random.initial_seed())

    结果输出:

    tex 复制代码
    tensor([[-0.2407,  0.0889, -0.1580],
            [-0.3603, -0.2630,  0.8047]])
    随机种子: 973960656430200
    tensor([[ 0.3607, -0.2859, -0.3938],
            [ 0.2429, -1.3833, -2.3134]])
    随机种子: 100

1.4 创建 0-1张量

  • torch.ones 和 torch.ones_like 创建全1张量

    python 复制代码
    # 1. 创建指定形状全1张量
    data = torch.ones(2,3)
    print(data)
    # 2. 根据张量形状创建全1张量
    data = torch.ones_like(data)
    print(data)

    输出结果:

    tex 复制代码
    tensor([[1., 1., 1.],
            [1., 1., 1.]])
    tensor([[1., 1., 1.],
            [1., 1., 1.]])
  • torch.zeros 和 torch.zeros_like 创建全0张量

    python 复制代码
    # 1. 创建指定形状全0张量
    data = torch.zeros(2,3)
    print(data)
    # 2. 根据张量形状创建全0张量
    data = torch.zeros_like(data)
    print(data)

    输出结果:

    tex 复制代码
    # 1. 创建指定形状全0张量
    data = torch.zeros(2,3)
    print(data)
    # 2. 根据张量形状创建全0张量
    data = torch.zeros_like(data)
    print(data)
  • torch.full 和 torch.full_like 创建全为指定值张量

    python 复制代码
    # 1. 创建指定形状指定值的张量
    data = torch.full([2,3],10)
    print(data)
    # 2. 根据张量形状创建指定值的张量
    data = torch.full_like(data,20)
    print(data)

    输出结果:

    tex 复制代码
    tensor([[10, 10, 10],
            [10, 10, 10]])
    tensor([[20, 20, 20],
            [20, 20, 20]])

1.5 张量的类型转换

  • data.type(torch.DoubleTensor)

    python 复制代码
    data = torch.full([2,3],10)
    print("张量的类型:",data.dtype)
    # 1.将类型转为 float64
    data = data.type(torch.DoubleTensor)
    print("张量的类型:",data.dtype)

    输出结果:

    tex 复制代码
    张量的类型: torch.int64
    张量的类型: torch.float64
  • data.double()

    python 复制代码
    data = torch.full([2,3],10)
    print("张量的类型:",data.dtype)
    data = data.double()
    print("张量的类型:",data.dtype)

    输出结果:

    tex 复制代码
    张量的类型: torch.int64
    张量的类型: torch.float64
相关推荐
算家计算18 分钟前
阿里重磅开源Qwen3-Next!破局AI算力瓶颈,训练成本暴降
人工智能
Billy_Zuo24 分钟前
人工智能机器学习——模型评价及优化
android·人工智能·机器学习
Web3_Daisy1 小时前
克隆代币 + 捆绑开盘:多链环境下的低成本发币玩法
人工智能·web3·区块链·比特币
sali-tec1 小时前
C# 基于halcon的视觉工作流-章32-线线测量
开发语言·人工智能·算法·计算机视觉·c#
七牛云行业应用1 小时前
图灵奖得主萨顿演讲解读:深度学习的局限与AI新范式
人工智能·深度学习
IMER SIMPLE1 小时前
人工智能-python-深度学习-神经网络VGG(详解)
人工智能·python·深度学习
martinzh1 小时前
检索器江湖:那些让RAG神功大成的武林绝学
人工智能
Juchecar2 小时前
通过“单词补全”演示 Transformer 原理(Python代码可运行)
人工智能·python
禁默2 小时前
第六届机器学习与计算机应用国际学术会议
运维·人工智能·机器学习·自动化
念念01072 小时前
基于机器学习的P2P网贷平台信用违约预测模型
人工智能·机器学习