簡體   English   中英

-1在tensor.size(-1)中意味着什么?

[英]What does the -1 mean in tensor.size(-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)

希望這對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM