pytorch张量的创建

张量的创建

  • 张量(Tensors)类似于NumPy的ndarrays ,但张量可以在GPU上进行计算。从本质上来说,PyTorch是一个处理张量的库。一个张量是一个数字、向量、矩阵或任何n维数组。
python 复制代码
import torch
import numpy
torch.manual_seed(7) # 固定随机数种子

直接创建

  1. torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False)
  2. 功能:从data创建tensor
    • data: 数据,可以是list,numpy
    • dtype: 数据类型,默认与data的一致
    • device: 所在设备,cuda/cpu
    • requires_grad: 是否需要梯度
    • pin_memory: 是否存于锁页内存
python 复制代码
torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])

tensor([[0.1000, 1.2000],
,        [2.2000, 3.1000],
,        [4.9000, 5.2000]])
  1. torch.from_numpy(ndarray)
  2. 功能:从numpy创建tensor

从torch.from_numpy创建的tensor于原ndarray共享内存,当修改其中一个数据,另一个也将会被改动。

python 复制代码
a = numpy.array([1, 2, 3])
t = torch.from_numpy(a)

依据数值创建

  1. torch.zeros(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
  2. 功能:依size创建全0张量
    • size: 张量的形状
    • out: 输出的张量
    • layout: 内存中布局形式
    • device: 所在设备
    • requires_grad: 是否需要梯度
python 复制代码
torch.zeros(2, 3)

tensor([[0., 0., 0.],
,        [0., 0., 0.]])
  1. torch.zeros_like(input, dtype=None, layout=None, device=None, requires_grad=False)
  2. 功能:依input形状创建全0张量
    • input: 创建与input同形状的全0张量
    • dtype: 数据类型
    • layout: 内存中布局形式
python 复制代码
input = torch.empty(2, 3)
torch.zeros_like(input)

tensor([[0., 0., 0.],
,        [0., 0., 0.]])
python 复制代码
torch.ones(2, 3)

tensor([[1., 1., 1.],
,        [1., 1., 1.]])
  1. torch.ones_like(input, dtype=None, layout=None, device=None, requires_grad=False)
  2. 功能:依input形状创建全1张量
    • size: 张量的形状
    • dtype: 数据类型
    • layout: 内存中布局形式
    • device: 所在设备
    • requires_grad: 是否需要梯度
python 复制代码
input = torch.empty(2, 3)
torch.ones_like(input)

tensor([[1., 1., 1.],
,        [1., 1., 1.]])
  1. torch.full_like(input, dtype=None, layout=torch.strided, device=None, requires_grad=False)
  2. 功能: 依input形状创建指定数据的张量
    • size: 张量的形状
    • fill_value: 张量的值
  3. torch.arange(start=0, end. step=1, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
  4. 功能:创建等差的1维张量
    • start: 数列起始值
    • end: 数列结束值
    • step: 数列公差,默认为1
python 复制代码
torch.arange(1, 2.5, 0.5)

tensor([1.0000, 1.5000, 2.0000])

依概率分布创建张量

torch.normal(mean, std, out=None) : 生成正态分布

python 复制代码
# mean为张量, std为张量
torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1))

tensor([0.8532, 2.7075, 3.7575, 3.2200, 6.0145, 5.5526, 6.8577, 8.3697, 9.0276,
,        9.8318])

torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) : 生成标准正态分布

python 复制代码
torch.randn(2, 3)

tensor([[1.3955, 1.3470, 2.4382],
,        [0.2028, 2.4505, 2.0256]])

torch.rand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) : 在[0,1)上,生成均匀分布

python 复制代码
torch.rand(2, 3)

tensor([[0.7405, 0.2529, 0.2332],
,        [0.9314, 0.9575, 0.5575]])

张量拼接与切分

torch.cat(tensors, dim=0, out=None) : 将张量按维度进行拼接

python 复制代码
x = torch.randn(2, 3)
torch.cat((x, x, x), 1)

# 
tensor([[-1.7038,  0.6248,  0.1196, -1.7038,  0.6248,  0.1196, -1.7038,  0.6248,
,          0.1196],
,        [-0.8049,  1.6162,  0.2516, -0.8049,  1.6162,  0.2516, -0.8049,  1.6162,
,          0.2516]])

torch.stack(tensors, dim=0, out=None) :在新创建的维度上进行拼接

torch.chunk(input, chunks, dim=0) : 将张量按维度进行平均切分

相关推荐
数科云3 小时前
AI提示词(Prompt)入门:什么是Prompt?为什么要写好Prompt?
人工智能·aigc·ai写作·ai工具集·最新ai资讯
Devlive 开源社区3 小时前
技术日报|Claude Code超级能力库superpowers登顶日增1538星,自主AI循环ralph爆火登榜第二
人工智能
软件供应链安全指南4 小时前
灵脉 IAST 5.4 升级:双轮驱动 AI 漏洞治理与业务逻辑漏洞精准检测
人工智能·安全
lanmengyiyu4 小时前
单塔和双塔的区别和共同点
人工智能·双塔模型·网络结构·单塔模型
微光闪现4 小时前
AI识别宠物焦虑、紧张和晕车行为,是否已经具备实际可行性?
大数据·人工智能·宠物
技术小黑屋_4 小时前
用好Few-shot Prompting,AI 准确率提升100%
人工智能
中草药z4 小时前
【嵌入模型】概念、应用与两大 AI 开源社区(Hugging Face / 魔塔)
人工智能·算法·机器学习·数据集·向量·嵌入模型
web3.08889994 小时前
微店商品详情API实用
python·json·时序数据库
知乎的哥廷根数学学派5 小时前
基于数据驱动的自适应正交小波基优化算法(Python)
开发语言·网络·人工智能·pytorch·python·深度学习·算法
DisonTangor5 小时前
GLM-Image:面向密集知识与高保真图像生成的自回归模型
人工智能·ai作画·数据挖掘·回归·aigc