日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

pytorch中with?torch.no_grad():的用法實例_python

作者:這是一只小菜雞 ? 更新時間: 2022-05-14 編程語言

1.關于with

with是python中上下文管理器,簡單理解,當要進行固定的進入,返回操作時,可以將對應需要的操作,放在with所需要的語句中。比如文件的寫入(需要打開關閉文件)等。

以下為一個文件寫入使用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后的語句運行,將其返回結果給到as后的變量(sh),之后的代碼塊對close進行操作。

2.關于with torch.no_grad():

在使用pytorch時,并不是所有的操作都需要進行計算圖的生成(計算過程的構建,以便梯度反向傳播等操作)。而對于tensor的計算操作,默認是要進行計算圖的構建的,在這種情況下,可以使用 with torch.no_grad():,強制之后的內容不進行計算圖構建。

以下分別為使用和不使用的情況:

(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)

運行結果:

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():

而對應的不使用的情況

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)

結果如下:

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=)

可以看到,此時有grad_fn=屬性,表示,計算的結果在一計算圖當中,可以進行梯度反傳等操作。但是,兩者計算的結果實際上是沒有區別的。

附:pytorch使用模型測試使用with torch.no_grad():

使用pytorch時,并不是所有的操作都需要進行計算圖的生成(計算過程的構建,以便梯度反向傳播等操作)。而對于tensor的計算操作,默認是要進行計算圖的構建的,在這種情況下,可以使用 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)

運行結果:

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]])

總結

原文鏈接:https://blog.csdn.net/weixin_44134757/article/details/105775027

欄目分類
最近更新