日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PyTorch——device与cuda.device用法

發(fā)布時間:2023/12/16 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PyTorch——device与cuda.device用法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 1 查看當(dāng)前的device
  • 2 cpu設(shè)備可以使用“cpu:0”來指定
  • 3 gpu設(shè)備可以使用“cuda:0”來指定
  • 4 查詢CPU和GPU設(shè)備數(shù)量
  • 5 從CPU設(shè)備上轉(zhuǎn)換到GPU設(shè)備
    • 5.1 torch.Tensor方法默認使用CPU設(shè)備
    • 5.2 使用to方法將cpu的Tensor轉(zhuǎn)換到GPU設(shè)備上
    • 5.3 使用.cuda方法將cpu的Tensor轉(zhuǎn)換到GPU設(shè)備上

1 查看當(dāng)前的device

  • 輸入情況:
import torch print("Default Device : {}".format(torch.Tensor([4, 5, 6]).device))
  • 輸出情況:
Default Device : cpu

2 cpu設(shè)備可以使用“cpu:0”來指定

  • 輸入情況
device = torch.Tensor([1, 2, 3], device="cpu:0").device print("Device Type: {}".format(device))
  • 輸出情況
Device Type: cpu

3 gpu設(shè)備可以使用“cuda:0”來指定

  • 輸入情況
gpu = torch.device("cuda:0") print("GPU Device:【{}:{}】".format(gpu.type, gpu.index))
  • 輸出情況
GPU Device:【cuda:0

4 查詢CPU和GPU設(shè)備數(shù)量

  • 輸入情況
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設(shè)備上轉(zhuǎn)換到GPU設(shè)備

5.1 torch.Tensor方法默認使用CPU設(shè)備

  • 輸入情況
data = torch.Tensor([[1, 4, 7], [3, 6, 9], [2, 5, 8]]) print(data.shape)
  • 輸出情況
torch.Size([3, 3])

5.2 使用to方法將cpu的Tensor轉(zhuǎn)換到GPU設(shè)備上

  • 輸入情況:
data_gpu = data.to(torch.device("cuda:0")) print(data_gpu.device)
  • 輸出情況:
cuda:0

5.3 使用.cuda方法將cpu的Tensor轉(zhuǎn)換到GPU設(shè)備上

  • 輸入情況:
data_gpu2 = data.cuda(torch.device("cuda:0")) # 如果只有一塊gpu的話 直接寫成這樣:data_gpu2 = data.cuda() print(data_gpu2.device)
  • 輸出情況:
cuda:0

總結(jié)

以上是生活随笔為你收集整理的PyTorch——device与cuda.device用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。