当前位置:网站首页>深度学习--数据操作
深度学习--数据操作
2022-07-01 21:47:00 【lxt1101】
torch.arange(12)产生一个从0开始到11(包括11)的一个向量,reshape函数,把向量转换为3x4的矩阵
import torch
x=torch.arange(12)
x=x.reshape(3,4)
#x=torch.zeros((2,3,4))#两个2个,三行4列的二维数组组成的三位数组
下面第一行代码等同于上述代码。sum()函数用于求和,下面的cat函数用户把两个矩阵进行合并,dim等于0时表示按行合并,dim等于1时表示按列合并
x=torch.arange(12,dtype=torch.float32).reshape(3,4)
x.sum()
y=torch.tensor([[2.0,1,4,3],[1,2,3,4],[4,3,2,1]])
torch.cat((x,y),dim=0),torch.cat((x,y),dim=1)#dim=0按行/一维合并,dim=1按列/二维进行合并,dim=2
下面是输出结果:
x=torch.arange(12,dtype=torch.float32).reshape(3,4)
x.sum()
y=torch.tensor([[2.0,1,4,3],[1,2,3,4],[4,3,2,1]])
torch.cat((x,y),dim=0),torch.cat((x,y),dim=1)#dim=0按行/一维合并,dim=1按列/二维进行合并,dim=2
按照逻辑运算符来进行判断是否相等:
#按逻辑运算符来判断相等
# x=torch.arange(0,8,dtype=torch.float32).reshape(2,2,2)
# y=torch.arange(8,16,dtype=torch.float32).reshape(2,2,2)
# torch.cat((x,y),dim=2)
y==x
下面是输出结果:
tensor([[False, True, False, True],
[False, False, False, False],
[False, False, False, False]])
矩阵即使形状不同在Python里面也可以通过广播机制按元素进行操作:
a=torch.arange(3).reshape((3,1))
b=torch.arange(2).reshape((1,2))
a,b
a+b#即使形状不同,我们仍然可以通过广播机制来执行按元素操作,会把矩阵复制为(3,2)矩阵
下面是输出结果:
a=torch.arange(3).reshape((3,1))
b=torch.arange(2).reshape((1,2))
a,b
a+b#即使形状不同,我们仍然可以通过广播机制来执行按元素操作,会把矩阵复制为(3,2)矩阵
两矩阵之间同样可以曹勇一般数据运算时间的运算法:+,-,*,**,/:
x=torch.tensor([1.0,2,4,8])
y=torch.tensor([2,2,2,2])
x+y,x-y,x*y,x**y
下面是输出结果:
x=torch.tensor([1.0,2,4,8])
y=torch.tensor([2,2,2,2])
x+y,x-y,x*y,x**y
边栏推荐
- Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
- JVM有哪些类加载机制?
- Matlab traverses images, string arrays and other basic operations
- 配置筛选机
- CSDN购买的课程从哪里可以进入
- 内存导致的电脑游戏中显示hdmi无信号 从而死机的情况
- #yyds干货盘点# 解决名企真题:扭蛋机
- GenICam GenTL 标准 ver1.5(4)第五章 采集引擎
- Redis configuration and optimization
- 分享一个一年经历两次裁员的程序员的一些感触
猜你喜欢
随机推荐
20220701
固定资产管理子系统报表分为什么大类,包括哪些科目
【日常训练】66. 加一
MySQL的存储过程
C#/VB.NET 给PDF文档添加文本/图像水印
Separate the letters and numbers in the string so that the letters come first and the array comes last
Kubernetes创建Service访问Pod
企业架构与项目管理的关联和区别
Communication between browser tab pages
RestTemplate 远程调用工具类
对象内存布局
Dark horse programmer - software testing - stage 06 2-linux and database-01-08 Chapter 1 - description of the content of the Linux operating system stage, description of the basic format and common fo
配置筛选机
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
Mysql——》MyISAM存储引擎的索引
【MySQL】索引的分类
91.(cesium篇)cesium火箭发射模拟
Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
List announced | outstanding intellectual property service team in China in 2021
LC501. 二叉搜索树中的众数