当前位置:网站首页>Pytorch常用函数
Pytorch常用函数
2022-07-31 05:16:00 【王大队长】
本文记录Pytorch常用函数,值得一提的是本文的所有涉及函数都会给出官方文档链接,我想这可能会节约您的时间。个人应用场景为图像处理、深度学习、计算机视觉。
目录
torch.from_numpy
torch.from_numpy(ndarray) → Tensor
将一个numpy的ndarray类型转换为tensor类型(与之对应,用tensor的.numpy方法可以将tensor变成Numpy)
例子:


官方文档链接:
torch.meshgrid
torch.meshgrid(*tensors, indexing=None)
torch.meshgrid()的功能是生成网格,可以用于生成坐标。函数输入两个数据类型相同的一维张量,两个输出张量的行数为第一个输入张量的元素个数,列数为第二个输入张量的元素个数,当两个输入张量数据类型不同或维度不是一维时会报错。
其中第一个输出张量填充第一个输入张量中的元素,各行元素相同;第二个输出张量填充第二个输入张量中的元素各列元素相同。

在我看来,torch.meshgrid函数就像我之前写得加None升维的作用一样,就是为了构造x的一个元素对应y的所有元素(或者说类似构造三维输出,已知x坐标和y坐标然后一个x的坐标对应多个y)

官方文档链接:
torch.stack
torch.stack(tensors, dim=0, *, out=None) → Tensor
沿新维度连接一系列张量。 序列中所有的张量都应该为相同形状。

官方文档链接:
torch.repeat
PyTorch中的repeat()函数可以对张量进行重复扩充[1]。
当参数只有两个时:(列的重复倍数,行的重复倍数)。1表示不重复
当参数有三个时:(通道数的重复倍数,列的重复倍数,行的重复倍数)。
import torch
a= torch.arange(30).reshape(5,6)
print(a)
print('b:',a.repeat(2,2))
print('c:',a.repeat(2,1,1))/usr/bin/python3 /home/thu/test_python/repeat.py
tensor([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29]])
b: tensor([[ 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11, 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17, 12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 24, 25, 26, 27, 28, 29],
[ 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11, 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17, 12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 24, 25, 26, 27, 28, 29]])
c: tensor([[[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29]],
[[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29]]])
Process finished with exit code 0torch.permute
torch.permute(input, dims)
参数:
input:输入张量
dims:(元组类型)需要交换的维度
当然我们一般不会直接用torch.permute,而是对一个tensor调用其permute方法:
例子:改变图像的维度顺序(c*h*w)


注意:reshape并不能起到改变通道顺序的功能(可以看到结果是不对的):

官方文档链接:
待更新...
参考资料:
边栏推荐
- understand js operators
- cocoscreator3.5.2打包微信小游戏发布到QQ小游戏修改
- The server time zone value ‘й‘ is unrecognized or represents more than one time zone
- Take you to understand the MySQL isolation level, what happens when two transactions operate on the same row of data at the same time?
- 使用ps | egrep时过滤排除掉egrep自身
- SSH自动重连脚本
- Podspec verification dependency error problem pod lib lint , need to specify the source
- function in js
- podspec 校验依赖出错问题 pod lib lint ,需要指定源
- Powershell中UTF-8环境中文乱码解决办法
猜你喜欢

qt:cannot open C:\Users\XX\AppData\Local\Temp\main.obj.15576.16.jom for write

通信原理——纠错编码 | 汉明码(海明码)手算详解

朴素贝叶斯文本分类(代码实现)

Global scope and function scope in js

VS通过ODBC连接MYSQL(一)

Notes on creating a new virtual machine in Hyper-V

The server time zone value ‘й‘ is unrecognized or represents more than one time zone
![[Cloud native] Open source data analysis SPL easily copes with T+0](/img/89/4a96358956782ef9dacf0b700b54c3.png)
[Cloud native] Open source data analysis SPL easily copes with T+0

For penetration testing methods where the output point is a timestamp (take Oracle database as an example)
![[Cloud Native] What should I do if SQL (and stored procedures) run too slowly?](/img/40/be3a30743ee2bc6c82f82ab852f29c.png)
[Cloud Native] What should I do if SQL (and stored procedures) run too slowly?
随机推荐
How MySQL - depots table?A look at will understand
quick-3.5 lua调用c++
configure:error no SDL library found
Build vulhub vulnerability shooting range on kali
朴素贝叶斯文本分类(代码实现)
Android software security and reverse analysis reading notes
为什么bash中的read要配合while才能读取/dev/stdin的内容
cv2.imread()
mysql common commands
使用 OpenCV 提取图像的 HOG、SURF 及 LBP 特征 (含代码)
为数学而歌之伯努利家族
RuntimeError: CUDA error: no kernel image is available for execution on the device问题记录
Chinese garbled solution in UTF-8 environment in Powershell
Flutter mixed development module dependencies
浅谈对分布式模式下CAP的理解
活体检测CDCN学习笔记
[swagger close] The production environment closes the swagger method
VS通过ODBC连接MYSQL(二)
纯shell实现文本替换
Markdown 帮助文档