網(wǎng)站首頁 編程語言 正文
1.關(guān)于with
with是python中上下文管理器,簡單理解,當(dāng)要進(jìn)行固定的進(jìn)入,返回操作時,可以將對應(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后的語句運(yùn)行,將其返回結(jié)果給到as后的變量(sh),之后的代碼塊對close進(jìn)行操作。
2.關(guān)于with torch.no_grad():
在使用pytorch時,并不是所有的操作都需要進(jìn)行計算圖的生成(計算過程的構(gòu)建,以便梯度反向傳播等操作)。而對于tensor的計算操作,默認(rèn)是要進(jìn)行計算圖的構(gòu)建的,在這種情況下,可以使用 with torch.no_grad():,強(qiáng)制之后的內(nèi)容不進(jìn)行計算圖構(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]])
此時的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=)
可以看到,此時有g(shù)rad_fn=
附:pytorch使用模型測試使用with torch.no_grad():
使用pytorch時,并不是所有的操作都需要進(jìn)行計算圖的生成(計算過程的構(gòu)建,以便梯度反向傳播等操作)。而對于tensor的計算操作,默認(rèn)是要進(jìn)行計算圖的構(gòu)建的,在這種情況下,可以使用 with torch.no_grad():,強(qiáng)制之后的內(nèi)容不進(jìn)行計算圖構(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é)
原文鏈接:https://blog.csdn.net/weixin_44134757/article/details/105775027
相關(guān)推薦
- 2022-09-28 python?Scala函數(shù)與訪問修辭符實例詳解_python
- 2022-07-03 C++詳細(xì)講解函數(shù)調(diào)用與Struct和CLass的區(qū)別_C 語言
- 2022-03-14 內(nèi)存常用的頁面調(diào)度算法(頁面調(diào)度算法采用什么算法)
- 2022-04-20 C++的多態(tài)和虛函數(shù)你真的了解嗎_C 語言
- 2022-07-25 C/C++實現(xiàn)線性單鏈表的示例代碼_C 語言
- 2022-07-09 使用cgroup控制cpu、內(nèi)存、IO資源實踐
- 2023-03-20 Redis腦裂導(dǎo)致數(shù)據(jù)丟失的解決_Redis
- 2022-04-15 Go?interface{}?轉(zhuǎn)切片類型的實現(xiàn)方法_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支