将 PyTorch Tensor 转换为 Python 列表

方法一:使用 .tolist() 方法(推荐)

python 复制代码
import torch

# 创建一个 tensor
tensor = torch.tensor([9.6919, -0.6950, 11.8760, 1.6362, 8.3674, 9.2179])

# 转换为列表
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() 函数

python 复制代码
import torch

tensor = torch.tensor([9.6919, -0.6950, 11.8760, 1.6362, 8.3674, 9.2179])

# 转换为列表
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)

python 复制代码
import torch

tensor = torch.tensor([9.6919, -0.6950, 11.8760, 1.6362, 8.3674, 9.2179])

# 先转换为 numpy 数组,再转换为列表
list_result = tensor.numpy().tolist()

print(list_result)
print(type(list_result))

处理 GPU Tensor

如果 tensor 在 GPU 上,需要先移动到 CPU:

python 复制代码
import torch

# 假设 tensor 在 GPU 上
tensor_gpu = torch.tensor([9.6919, -0.6950, 11.8760, 1.6362, 8.3674, 9.2179]).cuda()

# 先移动到 CPU,再转换为列表
list_result = tensor_gpu.cpu().tolist()

print(list_result)

处理多维 Tensor

python 复制代码
import torch

# 二维 tensor
tensor_2d = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])

# 转换为嵌套列表
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]]]

完整示例

python 复制代码
import torch

def tensor_to_list(tensor):
    """
    将 PyTorch tensor 转换为 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("转换后的列表:", result)
print("列表类型:", type(result))
print("列表元素类型:", type(result[0]))

注意事项

  1. .tolist() vs list():

    • .tolist(): 推荐使用,适用于任何维度的 tensor,返回嵌套列表
    • list(): 只适用于一维 tensor,对于多维 tensor 会返回 tensor 列表而不是数值列表
  2. 数据类型: 转换后的列表元素会保持 tensor 的数据类型(如 float32、float64 等)

  3. 性能 : 对于大型 tensor,.tolist() 可能比 list() 稍快

  4. 梯度 : 如果 tensor 有梯度,.tolist() 会自动处理,只返回数值

推荐做法

始终使用 .tolist() 方法,因为它:

  • 适用于任何维度的 tensor
  • 自动处理 CPU/GPU 转换(需要先调用 .cpu()
  • 代码更清晰易读
  • 性能更好
python 复制代码
# 最佳实践
list_result = tensor.tolist()  # 对于 CPU tensor
# 或
list_result = tensor.cpu().tolist()  # 对于可能在 GPU 上的 tensor
相关推荐
研究点啥好呢5 分钟前
途游游戏AI产品经理面试题精选:10道高频考题+答案解析
人工智能·游戏·产品经理
KG_LLM图谱增强大模型8 分钟前
从数据孤岛到知识融合:用友大型本体模型LOM如何赋能企业知识管理和智能决策
人工智能·知识图谱
码以致用9 分钟前
用 DeepAgents 自动分析表格数据,一键生成图表与报告
人工智能·ai编程
lbb 小魔仙12 分钟前
基于Python构建RAG(检索增强生成)系统:从原理到企业级实战
开发语言·python
码上掘金13 分钟前
基于深度学习的行人计数与人群密度分析系统设计与实现
人工智能·深度学习
北京软秦科技有限公司18 分钟前
灌封胶耐候测试报告为何更依赖“AI报告审核”?IACheck如何提升长期环境可靠性判断精度
人工智能
程序员果子21 分钟前
Agent设计手册:四层架构、工程约束、框架选型
人工智能·agent·智能体·agent框架
2401_8322981025 分钟前
SaaS 到 Agent-as-a-Service——OpenClaw 生态爆发,开启企业数字化新时代
人工智能
SunnyDays101130 分钟前
Python 如何精准统计 Word 文档的页数、字数、行数
python·word文档字数统计
AI产品测评官32 分钟前
2026年AI招聘架构深潜:多Agent协同如何打造主动出击智能体代表?
人工智能·架构