当前位置:网站首页>解决:在验证阶段,第一个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)
边栏推荐
- glibc nptl库pthread_mutex_lock和pthread_mutex_unlock浅析
- 变压器只能转换交流电,那直流电怎么转换呢?
- F5《2022年应用策略现状报告》:边缘部署及负载安全成亚太地区关注焦点
- Important knowledge of golang: detailed explanation of context
- PHP specified fields are more than 100 in positive order and less than 100 in random order
- 快速排序的簡單理解
- Solution to the problem that MySQL cannot be started in xampp
- 30. concatenate substrings of all words
- Summary of operating system underlying knowledge (interview)
- System design and analysis - Technical Report - a solution for regularly clearing verification code
猜你喜欢

《墨者学院——SQL手工注入漏洞测试(MySQL数据库)》

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

任何代码未动的情况下第二天项目访问速度明显下降,案例分析

这五年的6个编程感悟!

The running rabbit fell
Solution to the problem that MySQL cannot be started in xampp

嵌入式软件架构设计-程序分层

Raspberry PI installing the wiring pi
![[MAE]Masked Autoencoders掩膜自编码器](/img/08/5ab2b0d5b81c723919046699bb6f6d.png)
[MAE]Masked Autoencoders掩膜自编码器

Gartner最新报告:低代码应用开发平台在国内的发展
随机推荐
VIM backup history command
php 二维数组插入
PHP指定字段大于100正序排,小于100随机排
TCP协议笔记
SQL injection vulnerability (principle)
Summary of operating system underlying knowledge (interview)
MySQL advanced statement 2
云上探“店”,云商店全新升级!
Thymeleaf——学习笔记
xcbdfbs xcvb
golang 重要知识:mutex
golang 重要知识:waitgroup 解析
ABP框架之——数据访问基础架构(下)
Volatile~ variables are not visible under multithreading
PHP 2D array insert
MySQL advanced statement I
js的slice()和splice()
Starting from 3, add paging function in the business system
12 BeautifulSoup类的初始化
B. AND 0, Sum Big-Codeforces Round #716 (Div. 2)