为了实现最大效率,哪个命令更有效?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 上执行了这两个命令。
添加回答
举报
0/150
提交
取消