当前位置:网站首页>使用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數據進行訓練》。
效果

參考:
边栏推荐
- 软件需求工程复习
- ACL 2022: is it no longer difficult to evaluate word polysemy? A new benchmark "dibimt"
- [pat grade B question bank] complete summary
- Sa-Token 单点登录 SSO模式二 URL重定向传播会话示例
- 01.电信_领域业务经验
- NFT platform development NFT mall source code NFT mall development chain game development
- Winter vacation daily question (improvement group) [end of week 4]
- 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
- Spring 2021 daily question [week6 not finished]
- Say no to credit card fraud! 100 lines of code to realize simplified real-time fraud detection
猜你喜欢

Say no to credit card fraud! 100 lines of code to realize simplified real-time fraud detection
![[C语言]限制查找次数,输出次数内查找到的最大值](/img/e6/cbb8dd54b49ade453251a70c8455e8.png)
[C语言]限制查找次数,输出次数内查找到的最大值
![[C语言]压缩字符串并添加标记字符](/img/b7/f7918f3ee0c409faffc70addd5ee65.png)
[C语言]压缩字符串并添加标记字符

LDPC 7 - 解码简单例子

Nocturnal installs APK and BP agent

Network Security Threat Intelligence System
![[c language] output students' names and scores in descending order of scores with structures](/img/41/b9dba88941560b296f4d7153b7c684.png)
[c language] output students' names and scores in descending order of scores with structures

Easycwmp source code analysis
![[C语言]对一个数组的元素排序后平移元素](/img/5b/3e74fc40787d94f6d0ab93332140ba.png)
[C语言]对一个数组的元素排序后平移元素

SISO decoder for min sum (supplementary Chapter 2)
随机推荐
Feign 共享登录信息进行请求
MATLAB 保存imshow绘制图片到指定文件夹中的两种方法
[c language] limit the number of searches and output the maximum value found in the number of internal searches
Oracle高级数据库复习
Class question: how to ensure that line table storage can be inserted at any time?
01.电信_领域业务经验
DataNode的启动流程
任意用户密码重置的10种方式
论工作流选型
EditText 金额限制
Some thoughts on how to do a good job of operation and maintenance management
[FAQs for novices on the road] about project management
SISO decoder for a general (n,n-1) SPC code(補充章節3)
Spring 2021 daily question [end of week4]
SISO Decoder for min-sum(补充章节2)
Some problems of DC-DC bootstrap capacitor
New work of "the father of LSTM": a new method towards self correcting neural network
在同花顺上面开股票账户好不好,安不安全?
Global and Chinese market of web content management software 2022-2028: Research Report on technology, participants, trends, market size and share
谈谈远程工作 | 社区征文