pytorch中with?torch.no_grad():的用法實(shí)例
1.關(guān)于with
with是python中上下文管理器,簡(jiǎn)單理解,當(dāng)要進(jìn)行固定的進(jìn)入,返回操作時(shí),可以將對(duì)應(yīng)需要的操作,放在with所需要的語(yǔ)句中。比如文件的寫(xiě)入(需要打開(kāi)關(guān)閉文件)等。
以下為一個(gè)文件寫(xiě)入使用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后的語(yǔ)句運(yùn)行,將其返回結(jié)果給到as后的變量(sh),之后的代碼塊對(duì)close進(jìn)行操作。
2.關(guān)于with torch.no_grad():
在使用pytorch時(shí),并不是所有的操作都需要進(jìn)行計(jì)算圖的生成(計(jì)算過(guò)程的構(gòu)建,以便梯度反向傳播等操作)。而對(duì)于tensor的計(jì)算操作,默認(rèn)是要進(jìn)行計(jì)算圖的構(gòu)建的,在這種情況下,可以使用 with torch.no_grad():,強(qiáng)制之后的內(nèi)容不進(jìn)行計(jì)算圖構(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)
運(yùn)行結(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]])
此時(shí)的outputs沒(méi)有 屬性。
(2)不使用with torch.no_grad():
而對(duì)應(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>)
可以看到,此時(shí)有g(shù)rad_fn=<AddmmBackward>屬性,表示,計(jì)算的結(jié)果在一計(jì)算圖當(dāng)中,可以進(jìn)行梯度反傳等操作。但是,兩者計(jì)算的結(jié)果實(shí)際上是沒(méi)有區(qū)別的。
附:pytorch使用模型測(cè)試使用with torch.no_grad():
使用pytorch時(shí),并不是所有的操作都需要進(jìn)行計(jì)算圖的生成(計(jì)算過(guò)程的構(gòu)建,以便梯度反向傳播等操作)。而對(duì)于tensor的計(jì)算操作,默認(rèn)是要進(jìn)行計(jì)算圖的構(gòu)建的,在這種情況下,可以使用 with torch.no_grad():,強(qiáng)制之后的內(nèi)容不進(jìn)行計(jì)算圖構(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)
運(yùn)行結(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用Pytorch實(shí)現(xiàn)線性回歸模型的步驟
線性關(guān)系是一種非常簡(jiǎn)單的變量之間的關(guān)系,因變量和自變量在線性關(guān)系的情況下,可以使用線性回歸算法對(duì)一個(gè)或多個(gè)因變量和自變量間的線性關(guān)系進(jìn)行建模,本文主要介紹了如何利用Pytorch實(shí)現(xiàn)線性模型,需要的朋友可以參考下2024-01-01基于PyQt5實(shí)現(xiàn)圖轉(zhuǎn)文功能(示例代碼)
PyQt提供了一個(gè)設(shè)計(jì)良好的窗口控件集合,具有更方便的操作性。學(xué)過(guò)VB的同學(xué)會(huì)知道,相比與VB的使用,在界面設(shè)計(jì)上元素更豐富,這篇文章主要介紹了基于PyQt5完成的圖轉(zhuǎn)文功能,需要的朋友可以參考下2022-06-06Python 使用Numpy對(duì)矩陣進(jìn)行轉(zhuǎn)置的方法
今天小編就為大家分享一篇Python 使用Numpy對(duì)矩陣進(jìn)行轉(zhuǎn)置的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01Python入門(mén)教程(四十三)Python的NumPy數(shù)據(jù)類(lèi)型
這篇文章主要介紹了Python入門(mén)教程(四十二)Python的NumPy數(shù)組裁切,NumPy有一些額外的數(shù)據(jù)類(lèi)型,并通過(guò)一個(gè)字符引用數(shù)據(jù)類(lèi)型,例如 i 代表整數(shù),u 代表無(wú)符號(hào)整數(shù)等,需要的朋友可以參考下2023-05-05