当前位置:网站首页>深度学习--数据操作
深度学习--数据操作
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边栏推荐
- CIO's discussion and Analysis on the definition of high-performance it team
- 性能测试计划怎么编写
- 多种智能指针
- Learn MySQL from scratch - database and data table operations
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
- Recent public ancestor offline practice (tarjan)
- IDA动态调试apk
- EasyExcel 复杂数据导出
- MQ learning notes
- # CutefishOS系统~
猜你喜欢
随机推荐
对象内存布局
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
高攀不起的希尔排序,直接插入排序
Recent public ancestor offline practice (tarjan)
【日常训练】326. 3 的幂
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
【图像分割】2021-SegFormer NeurIPS
QT uses ffmpeg4 to convert the qimage of ARGB to yuv422p
Show member variables and methods in classes in idea
Copy ‘XXXX‘ to effectively final temp variable
详解Kubernetes网络模型
Learn MySQL from scratch - database and data table operations
Four methods of JS array splicing [easy to understand]
固定资产管理子系统报表分为什么大类,包括哪些科目
How to write a performance test plan
CIO's discussion and Analysis on the definition of high-performance it team
详解LockSupport的使用
【日常训练】66. 加一
地图其他篇总目录
Slope compensation








