tensor - numpy - list 转换

创建日期: 2023-01-03 20:35 | 作者: 风波 | 浏览次数: 14 | 分类: PyTorch

tensor 初始化

import torch

tensor = torch.tensor([3,4,5,6])

tensor 转 Numpy

numpy_array = tensor.numpy()

numpy 转 tensor

import torch

torch.from_numpy(a)

list 转 numpy.array

# -*- coding: utf-8 -*-

import numpy as np


def main():
    alist = [1, 2, 3, 4, 5, 6]
    arr = np.array(alist)
    print(arr, type(arr))      # (array([1, 2, 3, 4, 5, 6]), <type 'numpy.ndarray'>)

if "__main__" == __name__:
    main()

numpy.array 转 list

# -*- coding: utf-8 -*-

import numpy as np


def main():
    alist = [1, 2, 3, 4, 5, 6]
    arr = np.array(alist)

    # numpy.array 转 list
    blist = arr.tolist()
    print(blist, type(blist))      # ([1, 2, 3, 4, 5, 6], <type 'list'>)

if "__main__" == __name__:
    main()

参考:https://blog.csdn.net/weixin_39655993/article/details/113960737

14 浏览
12 爬虫
0 评论