当前位置:网站首页>记录贴:pytorch学习Part1
记录贴:pytorch学习Part1
2022-08-08 17:33:00 【安联之夜】
记录贴:pytorch学习Part1
一、数据类型
1、标量
a = torch.tensor(1.)#生成标量
a.shape
2、张量
a = torch.randn(2,3)#生成2*3的随机矩阵
a.shape
3、浮点数
a = torch.FloatTensor(1)#生成随机浮点数
a.shape
二、创建Tensor
1、array转tensor
a = np.array([2,3.3])
torch.from_numpy(a)
2、生成随机tensor
a = torch.rand(2,3)#创建3*3的0到1均匀分布的矩阵
a = torch.randint(1,10,[2,3])#指定均匀分布的范围
a = torch.randperm(10)#不重复生成0-10的数据
a = torch.randn(2,3)#创建均值为0方差为1正态分布矩阵
a = torch.tensor([2.,3.2])#若括号内为list,则接受现有数据
a = torch.tensor(2,3)#若括号内不是list,则为shape信息
a = torch.Tensor(2,3)
a = torch.FloatTensor(2,3)
a = torch.empty(2,3)#生成0矩阵
a = torch.normal(mean=torch.full([10],0.),std=torch.arange(1,0,-0.1))#生成10个服从正态分布的随机数,其中均值为0,方差从1开始,依次递减0.1
a = torch.full([2,3],7)#生成全是7的矩阵
a = torch.arange(0,10)#生成0-9的整数
a = torch.arange(0,10,2)#生成02468
a = torch.linspace(0,10,steps=4)#将0-10分为3份
a = torch.logspace(0,-1,steps=10)#对数
a = torch.ones(2,3)#生成2*3的一矩阵
a = torch.zeros(3,3)#生成2*3的0矩阵
a = torch.eye(3,4)#生成主对角阵
三、索引与切片
#普通索引
a = torch.rand(4,3,28,28)
a[0].shape
a[:2].shape
a[:2,1:,:,:].shape
a[:2,1:,:].shape
a[:,:,0:28:2,0:28:2].shape
a.index_select(0,torch.tensor([0,2]))#索引第一个位置的前两个
a.index_select(2,torch.arange(8))
a[...].shape#...作用为省略,:
a[0,...].shape
src = torch.tensor([[4,3,5],[6,7,8]])#生成tensor
torch.take(src,torch.tensor([0,2,-1]))#
#条件索引
x = torch.randn(3,4)
mask = x.ge(0.5)#判断x里0-5的元素
torch.masked_select(x,mask)#之后根据上面的结果进行索引
四、维度变换
a = torch.rand(4,1,28,28)
a.view(4,28*28)#变换为指定形状
a.unsqueeze(0)#在a的第一个位置增加维度
a.squeeze()#删除无用维度
a = torch.rand(4,32,1,1)
a.expand(4,32,14,14)#膨胀为指定维度
a.expand(-1,32,-1,-1)#-1保持不变
a.repeat(4,32,1,1)#第一个位置复制4次,第二个位置复制32次
a.t()#装置
a.transpose(1,3)#交换1和3
torch.eq(a,a1)#判断两个矩阵是否相等
a.permute(0,2,3,1)#改变矩阵维度的位置
边栏推荐
- L2-021 点赞狂魔 (25 分)
- 开源一夏 | RuntimeException 子类
- 迁移学习(Transfer Learning)的背景、历史
- leetcode:296.最佳的碰头地点
- mysql中模糊查询的四种用法介绍
- glide4入门
- Are Huishang Futures official and reliable?Is it safe to open an account in Huishang Futures?
- 医疗机构漏诊,该不该赔?--一起交通事故多处骨折,又遇到医疗机构漏诊
- L2-016 愿天下有情人都是失散多年的兄妹 (25 分)
- Getting started with glide4
猜你喜欢
The latest research from PNAS: 81% problem solving rate, neural network Codex opens the door to the world of advanced mathematics
arxiv国内镜像——快速下载
Qt——选择文件夹并获取路径以及文件夹下子文件
LeetCode_Binary Tree_Medium_515. Find the maximum value in each tree row
Regular use in js
IP分配——DHCP(讲解+配置)
Open source summer | I have nothing to do during the epidemic, I made a button display box special effect to display my blog
开源一夏 | RuntimeException 子类
超越CLIP的多模态模型,只需不到1%的训练数据!南加大最新研究来了
【NodeJs篇】fs文件系统模块
随机推荐
Appium 自动化测试环境搭建
Node基于TCP建立的服务器
【教程2】疯壳·ARM功能手机-测试程序介绍
Getting started with glide4
L2-026 小字辈 (25 分)
爬百度图片
章节小测一
L2-019 悄悄关注 (25 分)
【FPGA教程案例46】图像案例6——基于FPGA的图像高斯滤波verilog实现,通过MATLAB进行辅助验证
Cy5反式环辛烯,TCO-Cy5,Cy5 trans-cyclooctene标记生物分子
R file not found problem
yarn : 无法加载文件 D:xxx\node_global\yarn.ps1 因为在此系统上禁止运行脚本
Tensorflow教程(四)——MNIST项目入门
Superficial understanding of ports
Cholesterol-PEG-DBCO,CLS-PEG-DBCO,胆固醇-聚乙二醇-二苯基环辛炔一种环炔烃
The difference between B+ tree and B- tree
咸阳广发证券股票开户安全吗,需要准备什么证件
数据库分析与优化
新版松鼠as换源操作
【20210923】选择感兴趣的研究方向?