當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python PyTorch is_tensor()用法及代碼示例

PyTorch torch.is_tensor()如果傳遞的對象是PyTorch張量,則方法返回True。

用法: torch.is_tensor(object) 

參數

  • object:這是要測試的輸入張量。

返回:它返回True或False。

讓我們借助幾個示例來了解這個概念:
範例1:

# Importing the PyTorch library  
import torch  
    
# A constant tensor of size n 
a = torch.FloatTensor([1, 4, 6, 9]) 
print(a) 
  
# Applying the is_tensor function and  
# storing the result in 'out' 
out = torch.is_tensor(a) 
print(out)

輸出:

1
 4
 6
 9
[torch.FloatTensor of size 4]
True

範例2:

# Importing the PyTorch library  
import torch  
    
# A constant tensor of size n 
a = torch.randn(4, 6) 
print(a) 
  
# Applying the is_tensor function and  
# storing the result in 'out' 
out = torch.is_tensor(a) 
print(out)

輸出:

 0.7491 -1.5987 -0.9733  0.0436 -0.3093  2.0007
 0.5679 -0.0092 -0.2573  0.9173  2.9849 -2.0159
-1.9215 -0.9131 -0.8244  0.4160 -0.3855  0.7033
 1.7367 -1.1454 -1.4369 -0.9856 -0.9076  0.6267
[torch.FloatTensor of size 4x6]
True

相關用法


注:本文由純淨天空篩選整理自PranchalKatiyar大神的英文原創作品 Python – PyTorch is_tensor() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。