pytorch 实现tensor与numpy数组转换
作者:头发光了你就强了 时间:2022-02-07 16:06:44
看代码,tensor转numpy:
a = torch.ones(2,2)
b = a.numpy()
c=np.array(a) #也可以转numpy数组
print(type(a))
print(type(b))
print(a)
print(b)
输出为:
<class ‘torch.Tensor'>
<class ‘numpy.ndarray'>
tensor([[1., 1.],
[1., 1.]])
[[1. 1.]
[1. 1.]]
numpy转tensor:
import torch
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
c=torch.Tensor(a) #也可以转pytorch Tensor
print(type(a))
print(type(b))
print(a)
print(b)
输出为:
<class ‘numpy.ndarray'>
<class ‘torch.Tensor'>
[1. 1. 1. 1. 1.]
tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
可见pytorch的tensor对象与numpy数组是可以相互转换的,且numpy数组的默认类型是double
来源:https://blog.csdn.net/qq_33345917/article/details/86552152
标签:pytorch,tensor,numpy,转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
matplotlib共享坐标轴的实现(X或Y坐标轴)
2023-12-01 23:58:12
![](https://img.aspxhome.com/file/2023/5/83805_0s.png)
Python OpenCV图像颜色变换示例
2023-07-30 06:05:00
![](https://img.aspxhome.com/file/2023/5/93505_0s.jpg)
通过事务日志解决SQL Server常见四大故障(二)
2009-03-25 13:51:00
学习ASP和编程的28个观点
2008-06-27 12:57:00
python使用两种发邮件的方式smtp和outlook示例
2021-02-12 13:31:54
详解Python如何获取列表(List)的中位数
2022-02-01 02:35:37
![](https://img.aspxhome.com/file/2023/0/86180_0s.png)
WEB2.0网页制作标准教程(8)CSS布局入门
2007-09-11 13:21:00
![](https://img.aspxhome.com/file/uploadpic/20079/11/200791113246711.gif)
ASP获取ACCESS数据库的表名以及表名对应的字段名和字段类型
2009-12-25 18:57:00
Date对象格式化方法
2009-11-16 13:17:00
用户体验至上的网页设计秘笈
2007-09-07 10:16:00
Win7的IIS7中ASP获得的系统日期格式为斜杠的解决办法
2012-12-04 19:57:33
IE6,IE7中定位相关的怪异问题
2009-12-08 12:49:00
![](https://img.aspxhome.com/file/UploadPic/200912/8/position_demo1-45s.png)
Python跑循环时内存泄露的解决方法
2023-07-18 08:18:22
![](https://img.aspxhome.com/file/2023/5/131225_0s.jpg)
一个带采集远程文章内容,保存图片,生成文件等完整的采集功能
2011-04-02 11:02:00
教你如何利用Python批量翻译英文Word文档并保留格式
2022-10-18 21:27:17
![](https://img.aspxhome.com/file/2023/5/121035_0s.png)
2010怎么就宅了——我们是设计星球的阿凡达
2010-03-09 13:26:00
![](https://img.aspxhome.com/file/UploadPic/20103/9/1003-2010zai-01-16s.jpg)
Python中动态创建类实例的方法
2023-07-20 02:36:44
Python学习笔记之os模块使用总结
2023-05-12 07:46:21
Python二叉搜索树与双向链表转换实现方法
2022-08-23 12:46:34
Python Flask-Login实现用户会话管理
2023-06-05 13:13:50
![](https://img.aspxhome.com/file/2023/9/90589_0s.png)