為了實(shí)現(xiàn)最大效率,哪個(gè)命令更有效?x = torch.randn(100, 100).to(device)x = torch.randn(100, 100, device = device)在進(jìn)行繁重的張量運(yùn)算時(shí),使用一個(gè)與另一個(gè)相比有什么好處嗎?有人告訴我其中一個(gè)效率較低,但無法正確計(jì)算如何比較兩者。我假設(shè)第二個(gè)更好,因?yàn)樗苯訉?dǎo)出到設(shè)備,而不是先創(chuàng)建張量,然后必須將其轉(zhuǎn)移到device.
1 回答

呼啦一陣風(fēng)
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊
好吧,我不是這方面的專家。這是給你的計(jì)時(shí)結(jié)果。
%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 上執(zhí)行了這兩個(gè)命令。
添加回答
舉報(bào)
0/150
提交
取消