温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用pytorch怎么对tensor与numpy数组进行转换

发布时间:2021-02-26 16:22:19 来源:亿速云 阅读:153 作者:戴恩恩 栏目:开发技术

这篇文章主要介绍了使用pytorch怎么对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

到此这篇关于使用pytorch怎么对tensor与numpy数组进行转换的文章就介绍到这了,更多相关使用pytorch怎么对tensor与numpy数组进行转换的内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI