網站首頁 編程語言 正文
1 查看當前的device
輸入情況:
import torch print("Default Device : {}".format(torch.Tensor([4, 5, 6]).device))
輸出情況:
Default Device : cpu
2 cpu設備可以使用“cpu:0”來指定
輸入情況
device = torch.Tensor([1, 2, 3], device="cpu:0").device print("Device Type: {}".format(device))
輸出情況
Device Type: cpu
3 gpu設備可以使用“cuda:0”來指定
輸入情況
gpu = torch.device("cuda:0") print("GPU Device:【{}:{}】".format(gpu.type, gpu.index))
輸出情況
GPU Device:【cuda:0】
4 查詢CPU和GPU設備數量
輸入情況
print("Total GPU Count :{}".format(torch.cuda.device_count())) print("Total CPU Count :{}".format(torch.cuda.os.cpu_count()))
輸出情況
Total GPU Count :1
Total CPU Count :8
5 從CPU設備上轉換到GPU設備
5.1 torch.Tensor方法默認使用CPU設備
輸入情況
data = torch.Tensor([[1, 4, 7], [3, 6, 9], [2, 5, 8]]) print(data.shape)
輸出情況
torch.Size([3, 3])
5.2 使用to方法將cpu的Tensor轉換到GPU設備上
輸入情況:
data_gpu = data.to(torch.device("cuda:0")) print(data_gpu.device)
輸出情況:
cuda:0
5.3 使用.cuda方法將cpu的Tensor轉換到GPU設備上
輸入情況:
data_gpu2 = data.cuda(torch.device("cuda:0")) # 如果只有一塊gpu的話 直接寫成這樣:data_gpu2 = data.cuda() print(data_gpu2.device)
輸出情況:
cuda:0
原文鏈接:https://blog.csdn.net/weixin_42521185/article/details/123913221
相關推薦
- 2022-04-23 Tooltip 組件:根據內容是否溢出判斷是否顯示 Tooltip
- 2022-11-06 Python命令行參數解析包argparse的使用詳解_python
- 2022-06-16 詳解Python如何循環遍歷Numpy中的Array_python
- 2022-03-17 Redis快速部署為Docker容器的實現方法_docker
- 2022-10-02 react中(含hooks)同步獲取state值的方式_React
- 2022-10-23 Go語言數據結構之選擇排序示例詳解_Golang
- 2022-10-14 【Python】pytorch 保存模型、checkpoint
- 2022-03-30 c語言static關鍵字用法詳解_C 語言
- 最近更新
-
- 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同步修改后的遠程分支