当前位置:网站首页>记录贴: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)#改变矩阵维度的位置
边栏推荐
- 博客主页rrs代码
- The new version of squirrel as source change operation
- The difference between a uri (url urn)
- The difference between rv and sv
- 使用电脑通过VNC Viewer远程连接树莓派4B
- 开源一夏 | 疫情期间闲来无事,我自制了一个按钮展示框特效来展示我的博客
- 数据库分析与优化
- L2-013 红色警报 (25 分)(并查集)
- Basic knowledge of software engineering, software engineering
- Reprinted, the fragment speaks very well, the big guy
猜你喜欢
【DB运营管理/开发解决方案】上海道宁为您提供提高工作便利性的集成开发工具——Orange
比较器是否可以当做运放使用?
转载,fragment讲的很好的大佬
【20210923】选择感兴趣的研究方向?
4. S32K14X study notes: S32 Design Studio new and imported projects
grpc服务发现&负载均衡
Detailed explanation of JVM memory model and structure (five model diagrams)
List<String>用空串替换null值,并且都加上单引号,并且转为字符串用,分割
arxiv国内镜像——快速下载
Regular use in js
随机推荐
dp,dpi,px知识补充
Camera calibration toobox for Matlab(一)—— 工具包的基本使用
Tensorflow教程(三)——获取数据 feed 和 fetchn
leetcode:294.翻转游戏
Tensorflow教程(六)——变量基础操作
L2-022 重排链表 (25 分)(模拟链表)
Node基于TCP建立的服务器
PNAS最新研究:81%解题率,神经网络 Codex 推开高等数学世界大门
Tensorflow教程(四)——MNIST项目入门
[In-depth study of 4G/5G/6G topic-54]: L3 signaling control-3-segmentation of software functions and processes-signaling of CU-UP network elements
L2-017 人以群分 (25 分)
The difference between B+ tree and B- tree
开源一夏 | 疫情期间闲来无事,我自制了一个按钮展示框特效来展示我的博客
【20210923】Choose the research direction you are interested in?
咸阳广发证券股票开户安全吗,需要准备什么证件
Getting started with glide4
L2-027 名人堂与代金券 (25 分)
L2-012 关于堆的判断 (25 分)(堆)
无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
【教程2】疯壳·ARM功能手机-测试程序介绍