当前位置:网站首页>使用Visdom對損失函數進行監控
使用Visdom對損失函數進行監控
2022-06-11 18:21: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數據進行訓練》。
效果

參考:
边栏推荐
- H. 264 concept
- ctfhub-sql布尔盲注
- RadioGroup动态添加RadioButton
- [pat grade B question bank] complete summary
- DataNode的启动流程
- Ctfhub SQL Boolean blind annotation
- New work of "the father of LSTM": a new method towards self correcting neural network
- Talking about telework | community essay solicitation
- [FAQs for novices on the road] about project management
- 互联网_业务分析概述
猜你喜欢

Cryptology Summary

SISO decoder for a general (n,n-1) SPC code(補充章節3)

关于keil中,while循环条件不成立却无法跳出的问题
![Spring 2021 daily question [week7 not finished]](/img/93/582608e18bf6d78c552fa9478cdd77.jpg)
Spring 2021 daily question [week7 not finished]

網絡安全威脅情報體系
![[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration](/img/ae/9a0c300f2dcb03b05d737f14b0955f.jpg)
[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration

Sorted circular linked list

Reading summary of nacos2.x source code

使用mysql判断日期是星期几

How to learn and self-study
随机推荐
互联网_业务分析概述
Is it good or not to open a stock account on the flush? Is it safe?
Spring 2021 daily question [week7 not finished]
【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits
[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)
[c language] output the average score and the student data below or equal to the average score with the structure
Sorted circular linked list
MATLAB 保存imshow绘制图片到指定文件夹中的两种方法
Implementation of servlet file upload function (Commons fileUpload)
关于keil中,while循环条件不成立却无法跳出的问题
Ctfhub SQL Boolean blind annotation
软件测试技术复习
Global and Chinese market of web content management software 2022-2028: Research Report on technology, participants, trends, market size and share
Reading summary of nacos2.x source code
Sqli labs customs clearance hey hey~
[C语言]限制查找次数,输出次数内查找到的最大值
求字符串中最大的 3 位相同数字
[golang] leetcode - 292 Nim games (Mathematics)
[pat grade B question bank] complete summary
[FAQs for novices on the road] about project management