首页 技术 正文
技术 2022年11月6日
0 收藏 660 点赞 1,008 浏览 5593 个字

下面是常见函数的代码例子

 import torch
import numpy as np
print("分割线-----------------------------------------")
#加减乘除操作
a = torch.rand(,)
b = torch.rand()
print(a)
print(b)
print(torch.add(a, b))
print(torch.sub(a, b))
print(torch.mul(a, b))
print(torch.div(a, b))
print(torch.all(torch.eq(a - b,torch.sub(a,b))))#判断torch的减法和python的减法结果是否一致
print("分割线-----------------------------------------")
#矩阵乘法(点乘和叉乘)matmul mm @ *
a = torch.ones(,)*
b = torch.ones(,)
print(a*b)#点乘积
print(a.matmul(b))#叉乘积
print(a@b)#叉乘积
print(a.mm(b))#叉乘积,相比于前两种,这一种只能适合二维数组的乘积
a = torch.rand(,,,)
b = torch.rand(,,,)
#torch.mm(a,b).shape#此时会报错,mm只适合二维
print(torch.matmul(a,b).shape)#torch.Size([, , , ])
b = torch.rand(,,,)
torch.matmul(a, b).shape #torch.Size([, , , ])
b = torch.rand(,,)
#torch.matmul(a, b).shape ,报错,因为b的4对应a的3无法进行广播,所以报错
print("分割线-----------------------------------------")
#power的使用
a = torch.full([,],)
print(a.pow())
print(a**)
aa = a**
print(aa.sqrt())
print(aa**(0.5))
print(aa.rsqrt())#开根号后的倒数
print("分割线-----------------------------------------")
#floor(),ceil(),round(),trunc(),frac()的使用
a = torch.tensor(3.14)
print(a.floor(),a.ceil(),a.trunc(),a.frac())#后两个是取整,和取小数
print(a.round()) #四舍五入
print("分割线-----------------------------------------")
#clamp 和 dim,keepdim
grad = torch.rand(,)*
print(grad.max(),grad.median(), grad.min())
print(grad)
print(grad.clamp())#小于10的都变成10
print(grad.clamp(,))#不在3到10之间的变为3或者10
a = torch.randn(,)
print(a.max(dim=))#返回每一列最大值组成的数组和对应的下标
print(a.argmax(dim = ))#这个只返回最大值对应的下标
print(a.max(dim=,keepdim=True))#keepdim的作用是使返回值维度是否仍然为原来的维度不变
print(a.argmax(dim=,keepdim=True))
print("分割线-----------------------------------------")
#topk和kthvalue
print(a.topk(,dim=))#返回每行最大的三个数和下标
print(a.topk(,dim=,largest=False))#返回每行最小的三个数和下标
print(a.kthvalue(,dim=))#返回每行第五大的数字和对应数字的下标
print("分割线-----------------------------------------")
#矩阵的比较,cat的使用
m = torch.rand(,)
n = torch.rand(,)
print(m,n)
print(m == n)
print(m.eq(n))
print(m > n)
a = torch.rand(,,)
b = torch.rand(,,)
print(torch.cat([a,b],dim = ).shape)#按行进行拼接,torch.Size([, , ])
#更详细的拼接可以看下面的图
a1 = torch.rand(,,,)
a2 = torch.rand(,,,)
#torch.cat([a1,a2],dim = ).shape#报错原因是如果进行维度0上进行拼接,则要保证其他维度必须一致
a1 = torch.rand(,,,)
a2 = torch.rand(,,,)
print(torch.cat([a1,a2],dim=).shape)#torch.Size([, , , ])
print("分割线-----------------------------------------")
#stack和split的使用
#用来进行维度的扩充,这个就是在dim =2进行扩充
print(torch.stack([a1,a2],dim=).shape)#torch.Size([, , , , ])
aa , bb =a1.split([,],dim=)#拆分成两份每份数目是2,
print(aa.shape,bb.shape)
aaa,bbb = a1.split(,dim=)#每份长度为2
print(aaa.shape,bbb.shape)
aa,bb = a1.chunk(,dim = )#拆成两块,每块一个
print("分割线-----------------------------------------")
#where,gather的使用
cond = torch.rand(,)
print(cond)
a = torch.zeros(,)
b = torch.ones(,)
s = torch.where(cond>0.5,a,b)
print(s)#如果大于0.5对应位置为a的对应位置的值,否则为b的对应位置的值
prob = torch.randn(,)
idx = prob.topk(dim =,k=)
id = idx[]
print(id)#索引下标
label= torch.arange()+
d = torch.gather(label.expand(,),dim =,index = id)#获取对应索引下标的值
print(d)

运行结果如下

D:\anaconda\anaconda\pythonw.exe D:/Code/Python/龙良曲pytorch学习/高级操作.py
分割线-----------------------------------------
tensor([[0.5581, 0.2369, 0.1379, 0.3702],
[0.1565, 0.1022, 0.5839, 0.1778],
[0.0204, 0.1498, 0.5276, 0.4219]])
tensor([0.7969, 0.9313, 0.0608, 0.0245])
tensor([[1.3551, 1.1682, 0.1988, 0.3947],
[0.9535, 1.0335, 0.6448, 0.2023],
[0.8173, 1.0811, 0.5884, 0.4464]])
tensor([[-0.2388, -0.6944, 0.0771, 0.3457],
[-0.6404, -0.8291, 0.5231, 0.1533],
[-0.7766, -0.7815, 0.4667, 0.3974]])
tensor([[0.4448, 0.2206, 0.0084, 0.0091],
[0.1247, 0.0952, 0.0355, 0.0044],
[0.0162, 0.1395, 0.0321, 0.0103]])
tensor([[ 0.7003, 0.2544, 2.2669, 15.1075],
[ 0.1964, 0.1097, 9.5973, 7.2539],
[ 0.0255, 0.1609, 8.6706, 17.2148]])
tensor(True)
分割线-----------------------------------------
tensor([[3., 3.],
[3., 3.]])
tensor([[6., 6.],
[6., 6.]])
tensor([[6., 6.],
[6., 6.]])
tensor([[6., 6.],
[6., 6.]])
torch.Size([4, 3, 28, 32])
分割线-----------------------------------------
tensor([[9., 9.],
[9., 9.]])
tensor([[9., 9.],
[9., 9.]])
tensor([[3., 3.],
[3., 3.]])
tensor([[3., 3.],
[3., 3.]])
tensor([[0.3333, 0.3333],
[0.3333, 0.3333]])
分割线-----------------------------------------
tensor(3.) tensor(4.) tensor(3.) tensor(0.1400)
tensor(3.)
分割线-----------------------------------------
tensor(14.8811) tensor(8.5843) tensor(5.4463)
tensor([[10.3914, 14.8811, 8.5843],
[10.6012, 5.4463, 5.7588]])
tensor([[10.3914, 14.8811, 10.0000],
[10.6012, 10.0000, 10.0000]])
tensor([[10.0000, 10.0000, 8.5843],
[10.0000, 5.4463, 5.7588]])
torch.return_types.max(
values=tensor([1.1859, 0.7394, 1.2261, 0.5407]),
indices=tensor([5, 1, 2, 4]))
tensor([5, 1, 2, 4])
torch.return_types.max(
values=tensor([[1.1859],
[0.7394],
[1.2261],
[0.5407]]),
indices=tensor([[5],
[1],
[2],
[4]]))
tensor([[5],
[1],
[2],
[4]])
分割线-----------------------------------------
torch.return_types.topk(
values=tensor([[ 1.1859, 0.8406, 0.7883],
[ 0.7394, 0.4172, 0.2871],
[ 1.2261, 0.9851, 0.9759],
[ 0.5407, 0.1773, -0.2789]]),
indices=tensor([[5, 4, 7],
[1, 2, 4],
[2, 8, 4],
[4, 1, 8]]))
torch.return_types.topk(
values=tensor([[-1.7351, -0.3469, -0.3116],
[-1.8399, -1.1521, -0.3790],
[-1.3753, -0.6663, -0.2762],
[-1.6875, -1.5461, -0.9697]]),
indices=tensor([[0, 8, 6],
[3, 5, 0],
[7, 1, 5],
[0, 2, 3]]))
torch.return_types.kthvalue(
values=tensor([-0.1758, 0.0470, -0.2039, -0.6223]),
indices=tensor([2, 9, 3, 6]))
分割线-----------------------------------------
tensor([[0.9107, 0.4905],
[0.6499, 0.3425]]) tensor([[0.6911, 0.9619],
[0.1428, 0.5437]])
tensor([[False, False],
[False, False]])
tensor([[False, False],
[False, False]])
tensor([[ True, False],
[ True, False]])
torch.Size([9, 32, 8])
torch.Size([4, 3, 28, 32])
分割线-----------------------------------------
torch.Size([4, 3, 2, 14, 32])
torch.Size([2, 3, 14, 32]) torch.Size([2, 3, 14, 32])
torch.Size([2, 3, 14, 32]) torch.Size([2, 3, 14, 32])
分割线-----------------------------------------
tensor([[0.7541, 0.3861],
[0.9605, 0.7175]])
tensor([[0., 1.],
[0., 0.]])
tensor([[5, 4, 6],
[8, 2, 3],
[8, 6, 4],
[6, 2, 1]])
tensor([[105, 104, 106],
[108, 102, 103],
[108, 106, 104],
[106, 102, 101]])Process finished with exit code 0

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918