Featured image of post 【Pytorch】tensor.item() - 從 tensor 轉純量的方法 tensor.item()

【Pytorch】tensor.item() - 從 tensor 轉純量的方法 tensor.item()

紀錄怎麼用 .item() 把單一元素的 PyTorch tensor 轉回純量,方便查看或運算。

前言

在機器學習的領域中,我們經常用的資料格式就是 tensor,
但有時為了方便我們查看數值,我們需要將他轉回純量。

範例程式碼

我們能使用 「.item()」 來進行轉換,下面範例程式碼。

import torch
a = torch.tensor([2])

print(a)
print(a.item())
  • 結果

【Pytorch】tensor.item() - 從 tensor 轉純量的方法 tensor.item()

Licensed under CC BY-NC-SA 4.0
使用 Hugo 建立
主題 StackJimmy 設計