pytorch中with?torch.no_grad():的用法實例
1.關(guān)于with
with是python中上下文管理器,簡單理解,當(dāng)要進行固定的進入,返回操作時,可以將對應(yīng)需要的操作,放在with所需要的語句中。比如文件的寫入(需要打開關(guān)閉文件)等。
以下為一個文件寫入使用with的例子。
with open (filename,'w') as sh: sh.write("#!/bin/bash\n") sh.write("#$ -N "+'IC'+altas+str(patientNumber)+altas+'\n') sh.write("#$ -o "+pathSh+altas+'log.log\n') sh.write("#$ -e "+pathSh+altas+'err.log\n') sh.write('source ~/.bashrc\n') sh.write('. "/home/kjsun/anaconda3/etc/profile.d/conda.sh"\n') sh.write('conda activate python27\n') sh.write('echo "to python"\n') sh.write('echo "finish"\n') sh.close()
with后部分,可以將with后的語句運行,將其返回結(jié)果給到as后的變量(sh),之后的代碼塊對close進行操作。
2.關(guān)于with torch.no_grad():
在使用pytorch時,并不是所有的操作都需要進行計算圖的生成(計算過程的構(gòu)建,以便梯度反向傳播等操作)。而對于tensor的計算操作,默認是要進行計算圖的構(gòu)建的,在這種情況下,可以使用 with torch.no_grad():,強制之后的內(nèi)容不進行計算圖構(gòu)建。
以下分別為使用和不使用的情況:
(1)使用with torch.no_grad():
with torch.no_grad(): for data in testloader: images, labels = data outputs = net(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print('Accuracy of the network on the 10000 test images: %d %%' % ( 100 * correct / total)) print(outputs)
運行結(jié)果:
Accuracy of the network on the 10000 test images: 55 %
tensor([[-2.9141, -3.8210, 2.1426, 3.0883, 2.6363, 2.6878, 2.8766, 0.3396,
-4.7505, -3.8502],
[-1.4012, -4.5747, 1.8557, 3.8178, 1.1430, 3.9522, -0.4563, 1.2740,
-3.7763, -3.3633],
[ 1.3090, 0.1812, 0.4852, 0.1315, 0.5297, -0.3215, -2.0045, 1.0426,
-3.2699, -0.5084],
[-0.5357, -1.9851, -0.2835, -0.3110, 2.6453, 0.7452, -1.4148, 5.6919,
-6.3235, -1.6220]])
此時的outputs沒有 屬性。
(2)不使用with torch.no_grad():
而對應(yīng)的不使用的情況
for data in testloader: images, labels = data outputs = net(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print('Accuracy of the network on the 10000 test images: %d %%' % ( 100 * correct / total)) print(outputs)
結(jié)果如下:
Accuracy of the network on the 10000 test images: 55 %
tensor([[-2.9141, -3.8210, 2.1426, 3.0883, 2.6363, 2.6878, 2.8766, 0.3396,
-4.7505, -3.8502],
[-1.4012, -4.5747, 1.8557, 3.8178, 1.1430, 3.9522, -0.4563, 1.2740,
-3.7763, -3.3633],
[ 1.3090, 0.1812, 0.4852, 0.1315, 0.5297, -0.3215, -2.0045, 1.0426,
-3.2699, -0.5084],
[-0.5357, -1.9851, -0.2835, -0.3110, 2.6453, 0.7452, -1.4148, 5.6919,
-6.3235, -1.6220]], grad_fn=<AddmmBackward>)
可以看到,此時有g(shù)rad_fn=<AddmmBackward>屬性,表示,計算的結(jié)果在一計算圖當(dāng)中,可以進行梯度反傳等操作。但是,兩者計算的結(jié)果實際上是沒有區(qū)別的。
附:pytorch使用模型測試使用with torch.no_grad():
使用pytorch時,并不是所有的操作都需要進行計算圖的生成(計算過程的構(gòu)建,以便梯度反向傳播等操作)。而對于tensor的計算操作,默認是要進行計算圖的構(gòu)建的,在這種情況下,可以使用 with torch.no_grad():,強制之后的內(nèi)容不進行計算圖構(gòu)建。
with torch.no_grad(): for data in testloader: images, labels = data outputs = net(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print('Accuracy of the network on the 10000 test images: %d %%' % ( 100 * correct / total)) print(outputs)
運行結(jié)果:
Accuracy of the network on the 10000 test images: 55 %
tensor([[-2.9141, -3.8210, 2.1426, 3.0883, 2.6363, 2.6878, 2.8766, 0.3396,
-4.7505, -3.8502],
[-1.4012, -4.5747, 1.8557, 3.8178, 1.1430, 3.9522, -0.4563, 1.2740,
-3.7763, -3.3633],
[ 1.3090, 0.1812, 0.4852, 0.1315, 0.5297, -0.3215, -2.0045, 1.0426,
-3.2699, -0.5084],
[-0.5357, -1.9851, -0.2835, -0.3110, 2.6453, 0.7452, -1.4148, 5.6919,
-6.3235, -1.6220]])
總結(jié)
到此這篇關(guān)于pytorch中with torch.no_grad():用法的文章就介紹到這了,更多相關(guān)pytorch中with torch.no_grad():內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于PyQt5實現(xiàn)圖轉(zhuǎn)文功能(示例代碼)
PyQt提供了一個設(shè)計良好的窗口控件集合,具有更方便的操作性。學(xué)過VB的同學(xué)會知道,相比與VB的使用,在界面設(shè)計上元素更豐富,這篇文章主要介紹了基于PyQt5完成的圖轉(zhuǎn)文功能,需要的朋友可以參考下2022-06-06Python 使用Numpy對矩陣進行轉(zhuǎn)置的方法
今天小編就為大家分享一篇Python 使用Numpy對矩陣進行轉(zhuǎn)置的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python入門教程(四十三)Python的NumPy數(shù)據(jù)類型
這篇文章主要介紹了Python入門教程(四十二)Python的NumPy數(shù)組裁切,NumPy有一些額外的數(shù)據(jù)類型,并通過一個字符引用數(shù)據(jù)類型,例如 i 代表整數(shù),u 代表無符號整數(shù)等,需要的朋友可以參考下2023-05-05