python - tensor.size(-1) 中的 -1 是什么意思?
问题描述
我在 Pytorch 文档中看到过类似的内容,
import torch
a = torch.tensor([1, 2])
a.size() # torch.Size([2])
a.size(-1) # 2
这是如何运作的?我没找到说明。谢谢,
解决方案
a.size(-1)
指最后一个维度。例如,如果 x 的形状为 (10,20),则 x.size(-1) 指的是第二维,即 20。请看以下示例:
import torch
a= torch.zeros((2,5)) # a is matrix of 2 rows and 5 columns all elements are 0
#size gives a 1d tensor containing the shapes
a.size(-1)# refers to the last element in the tensor
这相当于:
a_size= a.size()
a_size(-1)
希望这对您有所帮助。
推荐阅读
- basic - 在 Apple II、AppleDOS 3.3 上使用 Applesoft 的语法错误 GET
- node.js - 什么是 MongoDB 中的“连接”?
- c - 以 ' * ' 作为参数的错误
- reactjs - ReactJS POST fetch 不起作用,错误是:TypeError: Failed to fetch
- spring - 如何从 WebSecurityConfigurerAdapter 中删除身份验证
- javascript - 如何删除由 div 的模糊背景图像造成的意外框阴影?
- javascript - 使用纯JavaScript单击元素后如何获取元素的ID?
- react-native - 无需 SDK 的 React 原生支付网关
- python - 在python中使用rawpy vs imageio读取尼康原始(NEF)图像
- python - 正则表达式:匹配项的变量数组丢弃可选文本