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

 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

  

pytorch基础2的更多相关文章

  1. [人工智能]Pytorch基础

    PyTorch基础 摘抄自<深度学习之Pytorch>. Tensor(张量) PyTorch里面处理的最基本的操作对象就是Tensor,表示的是一个多维矩阵,比如零维矩阵就是一个点,一维 ...

  2. 【新生学习】第一周:深度学习及pytorch基础

    DEADLINE: 2020-07-25 22:00 写在最前面: 本课程的主要思路还是要求大家大量练习 pytorch 代码,在写代码的过程中掌握深度学习的各类算法,希望大家能够坚持练习,相信经度过 ...

  3. pytorch基础学习(二)

    在神经网络训练时,还涉及到一些tricks,如网络权重的初始化方法,优化器种类(权重更新),图片预处理等,继续填坑. 1. 神经网络初始化(Network Initialization ) 1.1 初 ...

  4. PyTorch基础——词向量(Word Vector)技术

    一.介绍 内容 将接触现代 NLP 技术的基础:词向量技术. 第一个是构建一个简单的 N-Gram 语言模型,它可以根据 N 个历史词汇预测下一个单词,从而得到每一个单词的向量表示. 第二个将接触到现 ...

  5. pytorch 基础内容

    一些基础的操作: import torch as th a=th.rand(3,4) #随机数,维度为3,4的tensor b=th.rand(4)print(a)print(b) a+b tenso ...

  6. Pytorch 基础

    Pytorch 1.0.0 学习笔记: Pytorch 的学习可以参考:Welcome to PyTorch Tutorials Pytorch 是什么? 快速上手 Pytorch! Tensors( ...

  7. pytorch基础教程1

    0.迅速入门:根据上一个博客先安装好,然后终端python进入,import torch ******************************************************* ...

  8. 【pytorch】pytorch基础学习

    目录 1. 前言 # 2. Deep Learning with PyTorch: A 60 Minute Blitz 2.1 base operations 2.2 train a classifi ...

  9. Pytorch基础(6)----参数初始化

    一.使用Numpy初始化:[直接对Tensor操作] 对Sequential模型的参数进行修改: import numpy as np import torch from torch import n ...

  10. pytorch基础学习(一)

    在炼丹师的路上越走越远,开始入手pytorch框架的学习,越炼越熟吧... 1. 张量的创建和操作 创建为初始化矩阵,并初始化 a = torch.empty(, ) #创建一个5*3的未初始化矩阵 ...

随机推荐

  1. spark入门到精通(后续开始学习)

    早几年国内外研究者和业界比较关注的是在 Hadoop 平台上的并行化算法设计.然而, HadoopMapReduce 平台由于网络和磁盘读写开销大,难以高效地实现需要大量迭代计算的机器学习并行化算法. ...

  2. Eclipse代替Oracle接管Java EE

    Eclipse Foundation接替Oracle成为Java EE的新东家,Oracle不再管理Java EE. 作为采用的一部分,Java EE可能会更换新名称,Oracle建议在其建议中使用J ...

  3. 使用Pyppeteer进行gmail模拟登录

    import asyncio import time from pyppeteer import launch async def gmailLogin(username, password, url ...

  4. Petrozavodsk Summer-2016. Warsaw U Contest, XVI Open Cup Onsite.

    Petrozavodsk Summer-2016. Warsaw U Contest, XVI Open Cup Onsite. Problem A. Gambling Problem B. Colo ...

  5. VSCode 常用setiings.json设置

    { , , "editor.multiCursorModifier": "ctrlCmd", "editor.snippetSuggestions&q ...

  6. VS环境下,DEV插件的ComboBoxEdit控件最简单的数据源绑定和获取方法

    使用 ComboBoxEdit 控件绑定key/value值: 因为 ComboBoxEdit 没有 DataSource 属性,所以不能直接绑定数据源,只能一项一项的添加. 首先创建一个类ListI ...

  7. 人脸识别--Open set和Close set的区别

    训练和测试人脸识别分类器时,总会提到Open-set和Close-set.这俩词到底是什么概念呢?有什么区别呢? 所谓close-set,就是所有的测试集都在训练集中出现过.所以每次的预测直接得出测试 ...

  8. 为什么串行传输时总是LSB在前?

    https://superuser.com/questions/1104212/why-do-serial-ports-send-data-least-significant-bit-first 其实 ...

  9. CSS3——过渡

    过渡(transition)是CSS3中具有颠覆性的特征之一,我们可以在不使用 Flash 动画或 JavaScript 的情况下,当元素从一种样式变换为另一种样式时为元素添加效果. 帧动画:通过一帧 ...

  10. 总结加密、机密jar中的class

    1.加密和解密部署到jboss中间件中的的单个class文件,原理:使用“java源程序加密解决方案(基于Classloader解密) (2014-07-13 11:31)”blog即可实现: imp ...