为了账号安全,请及时绑定邮箱和手机立即绑定

.to(device) 在 pytorch 中的效率

.to(device) 在 pytorch 中的效率

肥皂起泡泡 2022-06-14 16:58:32
为了实现最大效率,哪个命令更有效?x = torch.randn(100, 100).to(device)x = torch.randn(100, 100, device = device)在进行繁重的张量运算时,使用一个与另一个相比有什么好处吗?有人告诉我其中一个效率较低,但无法正确计算如何比较两者。我假设第二个更好,因为它直接导出到设备,而不是先创建张量,然后必须将其转移到device.
查看完整描述

1 回答

?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

好吧,我不是这方面的专家。这是给你的计时结果。


%timeit torch.randn(100, 100).to(device)


The slowest run took 12.65 times longer than the fastest. This could mean that an intermediate result is being cached.

10000 loops, best of 3: 129 µs per loop

%timeit torch.randn(100, 100, device = device)


The slowest run took 88.54 times longer than the fastest. This could mean that an intermediate result is being cached.

100000 loops, best of 3: 11.6 µs per loop

PS 我在 Google Colab 上执行了这两个命令。


查看完整回答
反对 回复 2022-06-14
  • 1 回答
  • 0 关注
  • 202 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号