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) : 将张量按维度进行平均切分

相关推荐
yyywxk40 分钟前
ICML 2026 目标检测(object detection)方向上接收论文总结
人工智能·目标检测·计算机视觉
vibecoding777 小时前
一文搞懂企业级 API 网关选型:12 个维度、4 类企业、7 步落地
人工智能·大模型·ai编程
傻啦嘿哟7 小时前
某招聘平台爬虫:爬取招聘岗位数据,分析各城市薪资水平
开发语言·爬虫·python
2501_933670797 小时前
2026秋招量化分析岗技能栈:Python、SQL、统计建模、回测项目怎么准备
开发语言·python·sql
Rocktech_ruixun7 小时前
机器人本地跑LLM大模型对主板硬件有什么要求?瑞迅科技RK3588/3568方案选型解析
人工智能·科技·嵌入式硬件·机器人·边缘计算
2601_962077607 小时前
python Dejavu库快速识别音频指纹实例探究
python·音乐识别·dejavu库·音频指纹识别·实例探究
yxlalm7 小时前
Spring AI+RAG 01-项目背景与技术选型
java·人工智能·spring
tedcloud1237 小时前
Wand-Enhancer 怎么搭建?开源 Wand 客户端增强与远程控制工具介绍
大数据·服务器·人工智能·开源·音视频
科技苑7 小时前
如何用Python编程实现一个简单的Web爬虫?
人工智能·python
迅利科技7 小时前
新能源汽车零部件研发,SIMULIA一站式仿真解决方案如何缩短研发周期
人工智能·汽车