【从矩阵到图像的类型转换3】:单维数据、List、Numpy.ndarray、torch.Tensor等的相互转换

一、单个数据转换为其他数据格式

1.1 单个数据 -> list

直接在数据上进行列表化操作,或者直接接入已经有的列表

语法:

list_name = [single_data] 或者 list_name.append(single_data)

1.2 单个数据 -> numpy.ndarray

直接使用初始化语法就可以

语法:

numpyarr_name = np.array(single_data)

举例:

import numpy as np
single_data = 100
numpyarr_test = np.array(single_data)
print(numpyarr_test)
print(numpyarr_test.dtype)
print(numpyarr_test.shape)

image-20210319093653825

1.3 单个数据 ->torch.tensor

直接使用初始化语法就可以

方法一语法:(但这种方法新建的是维度为0)

tensor_name = torch.tensor(single_data)

方法二语法:通过列表的方式进行新建tensor

tensor_name = torch.Tensor([single_data])

方法三语法:新建一个空张量然后赋值进去(这种方法新建的维度为1)

tensor_name = torch.empty(1)
tensor_name[0] = single_data  

举例:

import torch
single_data = 100
tensor_test = torch.tensor(single_data)
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)

single_data = 100
tensor_test = torch.Tensor([single_data])
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)

single_data = 100
tensor_test = torch.empty(1)
tensor_test[0] = single_data  
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)

image-20210319100639662

二、list 转换为其他数据格式

2.1 list -> 单个数据

直接使用python中的强制类型转换

2.2 list -> numpy.ndarray

注意:转换时list里面的类型必须一样

语法:

numpyarr_name = np.array(list_name)

举例:

import numpy as np
list_test = [1,2,3,4]
print(type(list_test))
numpyarr_test = np.array(list_test)
print(type(numpyarr_test))
print(numpyarr_test.dtype)
print(numpyarr_test.shape)

image-20210318224551965

2.3 list -> torch.tensor

注意:转换时list里面所有的元素的类型(包括多维度列表的元素中元素)必须一样

语法:

tensor_name = torch.Tensor(list_name) 

举例:

import torch
list_test = [[1,2,3],[4,5,6]]
print(type(list_test))
tensor_test = torch.Tensor(list_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)

image-20210318225625664

三、numpy.ndarray 转换为其他数据格式

3.1 numpy.ndarry -> 单个数据

直接使用python中的强制类型转换

举例:

import numpy as np
single_data = float(np.array([100]))
print(single_data)
print(type(single_data))

image-20210319100848124

3.2 numpy.ndarry -> list

语法:

numpyarr_name.tolist()

举例:

import numpy as np
numpyarr_test = np.ones((1,2))
list_test = numpyarr_test.tolist()
print(type(list_test))
print(len(list_test))
print(len(list_test[0]))

image-20210318224805672

3.3 numpy.ndarry -> torch.tensor

注意1:只能转换成CPU张量

注意2:shape不会改变

语法:

tensor_name = torch.from_numpy(numpyarr_name)

举例:

import torch
import numpy as np
numpyarr_test = np.ones((1,2))
print(type(numpyarr_test))
print(numpyarr_test.dtype)
tensor_test = torch.from_numpy(numpyarr_test)
print(type(tensor_test))
print(tensor_test.type())

image-20210318221447318

四、torch.tensor 转换为其他数据格式

4.1 torch.tensor -> 单个数据

直接使用python中的强制类型转换

举例:

import torch
tensor_test = torch.ones(1)
single_data = float(tensor_test)
print(single_data)
print(type(single_data))

image-20210319102351865

4.2 torch.tensor -> list

语法:

list_name =  tensor_name.tolist()

举例:

import torch
tensor_test = torch.ones((1,3))
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
list_test =  tensor_test.tolist()
print(list_test)
print(type(list_test))

image-20210319102813437

4.3 torch.tensor -> numpy.ndarry

注意:只有CPU张量能进行如此转换

注意:shape不会改变

语法:

numpyarr_name = tensor_name.numpy()
# 直接使用不进行赋值的话不会改变类型

举例:

import torch
import numpy as np
tensor_test = torch.Tensor(1,2)
print(type(tensor_test))
print(tensor_test.type())
numpyarr_test = tensor_test.numpy()
print(type(numpyarr_test))
print(numpyarr_test.dtype)

image-20210318221515828

LAST、参考文献

pytorch: tensor类型的构建与相互转换_JNing-CSDN博客

NumPy - 数据类型 · TutorialsPoint NumPy 教程

Pytorch 基本的数据类型_洪流之源-CSDN博客_pytorch 数据类型

[numpy] ndarray 与 list 互相转换_doufuxixi的博客-CSDN博客_ndarray转list

python3 list, np.array, torch.tensor相互转换_黑白德芙Sani的博客-CSDN博客

Python——array与list相互转换_Haiyang_Duan-CSDN博客

python数据分析之numpy初始化(一)_野孩子的专栏-CSDN博客

pytorch中tensor与其他数据结构的转化(int, list, array)_木盏-CSDN博客

【PyTorch学习笔记】3:创建Tensor的多种方法_LauZyHou的笔记-CSDN博客_创建tensor

猜你喜欢

转载自blog.csdn.net/qq_41554005/article/details/115001010
今日推荐