pytorch张量更改维度为相邻值
我的目标是计算张量 x
的所有相邻唯一值。
假设我的张量是( x
看起来像一个列表,但它是pytorch张量)
x = [1,2,1,2,4,5]
我希望我的输出是:
[1,2] = 2
[2,1] = 1
[2,4] = 1
[4,5] = 1
我考虑更改张量的尺寸看起来像:
x = [[1,2],[2,1],[1,2],[2,4],[4,5]]
使用 tensor .View
,但找不到适用于任何长度张量的解决方案。
有什么想法是否是最好的方法?有内置功能吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AS @ihdv 显示,您可以使用
x
torch.stack.stack
pytorch.org/docs/stable/generated/torch.vstack.html“ rel =” nofollow noreferrer“>torch.vstack.vstack
,以获取带有重叠窗口的成对张量。然后,您可以应用在其上获取统计信息:
As @ihdv showed, you can stack shifted views of
x
withtorch.stack
ortorch.vstack
in order to get a tensor of pairs with overlapping windows.Then you can apply
torch.unique
on it to get the statistics: