torch.unique_consecutive

2024-05-06 22:52
文章标签 torch consecutive unique

本文主要是介绍torch.unique_consecutive,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

就是将原始的torch去除重复的部分

原理

输入参数主要三个
x(必选)
return_inverse 【bool形】(是否返回输出对应数字在原torch中的下标){可选}
return_counts【bool形】(是否返回输出对应数字的重复次数){可选}
输出:
主要有x(返回去除重复元素的torch)
inverse
counts

案例

x = torch.tensor([1, 1, 2, 2, 2, 4, 3, 3,4, 1, 1, 2])
output = torch.unique_consecutive(x)
output
## tensor([1, 2, 4, 3, 4, 1, 2])
output, inverse_indices = torch.unique_consecutive(x, return_inverse=True)
inverse_indices
## tensor([0, 0, 1, 1, 1, 2, 3, 3, 4, 5, 5, 6])
output, counts = torch.unique_consecutive(x, return_counts=True)
counts
## tensor([2, 3, 1, 2, 1, 2, 1])

这篇关于torch.unique_consecutive的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/965608

相关文章

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

pytorch torch.nn.functional.one_hot函数介绍

torch.nn.functional.one_hot 是 PyTorch 中用于生成独热编码(one-hot encoding)张量的函数。独热编码是一种常用的编码方式,特别适用于分类任务或对离散的类别标签进行处理。该函数将整数张量的每个元素转换为一个独热向量。 函数签名 torch.nn.functional.one_hot(tensor, num_classes=-1) 参数 t

torch.nn 与 torch.nn.functional的区别?

区别 PyTorch中torch.nn与torch.nn.functional的区别是:1.继承方式不同;2.可训练参数不同;3.实现方式不同;4.调用方式不同。 1.继承方式不同 torch.nn 中的模块大多数是通过继承torch.nn.Module 类来实现的,这些模块都是Python 类,需要进行实例化才能使用。而torch.nn.functional 中的函数是直接调用的,无需

torch.backends.cudnn.benchmark和torch.use_deterministic_algorithms总结学习记录

经常使用PyTorch框架的应该对于torch.backends.cudnn.benchmark和torch.use_deterministic_algorithms这两个语句并不陌生,在以往开发项目的时候可能专门化花时间去了解过,也可能只是浅尝辄止简单有关注过,正好今天再次遇到了就想着总结梳理一下。 torch.backends.cudnn.benchmark 是 PyTorch 中的一个设置

[LeetCode] 485. Max Consecutive Ones

题: 题目 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consec

[LeetCode] 128. Longest Consecutive Sequence

题:https://leetcode.com/problems/longest-consecutive-sequence/description/ 题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should

【稀疏矩阵】使用torch.sparse模块

文章目录 稀疏矩阵的格式coocsrcsc Construction of Sparse COO tensorsConstruction of CSR tensorsLinear Algebra operations(稀疏与稠密之间混合运算)Tensor methods and sparse(与稀疏有关的tensor成员函数)coo张量可用的tensor成员函数(经实测,csr也有一些可以用

LeetCode 63 Unique Paths II

题意: 给出一个带有障碍物的棋盘,每次行动向下或向右移动一格,求从左上角到右下角有几种方案。 思路: 简单dp题,假设dp[i][j]表示第i行第j列的方案数,那么状态转移方程就为 dp[i][j] = dp[i - 1][j] + dp[i][j - 1] 。 注意下边界条件就好了,而且对于障碍物,直接把dp清零即可。 可以发现这个dp只和当前行和上一行有关,进而做空间优化,用一

LeetCode 62 Unique Paths

题意: 一个n*m的棋盘,每次行动只能向下或者向右走1格,求从左上角走到右下角有几种不同的方案数。 思路: 因为行动只能向下向右,所以总步数是一定的,即n - m + 2步。那么问题就变成了这里面的哪几步是向下的,就是组合数了,即从n - m + 2个中选n - 1个的组合数。 题目里说的n和m值太夸张了,因为他的函数返回int……所以肯定很小。 代码: class S

Unique Email Address

思路1:面试的时候可以自己写process method class Solution {public int numUniqueEmails(String[] emails) {if(emails == null || emails.length == 0) {return 0;}HashSet<String> set = new HashSet<String>();for(String