網站首頁 編程語言 正文
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
相關推薦
- 2022-04-17 論一次 taro小程序分包優化經歷,小程序體積過大的優化
- 2022-11-03 C/C++預處理淺析使用形式_C 語言
- 2022-08-26 一篇文章學會GO語言中的變量_Golang
- 2022-06-23 詳解windows?server?2012的DHCP保留地址導出導入、DHCP故障轉移配置、DNS條
- 2022-10-17 Python速成篇之像selenium一樣操作電腦詳解_python
- 2024-03-21 Springboot實現接口傳輸加解密
- 2023-11-20 python設置matplotlib.plot的坐標刻度和坐標范圍
- 2022-08-17 C語言超全面覆蓋操作符知識點_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支