Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PyTorch device and cuda.device

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use PyTorch device and cuda.device". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use PyTorch device and cuda.device.

1 View current device input: import torchprint ("Default Device: {}" .format (torch.Tensor ([4,5,6]) .device))

Output:

Default Device: cpu

2 cpu devices can be specified using "cpu:0"

Input situation

Device= torch.Tensor ([1,2,3], device= "cpu:0") .deviceprint ("Device Type: {}" .format (device))

Output situation

Device Type: cpu

3 gpu devices can be specified using "cuda:0"

Input situation

Gpu = torch.device ("cuda:0") print ("GPU Device: [{}: {}]" .format (gpu.type, gpu.index))

Output situation

GPU Device: [cuda:0]

4 query the number of CPU and GPU devices

Input situation

Print ("Total GPU Count: {}" .format (torch.cuda.device_count () print ("Total CPU Count: {}" .format (torch.cuda.os.cpu_count ()

Output situation

Total GPU Count: 1

Total CPU Count: 8

5 convert from CPU device to GPU device 5.1 torch.Tensor method uses CPU device by default

Input situation

Data = torch.Tensor ([[1,4,7], [3,6,9], [2,5,8]) print (data.shape)

Output situation

Torch.Size ([3,3])

5.2 use the to method to convert the Tensor of cpu to the GPU device

Input situation:

Data_gpu = data.to (torch.device ("cuda:0")) print (data_gpu.device)

Output:

Cuda:0

5.3 use the .cuda method to convert the Tensor of cpu to the GPU device

Input situation:

Data_gpu2 = data.cuda (torch.device ("cuda:0")) # if there is only one piece of gpu, write it directly like this: data_gpu2 = data.cuda () print (data_gpu2.device)

Output:

Cuda:0

Thank you for your reading, the above is the content of "how to use PyTorch device and cuda.device". After the study of this article, I believe you have a deeper understanding of how to use PyTorch device and cuda.device, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report