.to(device) 在 pytorch 中的效率

为了实现最大效率,哪个命令更有效?


x = torch.randn(100, 100).to(device)

x = torch.randn(100, 100, device = device)

在进行繁重的张量运算时,使用一个与另一个相比有什么好处吗?有人告诉我其中一个效率较低,但无法正确计算如何比较两者。


我假设第二个更好,因为它直接导出到设备,而不是先创建张量,然后必须将其转移到device.


肥皂起泡泡
浏览 235回答 1
1回答

呼啦一阵风

好吧,我不是这方面的专家。这是给你的计时结果。%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 loopPS 我在 Google Colab 上执行了这两个命令。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python