当前位置:网站首页>解决:在验证阶段,第一个batch不会报错,第二个batch报cuda超出的错误
解决:在验证阶段,第一个batch不会报错,第二个batch报cuda超出的错误
2022-06-23 15:09:00 【代码小白的成长】
问题描述:如果训练代码中不加入验证阶段。程序能够正常执行,不会出现下面cuda超出的现象。
但是,如果用epoch=1阶段结束后的模型去执行验证阶段,会出现验证集的第一个banch不会报错,但是执行第二个batch时,会提示cuda超了。原因可能是由于梯度在验证阶段不回传,造成梯度的累计。
解决办法:
把生成代码加入with torch.no_grad():内,来取消验证阶段的loss
with torch.no_grad(): # 由于梯度在验证阶段不回传,会造成梯度的累计,所以需要加上这句
gen_imgs = G(torch.autograd.Variable(label.cuda()))
验证代码:
# 验证:从样本集中选一些数据做验证
epoch_path = os.path.join(save_dir, "val_epoch%s" %(epoch))
visualize.mkdirs(epoch_path)
for n, items in enumerate(valDataset):
# input image data
label, _, name = items
with torch.no_grad(): # 由于梯度在验证阶段不回传,会造成梯度的累计,所以需要加上这句
gen_imgs = G(torch.autograd.Variable(label.cuda()))
visualize.saveImg(gen_imgs, epoch_path, 'generated', name[0])
message = "val: iter %d/%d" % (n + 1, valDatset_size)
print(message)
边栏推荐
- [pyside2] pyside2 window is on the top of Maya (note)
- F5 application strategy status report in 2022: edge deployment and load security become the focus of attention in the Asia Pacific Region
- Important knowledge of golang: timer timer
- 快速排序的简单理解
- Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data (attached with paper Download)
- 32. Compose 优美的触摸动画
- B. Integers Shop-Hello 2022
- 英特尔Arc A380显卡消息汇总:跑分亮眼驱动拉胯 入门性价产品亟待优化
- 2022年个人理财利率是多少?个人如何选择理财产品?
- Moher College - manual SQL injection vulnerability test (MySQL database)
猜你喜欢
mysql主从只同步部分库或表的思路与方法

Matlab| sparse auxiliary signal denoising and pattern recognition in time series data

Summary of operating system underlying knowledge (interview)
Redis缓存三大异常的处理方案梳理总结

golang 重要知识:RWMutex 读写锁分析

golang 重要知识:atomic 原子操作

30. concatenate substrings of all words

Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data (attached with paper Download)

重卡界销售和服务的“扛把子”,临沂广顺深耕产品全生命周期服务

Pop() element in JS
随机推荐
golang 重要知识:RWMutex 读写锁分析
Volatile~ variables are not visible under multithreading
RF analyzer demo setup
golang 重要知识:waitgroup 解析
Sorting out and summarizing the handling schemes for the three major exceptions of redis cache
股票开账户如何优惠开户?在线开户安全么?
mysql 系列:总体架构概述
Which platform is a good place to open a futures account? Is it safe to open an online futures account?
现在我要买股票,怎么开户?手机开户安全么?
进销存软件排行榜前十名!
C. Set or Decrease-Educational Codeforces Round 120 (Rated for Div. 2)
139. 单词拆分
The "shoulder" of sales and service in the heavy truck industry, Linyi Guangshun deep ploughing product life cycle service
Stone from another mountain - Intelligent Question and answer technology in wechat search
SFOD:无源域适配升级优化,让检测模型更容易适应新数据(附论文下载)
Half wave loss equal thickness and equal inclination interference
C. Phoenix and Towers-Codeforces Global Round 14
Important knowledge of golang: timer timer
Summary of operating system underlying knowledge (interview)
golang 重要知识:atomic 原子操作