網站首頁 編程語言 正文
pytorch:tensor與numpy轉換 & .cpu.numpy()和.numpy() & torch.from_numpy VS torch.Tensor
作者:甘霖佳佳 更新時間: 2022-01-31 編程語言1.tensor to numpy
1.1代碼 .numpy()
import torch
a0 = torch.ones(6)
print(a0)
運行結果:
tensor([1., 1., 1., 1., 1., 1.])
a1 = a0.numpy()
print(a1)
運行結果:
[1. 1. 1. 1. 1. 1.]
1.2注意事項
①轉換后的tensor與numpy指向同一地址,所以對其中一個變量的值改變另一方也會隨之改變。
a0.add_(1)
print('a0=',a0)
print('a1=',a1)
運行結果:
a0= tensor([3., 3., 3., 3., 3., 3.])
a1= [3. 3. 3. 3. 3. 3.]
②如果想把CUDA tensor格式的數據改成numpy時,需要先將其轉換成cpu float-tensor隨后再轉到numpy格式。
numpy不能直接讀取CUDA tensor,需要將它轉化為 CPU tensor。
import torch
a=torch.ones(6)
if torch.cuda.is_available():
a=a.cuda()
print(a)
運行結果:
tensor([1., 1., 1., 1., 1., 1.], device=‘cuda:0’)
a=a.numpy()
運行結果:
TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
a=a.cpu().numpy()
print(a)
運行結果:
[1. 1. 1. 1. 1. 1.]
2.numpy to tensor
2.1 代碼 torch.from.numpy
import numpy as np
a = np.ones(6)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print('a=',a)
print('b=',b)
print('typea=',a.dtype)
print('typeb=',b.dtype)
運行結果:
a= [2. 2. 2. 2. 2. 2.]
b= tensor([2., 2., 2., 2., 2., 2.], dtype=torch.float64)
typea= float64
typeb= torch.float64
注意: 轉換后的tensor與numpy指向同一地址,所以對其中一個變量的值改變另一方也會隨之改變。且轉換后的數據類型與源類型一致。
2.2 torch.Tensor
import numpy as np
a = np.ones(6)
b = torch.Tensor(a)
np.add(a, 1, out=a)
print('a=',a)
print('b=',b)
print('typea=',a.dtype)
print('typeb=',b.dtype)
運行結果:
a= [2. 2. 2. 2. 2. 2.]
b= tensor([1., 1., 1., 1., 1., 1.])
typea= float64
typeb= torch.float32
注意: 轉換后的tensor與numpy不指向同一地址,所以對其中一個變量的值改變另一方不會隨之改變。且不論源數據類型是什么,轉換后的數據類型均為float32。
2.3 注意事項
①torch.Tensor和torch.from.numpy都可以將numpy類轉化為tensor類,但torch.from.numpy更加安全,使用torch.Tensor在非float類型下會與預期不符。
也就是說,黨轉換的源是float類型,torch.Tensor與torch.from.numpy會共享一塊內存,且轉換后的結果類型都是torch.float32。
當轉換的源不是float類型,torch.Tensor得到的是torch.float32,而torch.from_numpy則是與源類型一致。
②除chartensor外所有tensor都可以轉換為numpy
原文鏈接:https://blog.csdn.net/weixin_45928096/article/details/122388300
相關推薦
- 2022-08-15 python?datetime模塊詳解_python
- 2022-08-16 python+pytest自動化測試函數測試類測試方法的封裝_python
- 2023-02-25 從迷你todo?命令行入門Rust示例詳解_Rust語言
- 2022-06-30 Unity多屏幕設置的具體方案_C#教程
- 2022-12-11 Go?模塊在下游服務抖動恢復后CPU占用無法恢復原因_Golang
- 2022-12-30 python中的decode()與encode()深入理解_python
- 2022-04-15 Android開發Jetpack組件WorkManager用例詳解_Android
- 2022-10-16 實例詳解Python中的numpy.abs和abs函數_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支