首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

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

  • 25-03-03 18:41
  • 2615
  • 6935
blog.csdn.net

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

1.1 单个数据 -> list

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

语法:

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

1.2 单个数据 -> numpy.ndarray

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

语法:

numpyarr_name = np.array(single_data)
  • 1

举例:

import numpy as np
single_data = 100
numpyarr_test = np.array(single_data)
print(numpyarr_test)
print(numpyarr_test.dtype)
print(numpyarr_test.shape)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

image-20210319093653825

1.3 单个数据 ->torch.tensor

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

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

tensor_name = torch.tensor(single_data)
  • 1

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

tensor_name = torch.Tensor([single_data])
  • 1

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

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

举例:

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

image-20210319100639662

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

2.1 list -> 单个数据

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

2.2 list -> numpy.ndarray

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

语法:

numpyarr_name = np.array(list_name)
  • 1

举例:

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

image-20210318224551965

2.3 list -> torch.tensor

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

语法:

tensor_name = torch.Tensor(list_name) 
  • 1

举例:

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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))
  • 1
  • 2
  • 3
  • 4

image-20210319100848124

3.2 numpy.ndarry -> list

语法:

numpyarr_name.tolist()
  • 1

举例:

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]))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

image-20210318224805672

3.3 numpy.ndarry -> torch.tensor

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

注意2:shape不会改变

语法:

tensor_name = torch.from_numpy(numpyarr_name)
  • 1

举例:

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())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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))
  • 1
  • 2
  • 3
  • 4
  • 5

image-20210319102351865

4.2 torch.tensor -> list

语法:

list_name =  tensor_name.tolist()
  • 1

举例:

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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

image-20210319102813437

4.3 torch.tensor -> numpy.ndarry

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

注意:shape不会改变

语法:

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

举例:

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树科学计算基础软件包NumPyNumPy概述416632 人正在系统学习中
注:本文转载自blog.csdn.net的呆呆象呆呆的文章"https://blog.csdn.net/qq_41554005/article/details/115001010"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top