list python 转tensor_Pytorch--Tensor, Numpy--Array,Python--List 相互之间的转换。
版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_37589575/article/details/99446394
1.1 List --> Arrary: np.array(List 变量)
a = [1, 2, 3, 4]
b = np.array(a)
1.2 Arrary --> List: Array 变量.tolist()
a = [1, 2, 3, 4]
b = np.array(a)c = b.tolist()
2.1 List --> Tensor: torch.Tensor(List 变量)
a = [1, 2, 3, 4]
b = torch.Tensor(a)
2.2 Tensor --> List: Tensor 变量.numpy().tolist()
a = [1, 2, 3, 4]
b = torch.Tensor(a)c = b.numpy().tolist()
这里 c 和 a 的差异在于 List 转为 Tensor 的时候默认 Tensor 中的数据为 float 类型,所以转回来的时候会变为浮点数。
3.1 Array --> Tensor: torch.from_numpy(Array 变量)
a = [1, 2, 3, 4]
b = np.array(a)c = torch.from_numpy(b)
3.2 Tensor --> Array: torch.numpy(Tensor 变量)
a = [1, 2, 3, 4]
b = np.array(a)c = torch.from_numpy(b)
d = c.numpy()
如果需要转换 GPU 上的张量 Tensor,需要将其转换到 CPU 上,例如 GPU 上的 Tensor :
a = [1, 2, 3, 4]
b = np.array(a)c = torch.from_numpy(b).cuda()
d = c.numpy()
会报错: TypeError: can’t convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
这时应该使用
d = c.cpu().numpy()
先放回 CPU 在进行转换。
list python 转tensor_Pytorch--Tensor, Numpy--Array,Python--List 相互之间的转换。相关推荐
- python中数组(numpy.array)的基本操作【转载】
为什么要用numpy Python中提供了list容器,可以当作数组使用.但列表中的元素可以是任何对象,因此列表中保存的是对象的指针,这样一来,为了保存一个简单的列表[1,2,3].就需要三个指针和三 ...
- pytorch中tensor、numpy.array、list三者互相转换
1.1 list 转 numpy ndarray = np.array(list) 1.2 numpy 转 list list = ndarray.tolist() 2.1 list 转 torch. ...
- 多维tensor/numpy array的拼接(np.stack, torch.cat)
对于numpy array 当有如下情景,我需要4维tensor,(b,c,w,h), 而写的程序是单张图片遍历的,即每张图片(c,w,h),这时,就需要先用一个list,然后每次append,到最后 ...
- python读txt转array_np.array和txt文件的转换
np.array保存为txt 格式 numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='n', header='', footer ...
- [转载] 莫烦python学习笔记之numpy.array,dtype,empty,zeros,ones,arrange,linspace
参考链接: Python中的numpy.empty array:创建数组dtype:指定数据类型empty:创建数据接近0zeros:创建数据全为0ones:创建数据全为1arrange:按指定范围创 ...
- 轻松搞懂python列表list 和numpy.array数组的区别
文章目录 1.列表 2.数组(array) 3.程序举例 3.1 创建 3.2 元素访问 3.2.1一维数组.列表访问 3.2.2 二维数组.列表访问 3.3 浅拷贝与深拷贝 1.列表 list是处理 ...
- 莫烦python学习笔记之numpy.array,dtype,empty,zeros,ones,arrange,linspace
array:创建数组 dtype:指定数据类型 empty:创建数据接近0 zeros:创建数据全为0 ones:创建数据全为1 arrange:按指定范围创建数据 linspace:创建线段 imp ...
- python中找出numpy array数组的最值及其索引
在list列表中,max(list)可以得到list的最大值,list.index(max(list))可以得到最大值对应的索引 但在numpy中的array没有index方法,取而代之的是where ...
- python调用nastran,使用numpy在Python中导入nastran节点卡片组
If I want to import a Nastran Deck into a Python Array, in one fell swoop using numpy. How can I go ...
- python 获取唯一值_从Python列表中获取唯一值
python 获取唯一值 In this article, we will be understanding 3 ways to get unique values from a Python lis ...
最新文章
- 【对讲机的那点事】关于对讲机锂电池你了解多少?
- html相邻兄弟选择器,CSS CSS 相邻兄弟选择器 - 闪电教程JSRUN
- 在text html模版中写js,如何利用模板将HTML从JavaScript中抽离
- Python学习教程(Python学习路线):Python3之递归函数简单示例
- 自定义控件的子控件支持设计模式
- 【控制】《鲁棒控制-线性矩阵不等式处理方法》-俞立老师-第7章-保性能控制
- fifo的rdata_同步FIFO设计
- 大数据生态及其技术栈
- 2种继承方式学习笔记
- 红橙Darren视频笔记 view的绘制流程(下)基于API27
- 《密码爆破漏洞详解》——黑客必修的入门操作( 建议收藏 )
- 11. 给 apache ,nginx 设置变量
- AtomicInteger的CAS算法浅析
- db 如何将数据库中的表导入到PowerDesigner
- FANUC系统开发API(FOCAS2)pc应用开发,机加工领域
- iOS swift MD5加密
- 软件项目的测试计划和报告,如何撰写压力测试计划书与压力测试报告(一)
- 【*夜深了,无心睡眠*】
- 初识Shell Scripts编程--最最简单的shell例子
- 二维码也能成为大英博物馆的展品?