当前位置:网站首页>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 0
torch.permute
torch.
permute
(input, dims)
参数:
input:输入张量
dims:(元组类型)需要交换的维度
当然我们一般不会直接用torch.permute,而是对一个tensor调用其permute方法:
例子:改变图像的维度顺序(c*h*w)
注意:reshape并不能起到改变通道顺序的功能(可以看到结果是不对的):
官方文档链接:
待更新...
参考资料:
边栏推荐
猜你喜欢
Understanding of objects and functions in js
朴素贝叶斯文本分类(代码实现)
使用 OpenCV 提取图像的 HOG、SURF 及 LBP 特征 (含代码)
Powershell中UTF-8环境中文乱码解决办法
动态规划(一)| 斐波那契数列和归递
npm WARN config global `--global`, `--local` are deprecated. Use `--location solution
Artifact SSMwar exploded Error deploying artifact.See server log for details
Android software security and reverse analysis reading notes
[Ubuntu20.04 installs MySQL and MySQL-workbench visualization tool]
Eternal blue bug reappears
随机推荐
禅道安装及使用教程
VTK环境配置
For penetration testing methods where the output point is a timestamp (take Oracle database as an example)
VS connects to MYSQL through ODBC (2)
unicloud 云开发记录
A simple bash to powershell case
VS connects to MYSQL through ODBC (1)
活体检测FaceBagNet阅读笔记
quick lua加密
自定dialog 布局没有居中解决方案
cocos2d-x-3.x 修改和纪录
VS2017 connects to MYSQL
The latest MySql installation teaching, very detailed
数据库 | SQL查询进阶语法
quick-3.5 无法正常显示有混合纹理的csb文件
jenkins +miniprogram-ci upload WeChat applet with one click
360 加固 file path not exists.
Take you to understand the MySQL isolation level, what happens when two transactions operate on the same row of data at the same time?
Attribute Changer的几种形态
kotlin 插件更新到1.3.21