当前位置:网站首页>torch.相关函数
torch.相关函数
2022-07-26 22:37:00 【Acowardintheworld】
torch.nonzero()
Docstring:nonzero(input, *, out=None, as_tuple=False) -> LongTensor or tuple of LongTensors
涉及的参数主要是输入和 as_tuple,其实就是是否返回到一个元组,默认不是元组。
看下面的例子就懂了。
"""as_tuple=False的默认情况"""
>>> torch.nonzero(torch.tensor([1, 1, 1, 0, 1]))
tensor([[ 0],
[ 1],
[ 2],
[ 4]]) # 返回的是非0元素的索引,并且是索引的矩阵,shape= (非零元素,1)
>>> torch.nonzero(torch.tensor([[0.6, 0.0, 0.5, 0.0],
[0.0, 0.4, 0.0, 0.0],
[0.0, 0.0, 1.2, 0.0],
[0.0, 0.0, 0.0,-0.4]]))
tensor([[ 0, 0],
[ 0, 2],
[ 1, 1],
[ 2, 2],
[ 3, 3]]) #返回非零元素的在矩阵中的索引,每个元素都包含行列索引,shape= (非零元素,2)
"""as_tuple=True的默认情况"""
>>> torch.nonzero(torch.tensor(5), as_tuple=True)
(tensor([0]),) # 返回其实也是一个张量的索引,只是将它放在了一个元组里
>>> torch.nonzero(torch.tensor([1, 1, 1, 0, 1]), as_tuple=True)
(tensor([0, 1, 2, 4]),) #没区别,但是要提取列表里的某个元素,必须两个索引值,它们是一个整体,比如要提取1,[0][1]
>>> torch.nonzero(torch.tensor([[0.6, 0.0, 0.5, 0.0],
[0.0, 0.4, 0.0, 0.0],
[0.0, 0.0, 1.2, 0.0],
[0.0, 0.0, 0.0,-0.4]]), as_tuple=True)
#返回的是两个列表张量 构成 的一个元组,元组[0]是非零元素依次的行位置,元组[1]是非零元素依次的列位置,
(tensor([0, 0, 1, 2, 3]), tensor([0, 2, 1, 2, 3]))
总结:其实as_tuple=True返回元组没有什么用,如果需要非零元素的行位置或者列位置就可以设置一下,其实也就是as_tuple=False返回的第一列和第二列。
X.permute(dims)
将tensor的维度换位
https://zhuanlan.zhihu.com/p/76583143
torch.flatten(t,start_dim=开始维度,end_dim=结束维度)
将矩阵的某些维度降维处理,但是元素不丢失
t = torch.arange(120,).reshape(2,3,4,5)
t
t.shape
torch.flatten(t,start_dim=1,end_dim=-2)
torch.flatten(t,start_dim=1,end_dim=-2).shape
边栏推荐
猜你喜欢

Chapter 1 requirements analysis and SSM environment preparation

知识蒸馏——pytorch实现

Paging plug-in -- PageHelper

Go exceed API source code reading (IV) -- save (), SaveAs (name string)

画冲击函数

RecBole使用1
![[C language] classic recursion problem](/img/97/a88626e1a42f3f425396592a77100d.png)
[C language] classic recursion problem

DHCP, VLAN, NAT, large comprehensive experiment

Relationship between limit, continuity, partial derivative and total differential of multivariate function (learning notes)

Leetcode topic - binary tree chapter
随机推荐
三层架构 模拟
Embedded system migration [8] - device tree and root file system migration
CCPD data set processing (target detection and text recognition)
Mysql database complex operations: Database Constraints, query / connect table operations
Design of vision protector based on 51 single chip microcomputer
Geek challenge 2019 (review the loopholes)
Upload files to OSS file server
ResNet论文解读及代码实现(pytorch)
Paging plug-in -- PageHelper
Chapter 2 develop user traffic interceptors
第1章 开发第一个restful应用
Flink SQL (II) Kafka connector
Leetcode topic - binary tree chapter
1、 Kubernetes basic concept + environment installation (build cross server public network environment)
Skiasharp's WPF self drawn bouncing ball (case version)
Codeforces C1. Simple Polygon Embedding
深度学习调参技巧
4. Talk about the famous Zhang Zhengyou calibration method
Practice of data storage scheme in distributed system
Tree and binary tree (learning notes)