將PyTorch?Tensor轉(zhuǎn)換為Python列表的三種方法
方法一:使用.tolist()方法(推薦)
import torch # 創(chuàng)建一個 tensor tensor = torch.tensor([9.6919, -0.6950, 11.8760, 1.6362, 8.3674, 9.2179]) # 轉(zhuǎn)換為列表 list_result = tensor.tolist() print(list_result) print(type(list_result)) # 輸出: [9.6919, -0.695, 11.876, 1.6362, 8.3674, 9.2179] # 輸出: <class 'list'>
方法二:使用list()函數(shù)
import torch tensor = torch.tensor([9.6919, -0.6950, 11.8760, 1.6362, 8.3674, 9.2179]) # 轉(zhuǎn)換為列表 list_result = list(tensor) print(list_result) print(type(list_result)) # 輸出: [9.6919, -0.695, 11.876, 1.6362, 8.3674, 9.2179] # 輸出: <class 'list'>
方法三:使用.numpy()方法(適用于 CPU tensor)
import torch tensor = torch.tensor([9.6919, -0.6950, 11.8760, 1.6362, 8.3674, 9.2179]) # 先轉(zhuǎn)換為 numpy 數(shù)組,再轉(zhuǎn)換為列表 list_result = tensor.numpy().tolist() print(list_result) print(type(list_result))
處理 GPU Tensor
如果 tensor 在 GPU 上,需要先移動到 CPU:
import torch # 假設(shè) tensor 在 GPU 上 tensor_gpu = torch.tensor([9.6919, -0.6950, 11.8760, 1.6362, 8.3674, 9.2179]).cuda() # 先移動到 CPU,再轉(zhuǎn)換為列表 list_result = tensor_gpu.cpu().tolist() print(list_result)
處理多維 Tensor
import torch # 二維 tensor tensor_2d = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) # 轉(zhuǎn)換為嵌套列表 list_2d = tensor_2d.tolist() print(list_2d) # 輸出: [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] # 三維 tensor tensor_3d = torch.tensor([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]) list_3d = tensor_3d.tolist() print(list_3d) # 輸出: [[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]
完整示例
import torch
def tensor_to_list(tensor):
"""
將 PyTorch tensor 轉(zhuǎn)換為 Python 列表
"""
if tensor.is_cuda:
# 如果在 GPU 上,先移動到 CPU
tensor = tensor.cpu()
return tensor.tolist()
# 測試
tensor = torch.tensor([9.6919, -0.6950, 11.8760, 1.6362, 8.3674, 9.2179])
result = tensor_to_list(tensor)
print("原始 tensor:", tensor)
print("轉(zhuǎn)換后的列表:", result)
print("列表類型:", type(result))
print("列表元素類型:", type(result[0]))
注意事項
.tolist() vs list():
.tolist(): 推薦使用,適用于任何維度的 tensor,返回嵌套列表list(): 只適用于一維 tensor,對于多維 tensor 會返回 tensor 列表而不是數(shù)值列表
數(shù)據(jù)類型: 轉(zhuǎn)換后的列表元素會保持 tensor 的數(shù)據(jù)類型(如 float32、float64 等)
性能: 對于大型 tensor,.tolist() 可能比 list() 稍快
梯度: 如果 tensor 有梯度,.tolist() 會自動處理,只返回數(shù)值
推薦做法
始終使用 .tolist() 方法,因為它:
- 適用于任何維度的 tensor
- 自動處理 CPU/GPU 轉(zhuǎn)換(需要先調(diào)用
.cpu()) - 代碼更清晰易讀
- 性能更好
# 最佳實踐 list_result = tensor.tolist() # 對于 CPU tensor # 或 list_result = tensor.cpu().tolist() # 對于可能在 GPU 上的 tensor
到此這篇關(guān)于將PyTorch Tensor轉(zhuǎn)換為Python列表的三種方法的文章就介紹到這了,更多相關(guān)PyTorch Tensor轉(zhuǎn)為Python列表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
windowns使用PySpark環(huán)境配置和基本操作
pyspark是Spark對Python的api接口,可以在Python環(huán)境中通過調(diào)用pyspark模塊來操作spark,這篇文章主要介紹了windowns使用PySpark環(huán)境配置和基本操作,感興趣的可以了解一下2021-05-05
使用Python實現(xiàn)輕松調(diào)整視頻的播放速度
這篇文章主要介紹了如何通過 moviepy.editor 中的 VideoFileClip 類和 fx.speedx 函數(shù)實現(xiàn)輕松地調(diào)整視頻的播放速度,感興趣的可以了解下2024-11-11
python使用Pandas導(dǎo)出數(shù)據(jù)并保存為CSV文件
Pandas是Python中用于數(shù)據(jù)分析和處理的強大庫,它提供了靈活高效的數(shù)據(jù)結(jié)構(gòu),如DataFrame和Series,使得對數(shù)據(jù)的處理變得簡單易行,在實際應(yīng)用中,我們經(jīng)常需要將處理后的數(shù)據(jù)保存為CSV,所以本文給大家介紹了python使用Pandas導(dǎo)出數(shù)據(jù)并保存為CSV文件2024-12-12
教你用 Python 實現(xiàn)微信跳一跳(Mac+iOS版)
這幾天看網(wǎng)上好多微信跳一跳破解了,不過都是安卓的,無奈蘋果不是開源也沒辦法。本文給大家分享用 Python 來玩微信跳一跳(Mac+iOS版),具體實現(xiàn)代碼大家參考下本文2018-01-01
python中小數(shù)點后取2位(四舍五入)及取2位(四舍五不入)的方法
這篇文章主要給大家介紹了python中小數(shù)點后取2位(四舍五入)及取2位(四舍五不入)的方法,在Python中取兩位小數(shù)的方法其實非常簡單,需要的朋友可以參考下2023-08-08
Python Pyqt5多線程更新UI代碼實例(防止界面卡死)
這篇文章通過代碼實例給大家介紹了Python Pyqt5多線程更新UI防止界面卡死的問題,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-12-12

