tensor和numpy互相转换

numpy转为tensor

import torch
import numpy as np

arr1 = np.array([1,2,3], dtype=np.float32)
arr2 = np.array([4,5,6])
print(arr1.dtype)
print("nunpy中array的默认数据类型为:", arr2.dtype)

##########四种方法###########
'''
numpy中array默认的数据格式是int64类型,而torch中tensor默认的数据格式是float32类型。
as_tensor和from_numpy是浅拷贝,而tensor和Tensor则是属于深拷贝,浅拷贝是直接共享内存内存空间的,这样效率更高,而深拷贝是直接创建一个新的副本。
'''
tensor = torch.tensor(arr2)
Tensor = torch.Tensor(arr2)
as_tensor = torch.as_tensor(arr2)
from_numpy = torch.from_numpy(arr2)

print(tensor.dtype, "|",Tensor.dtype, "|",as_tensor.dtype, "|",from_numpy.dtype)
arr2[0] = 10
print(tensor, Tensor, as_tensor, from_numpy)
'''
结果为:
float32
numpy中array的默认数据类型为: int64
torch.int64 | torch.float32 | torch.int64 | torch.int64
tensor([4, 5, 6]) tensor([4., 5., 6.]) tensor([10,  5,  6]) tensor([10,  5,  6])
'''

tensor转为numpy

import torch
import numpy as np
a = torch.ones(5)
b = a.numpy()
b[0] = 2
 
print(a)
print(b)
'''
tensor([2., 1., 1., 1., 1.])
[2. 1. 1. 1. 1.]
numpy() 方法将tensor转numpy的array也是内存共享的
'''

转自:(60条消息) 【Pytorch】numpy数组与tensor互相转换的多种方法_numpy转tensor_郝同学的博客-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在PyTorch中,将Tensor转换NumPy数组有几种方法。一种常用的方法是使用`.numpy()`方法。这个方法将Tensor对象转换NumPy数组,但是需要注意的是,只有当Tensor对象在CPU上时才能进行转换。因此,如果你的Tensor对象在GPU上,需要先将其转移到CPU上,然后再使用`.numpy()`方法进行转换。具体代码如下所示: ```python import torch import numpy as np # 创建一个Tensor对象 a = torch.tensor([1, 2, 3]) # 将Tensor对象转换NumPy数组 b = a.cpu().numpy() # 打印转换后的结果 print(type(a)) print(type(b)) print(a) print(b) ``` 输出结果为: ``` <class 'torch.Tensor'> <class 'numpy.ndarray'> tensor([1, 2, 3]) <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [TensorNumpy互相转换](https://blog.csdn.net/m0_56676881/article/details/126912457)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [tensornumpy转换资源合集](https://download.csdn.net/download/Rocky006/87842415)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [pytorch 实现tensornumpy数组转换](https://download.csdn.net/download/weixin_38668225/13762306)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值