当前位置:网站首页>Monitoring loss functions using visdom
Monitoring loss functions using visdom
2022-06-11 18:23:00 【Yalin melon seeds】
problem
The loss function needs to be monitored Loss When did the training converge .
solve
pip
pip3 install visdom
Visdom
function :
python3 -m visdom.server
then , open http://localhost:8097/ that will do .
Python
Last , stay Python Just bury points in the program .
def cifar10_go():
# Instantiate a window
viz = Visdom(port=8097)
# Initialization window information
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, # Incoming dataset , Necessary parameters
batch_size=32, # Output batch size
shuffle=True, # Whether the data is disturbed
num_workers=4) # Number of processes , 0 Indicates that only the main process
model = MyCNN()
# Cross entropy loss function
criterion = nn.CrossEntropyLoss()
# Define optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, weight_decay=1e-2, momentum=0.9)
# Let's start training
start = time.time() # Time begins
for epoch in range(3): # Set the number of times to train on all data
for i, data in enumerate(dataloader):
# data It's the one we got batch size Size data
inputs, labels = data # Get the input data and its corresponding category results respectively
# First pass zero_grad() The function clears the gradient , Otherwise PyTorch Each calculation of the gradient will add up , If it is not cleared, the gradient calculated for the second time is equal to the gradient calculated for the first time plus the gradient calculated for the second time
optimizer.zero_grad()
# Get the output of the model , That is, the effect learned by the current model
outputs = model(inputs)
# Get the loss function of the real category of output results and data
loss = criterion(outputs, labels)
print('Epoch {}, Loss {}'.format(epoch + 1, loss))
# We're done loss Then back gradient propagation , After this process, the gradient will be recorded in the variable
loss.backward()
# Use the calculated gradient to optimize
optimizer.step()
# Update the listening information
viz.line([loss.item()], [i], win='train_loss', update='append')
end = time.time() # End of the timing
print(' Use your time : {:.5f} s'.format(end - start))
# Save model training results
torch.save(model, './MyCNN_model_23.pth')
Here is the last example :《PyTorch Use CIFAR-10 Data for training 》.
effect

Reference resources :
边栏推荐
猜你喜欢

求字符串中最大的 3 位相同数字
![[c language] limit the number of searches and output the maximum value found in the number of internal searches](/img/e6/cbb8dd54b49ade453251a70c8455e8.png)
[c language] limit the number of searches and output the maximum value found in the number of internal searches

VIM common commands

TR-069 protocol introduction
![[golang] leetcode - 292 Nim games (Mathematics)](/img/82/54c3f6be9d08687b42cba0487380f0.png)
[golang] leetcode - 292 Nim games (Mathematics)
![[C语言]用结构体把最高分的学生输出,可有多个最高分](/img/4e/836a8f717a2d9bf5f999a934ff4c91.png)
[C语言]用结构体把最高分的学生输出,可有多个最高分

Reading summary of nacos2.x source code

EasyCwmp源码分析

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

软件需求工程复习
随机推荐
SQL报错注入1
[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration
Feign shares login information for request
[document operation] of ACM
学习使用LSTM和IMDB评论数据进行情感分析训练
JS实现全屏展示的具体方法
如何学习和自学
The HashSet collection stores student objects and traverses
[golang] leetcode - 349 Intersection of two arrays (hash table)
Experiment 2: write a program and verify that the linear table sequence represents all operations
RadioGroup动态添加RadioButton
SQL语句当查询条件为空时默认查询全部数据,不为空是则按照条件进行查询
[C语言]限制查找次数,输出次数内查找到的最大值
[golang] leetcode - 292 Nim games (Mathematics)
[C语言]用结构体把最高分的学生输出,可有多个最高分
力扣23题,合并K个升序链表
使用Visdom对损失函数进行监控
DataNode的启动流程
Some thoughts on how to do a good job of operation and maintenance management
系统的可扩展型