当前位置:网站首页>使用Visdom对损失函数进行监控
使用Visdom对损失函数进行监控
2022-06-11 18:04:00 【亚林瓜子】
问题
需要监控损失函数Loss啥时候训练收敛了。
解决
pip
pip3 install visdom
Visdom
运行:
python3 -m visdom.server
然后,打开http://localhost:8097/即可。
Python
最后,在Python程序中埋点即可。
def cifar10_go():
# 实例化一个窗口
viz = Visdom(port=8097)
# 初始化窗口的信息
viz.line([0.], [0.], win='train_loss', opts=dict(title='train loss'))
transform = transforms.Compose([
transforms.RandomResizedCrop((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
cifar10_dataset = torchvision.datasets.CIFAR10(root='./data',
train=False,
transform=transform,
target_transform=None,
download=True)
dataloader = DataLoader(dataset=cifar10_dataset, # 传入的数据集, 必须参数
batch_size=32, # 输出的batch大小
shuffle=True, # 数据是否打乱
num_workers=4) # 进程数, 0表示只有主进程
model = MyCNN()
# 交叉熵损失函数
criterion = nn.CrossEntropyLoss()
# 定义优化器
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, weight_decay=1e-2, momentum=0.9)
# 下面开始训练
start = time.time() # 记时开始
for epoch in range(3): # 设置要在全部数据上训练的次数
for i, data in enumerate(dataloader):
# data就是我们获取的一个batch size大小的数据
inputs, labels = data # 分别得到输入的数据及其对应的类别结果
# 首先要通过zero_grad()函数把梯度清零,不然PyTorch每次计算梯度会累加,不清零的话第二次算的梯度等于第一次加第二次的
optimizer.zero_grad()
# 获得模型的输出结果,也即是当前模型学到的效果
outputs = model(inputs)
# 获得输出结果和数据真正类别的损失函数
loss = criterion(outputs, labels)
print('Epoch {}, Loss {}'.format(epoch + 1, loss))
# 算完loss之后进行反向梯度传播,这个过程之后梯度会记录在变量中
loss.backward()
# 用计算的梯度去做优化
optimizer.step()
# 更新监听的信息
viz.line([loss.item()], [i], win='train_loss', update='append')
end = time.time() # 计时结束
print('使用时间: {:.5f} s'.format(end - start))
# 保存模型训练结果
torch.save(model, './MyCNN_model_23.pth')
这里以为上一个例子:《PyTorch使用CIFAR-10数据进行训练》。
效果

参考:
边栏推荐
- ISCSI详解(四)——ISCSI服务端配置实战
- ACL 2022: is it no longer difficult to evaluate word polysemy? A new benchmark "dibimt"
- 初识企业级平台
- Sorted circular linked list
- 合并多棵二叉搜索树
- VIM common commands
- 社会工程学实战入门
- Sword finger offer (2nd Edition)
- SQL statement when the query condition is blank, all data will be queried by default. If it is not blank, the query will be performed according to the condition
- Hello go (XIII). Go language common standard library III
猜你喜欢

ACL 2022:评估单词多义性不再困扰?一种新的基准“DIBIMT”

Common shortcut keys for Hello go (x) and GoLand

LDPC 7 - 解码简单例子

Jsfinder, wafw00f installation, nmap configuration (msvcr120.dll file is missing)

TR-069协议介绍

DC-DC自举电容(BOOT)几个问题
![Codeworks round 481 (Div. 3) [done]](/img/60/01ed6180ccc4c99fe361d493525018.jpg)
Codeworks round 481 (Div. 3) [done]
![[C语言]用结构体把输入的指定分数范围内的学生输出](/img/40/cbd7fe5aafbaeb6237e6d257455e5e.png)
[C语言]用结构体把输入的指定分数范围内的学生输出
![[C语言]对一个数组的元素排序后平移元素](/img/5b/3e74fc40787d94f6d0ab93332140ba.png)
[C语言]对一个数组的元素排序后平移元素
![Spring 2021 daily question [week3 not finished]](/img/a4/72f2235d014613d26be0fc3524d236.jpg)
Spring 2021 daily question [week3 not finished]
随机推荐
MMA-Self-defining function
[untitled]
Secret comment-----
[c language] shift elements after sorting elements of an array
Common operations of Visio
Oracle高级数据库复习
ACL 2022: is it no longer difficult to evaluate word polysemy? A new benchmark "dibimt"
SISO Decoder for min-sum(补充章节2)
神经网络与深度学习-2- 机器学习简单示例-PyTorch
[C语言]限制查找次数,输出次数内查找到的最大值
Système d'information sur les menaces à la sécurité des réseaux
System learning typescript (V) - joint type
Global and Chinese market of high frequency bipolar junction transistors 2022-2028: Research Report on technology, participants, trends, market size and share
【无标题】
软件需求工程复习
初识企业级平台
Reading summary of nacos2.x source code
单选按钮 文字背景同时改变
论工作流选型
高性能架构设计