当前位置:网站首页>【PyTorch】cuda()与to(device)的区别
【PyTorch】cuda()与to(device)的区别
2022-07-23 02:50:00 【算法与编程之美】
问题
PyTorch中的Tensor要想在GPU中运行,可以有两种实现方式,其一是x.cuda(),其二是x.to(device)。两种方式均能实现GPU上运行,那么二者的区别是什么呢?
方法
import torch
device = 'cuda' if torch.cuda.is_available() else 'cpu'
a = torch.randn([3, 224, 224])
# (1) cuda()方式代码扩展性差,如果机器不支持GPU,则需要修改代码后才能在CPU上运行;
a.cuda()
# (2) to(device)方式代码扩展性好,即使没有GPU,也可以运行代码,不用做任何修改;
a.to(device)
结语
- 推荐使用to(device)的方式,主要原因在于这样的编程方式更加易于扩展,而cuda()必须要求机器有GPU,否则需要修改所有代码,to(device)的方式则不受此限制,device既可以是CPU也可以是GPU;
边栏推荐
- 2022-07-22:以下go语言代码输出什么?A:1;B:1.5;C:编译错误;D:1.49。 package main import “fmt“ func main() { var i
- es6相关面试题3
- D template error with different types
- 适合短视频分享的Fireshare
- seatunnel 架构
- Is it safe for CITIC futures to open an account online and will it be cheated?
- Baidu map eagle eye track service
- 隐藏网站服务器响应头中 PHP 版本信息
- nchar字符引起的ora-12899错误
- 567. 字符串的排列
猜你喜欢

Spark 内存管理机制 新版

IDM最新版软件的安装下载和使用方法

百度地图鹰眼轨迹服务

MySQL数据库提权学习

Use recursive string inversion and Full Permutation

31岁才转行程序员,目前34了,我来说说我的经历和一些感受吧...

.split(“,“, -1) 和 .split(“,“) 的区别

毕业1年,放弃实习机会,在家自学软件测试,同学实习刚结束,我已成月薪12k测试工程师

Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql

十年磨一剑,云原生分布式数据库PolarDB-X的核心技术演化
随机推荐
外地人在华泰开户安全吗?会被骗吗
XML的优缺点
Kill a process on Linux
Multi-UA V Cooperative Exploringfor the Unknown Indoor EnvironmentBased on Dynamic Target Tracking翻译
Burpsuite 插件的学习与使用
PHP 将 ASCII 码转为字符串,字符串转换 ASCII 码
Build the infrastructure construction of Internet of things with low code software
TDengine 助力西门子轻量级数字化解决方案 SIMICAS 简化数据处理流程
nchar字符引起的ora-12899错误
Want to give up software testing, 4 years of experience to interview 10 minutes, the test is now so difficult?
2022-07-22:以下go语言代码输出什么?A:1;B:1.5;C:编译错误;D:1.49。 package main import “fmt“ func main() { var i
Teach you how to set up Alibaba cloud DDNS in Qunhui
DigiCert代码签名证书
9.2章MATLAB的程序控制
毕业1年,放弃实习机会,在家自学软件测试,同学实习刚结束,我已成月薪12k测试工程师
Redis 6.0源码学习 Simple Dynamic String
本地提权的学习
C——位运算
电脑一直按键如何处理
Excel简单使用宏