当前位置:网站首页>Tensorboard visualization
Tensorboard visualization
2022-07-28 06:06:00 【Alan and fish】
Because when doing experiments , Need to record data , For the convenience of recording data , have access to tensorboard Record the experimental results during the operation , To compare the results of that experiment is the best .
tensorboard Common functions are , Save the model network structure , Save the loss of the training set , Let's introduce tensorboard Several common functions of .
1.tensorboard Panel Introduction

This panel image comes from :https://blog.csdn.net/Zhangsan0219/article/details/121763073
I wrote my notes again with reference to this author , Easy to record .
2. Draw a discount statistical chart
- Draw multiple pictures separately
# -*- coding: utf-8 -*-
# @Author :WenShuai Xiao
# @Time:2022-05-30
# @File:Tensorboard_line.py
import os.path
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from tqdm import tqdm
if __name__=='__main__':
print(' Beginning of the test ')
'''
Draw line
Start command tensorboard --logdir=logs
Designated port tensorboard --logdir=logs --port=6007
'''
# Create editor , Save log
# log_dir Save the path
logs_path=os.path.join("./logs","trian")
if not os.path.exists(logs_path):
print(' path does not exist ')
os.makedirs(logs_path)
writer=SummaryWriter(log_dir=logs_path)
# Simulate adding each epoch Of loss and Acc
for n_iter in tqdm(range(100)):
# writer.add_scalar Function to add a scalar value
# add_scalar(tags, scalar_value, global_stop)
# tags: string, Record the label of the object
# scalar_valud: float, Recorded value
# global_step: int, x Shaft value
writer.add_scalar('Loss/Train',np.random.random(),n_iter)
writer.add_scalar('Loss/Test',np.random.random(),n_iter)
writer.add_scalar('Accuracy/train',np.random.random(),n_iter)
writer.add_scalar('Accuracy/test',np.random.random(),n_iter)
writer.close()
print(' End of test ')
After running, it will be in logs File under which records are generated , Then find the path , Enter cmd-> enter , You can enter directly under this path dos command 
Then enter the following code , Copy the last link to the browser and you can see the result
# Activate the environment
conda activate pytorch
# Use tensorboard Open the path
tensorboard --dirlogs=logs

- Put multiple pictures together
# =====================================================
'''
writer.add_scalars(main_tag, tag_scalar_dict, global_step=None)
This function is used to combine multiple scalar Add to the same diagram
main_tag: string, Parent label
tag_scalar_dict: dict, Will be multiple scalar Dictionary objects grouped together ,
The dictionary's key Express tag, value For recorded scalar value
global_step: int, The image x Axis
'''
# ====================================================
for i in tqdm(range(100),desc=' Multiple pictures together '):
writer2.add_scalars(
'run_14h',{
'xsinx':i*np.sin(i/5),
'xcosx':i*np.cos(i/5),
'tanx':np.tan(i/5)
},i
)
writer2.close()
Show results 
The histogram behind , Continue to add notes when I need them
3. Draw model drawing
# -*- coding: utf-8 -*-
# @Author :WenShuai Xiao
# @Time:2022-05-30
# @File:Tensorboard_model.py
from torch import ones
from torch.utils.tensorboard import SummaryWriter
from torch.nn import Linear,ReLU,Sequential
import os
if __name__=='__main__':
logs_path = os.path.join("./logs", "model_graph")
if not os.path.exists(logs_path):
print(' path does not exist ')
os.makedirs(logs_path)
writer=SummaryWriter(logs_path)
# Write a model
model=Sequential(
Linear(1,10),
ReLU(),
Linear(10,1)
)
data=ones(10,1)
'''
add_graph
Draw the structure of the model
Double click the model to view
'''
writer.add_graph(model=model,input_to_model=data)
writer.close()
View the model diagram :
4. Multi model comparison
Sometimes it is necessary to adjust and compare the results of the experiment , This method can be used to save the results of each experiment to tensorboard in , If you learn bash grammar , Directly available bash Syntax automatically passes parameters , So you don't have to operate by yourself , Just wait patiently for the experimental results .
# -*- coding: utf-8 -*-
# @Author :WenShuai Xiao
# @Time:2022-05-30
# @File:Tensorboard_mutil_model.py
import os
import datetime
from torch.utils.tensorboard import SummaryWriter
if __name__=='__main__':
# Dynamic generation time
log_dir=os.path.join('logs',datetime.datetime.now().strftime("%Y%m%d_%H%M%S"))
if not os.path.exists(log_dir):
os.makedirs(log_dir)
writer=SummaryWriter(log_dir=log_dir)
# y=2x
for i in range(100):
writer.add_scalar(tag='y=2x',scalar_value=1.5*i,global_step=i)
writer.close()
View experiment results :

边栏推荐
- Distributed lock database implementation
- Sales notice: on July 22, the "great heat" will be sold, and the [traditional national wind 24 solar terms] will be sold in summer.
- Structured streaming in spark
- Manually create a simple RPC (< - < -)
- 小程序开发要多少钱?两种开发方法分析!
- 项目不报错,正常运行,无法请求到服务
- Digital collections strengthen reality with emptiness, enabling the development of the real economy
- 第七章 单行函数
- Construction of redis master-slave architecture
- Chapter 7 single line function
猜你喜欢
随机推荐
Chapter 7 single line function
Create a virtual environment using pycharm
xml解析实体工具类
【3】 Redis features and functions
Invalid packaging for parent POM x, must be “pom“ but is “jar“ @
速查表之转MD5
3: MySQL master-slave replication setup
更新包与已安装应用签名不一致
ModuleNotFoundError: No module named ‘pip‘
Books - Sun Tzu's art of war
Regular verification rules of wechat applet mobile number
It's not easy to travel. You can use digital collections to brush the sense of existence in scenic spots
分布式集群架构场景优化解决方案:分布式调度问题
matplotlib数据可视化
KubeSphere安装版本问题
JS!!
简单理解一下MVC和三层架构
【六】redis缓存策略
There is a problem with MySQL paging
Sales notice: on July 22, the "great heat" will be sold, and the [traditional national wind 24 solar terms] will be sold in summer.









