当前位置:网站首页>[pytorch] the difference between cuda() and to (device)
[pytorch] the difference between cuda() and to (device)
2022-07-23 10:19:00 【Beauty of algorithm and programming】
problem
PyTorch Medium Tensor If you want to be in GPU Run in , There are two ways to do it , The first is x.cuda(), The second is x.to(device). Both methods can be realized GPU Up operation , So what's the difference between them ?
Method
import torch
device = 'cuda' if torch.cuda.is_available() else 'cpu'
a = torch.randn([3, 224, 224])
# (1) cuda() Poor extensibility of mode code , If the machine does not support GPU, You need to modify the code before you can CPU Up operation ;
a.cuda()
# (2) to(device) The mode code has good scalability , Even without GPU, You can also run code , You don't have to make any changes ;
a.to(device)
Conclusion
- Recommended to(device) The way , The main reason is that this programming method is easier to expand , and cuda() The machine must have GPU, Otherwise, you need to modify all the code ,to(device) The way is not limited by this ,device It can be either CPU It can also be GPU;
边栏推荐
- ArcGIS calculates the correlation between two grid layers
- 【VSCODE】当前工作目录非当前文件夹/pathlib打印cwd路径错误
- This tool complements the last kilometer of JMeter performance analysis
- Deeply understand mvcc and bufferpool caching mechanism
- Error msb4181: the "qtrunwork" task returned false, but no error was recorded
- Three goals and eight tasks of intelligent construction pilot city notice
- Is it safe for Huatai Securities to open an account online? Is it true
- 《nlp入门+实战:第一章:深度学习和神经网络》
- A brief tutorial for soft exam system architecture designer | requirements engineering
- Android development learning diary - content provider (cross application database modification)
猜你喜欢

SSM framework takeout ordering system

这个工具,补齐了 JMeter性能分析最后一公里短板

世界正在被开源软件吞食

L-半胱氨酸修饰的金纳米粒子(Cys-GNPs)和牛血清白蛋白/生物素化白蛋白纳米粒

浏览安全怎么提升?教你设置安全浏览器信任站点

RichView TextBox Items 文本框

Use modern development methods and thinking to get rid of the "stumbling block" of legacy systems

ARP Spoofing protection of network security

Qt报错:错误 C2039 “Value“: 不是 “`global namespace‘“ 的成员

2.判断语句
随机推荐
What is the experience of writing concurrent tool classes (semaphore, cyclicbarrier, countdownlatch) by yourself in line 30?
MySQL three table query problem
JS div scroll to the bottom
2.判断语句
Jeecgboot import document
Error msb4181: the "qtrunwork" task returned false, but no error was recorded
[C language foundation] 16 variable array (array length can be extended)
Use reflection to modify the member variable whose modifier is final
十年架构五年生活-05第一次出差
Reverse pairs in an array
客户至上 | 国产BI领跑者,思迈特软件完成C轮融资
Technology sharing | big transaction blocking show master status
The technical points of the new project can be guided if necessary
如何将list中相同字段值归类在同一个list下
redis token记录用户登录设计求解?
mysql三表查询问题
【MySQL】游标「Cursor」
S2SH+mysql的在线英语学习系统
[summary]
L-半胱氨酸修饰的金纳米粒子(Cys-GNPs)和牛血清白蛋白/生物素化白蛋白纳米粒