【colab pytorch】使用tensorboardcolab可视化
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils, datasets !pip install tensorboardcolab
from tensorboardcolab import TensorBoardColab
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5, 1)
self.conv2 = nn.Conv2d(20, 50, 5, 1)
self.fc1 = nn.Linear(4*4*50, 500)
self.fc2 = nn.Linear(500, 10) def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2, 2)
x = x.view(-1, 4*4*50)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return F.log_softmax(x, dim=1)
class Config:
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value) model_config = Config(
cuda = True if torch.cuda.is_available() else False,
device = torch.device("cuda" if torch.cuda.is_available() else "cpu"),
seed = 2,
lr = 0.01,
epochs = 4,
save_model = False,
batch_size = 32,
log_interval = 100
) class Trainer: def __init__(self, config): self.cuda = config.cuda
self.device = config.device
self.seed = config.seed
self.lr = config.lr
self.epochs = config.epochs
self.save_model = config.save_model
self.batch_size = config.batch_size
self.log_interval = config.log_interval self.globaliter = 0
self.tb = TensorBoardColab() torch.manual_seed(self.seed) kwargs = {'num_workers': 1, 'pin_memory': True} if self.cuda else {} self.train_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((MNIST_MEAN,), (MNIST_STD,))
])),
batch_size=self.batch_size, shuffle=True, **kwargs) self.test_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((MNIST_MEAN,), (MNIST_STD,))
])),
batch_size=self.batch_size, shuffle=True, **kwargs) self.model = Network().to(self.device)
self.optimizer = optim.Adam(self.model.parameters(), lr=self.lr) def train(self, epoch): self.model.train()
for batch_idx, (data, target) in enumerate(self.train_loader): self.globaliter += 1
data, target = data.to(self.device), target.to(self.device) self.optimizer.zero_grad()
predictions = self.model(data) loss = F.nll_loss(predictions, target)
loss.backward()
self.optimizer.step() if batch_idx % self.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(self.train_loader.dataset),
100. * batch_idx / len(self.train_loader), loss.item()))
self.tb.save_value('Train Loss', 'train_loss', self.globaliter, loss.item()) def test(self, epoch):
self.model.eval()
test_loss = 0
correct = 0 with torch.no_grad():
for data, target in self.test_loader:
data, target = data.to(self.device), target.to(self.device)
predictions = self.model(data) test_loss += F.nll_loss(predictions, target, reduction='sum').item()
prediction = predictions.argmax(dim=1, keepdim=True)
correct += prediction.eq(target.view_as(prediction)).sum().item() test_loss /= len(self.test_loader.dataset)
accuracy = 100. * correct / len(self.test_loader.dataset) print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(self.test_loader.dataset), accuracy)) def main(): trainer = Trainer(model_config) for epoch in range(1, trainer.epochs + 1):
trainer.train(epoch)
trainer.test(epoch)
trainer.tb.flush_line('train_loss') if (trainer.save_model):
torch.save(trainer.model.state_dict(),"mnist_cnn.pt")
main()
Wait for 8 seconds...
TensorBoard link:
http://db797eee.ngrok.io
Train Epoch: 1 [0/60000 (0%)] Loss: 2.320306
Train Epoch: 1 [3200/60000 (5%)] Loss: 0.881239
Train Epoch: 1 [6400/60000 (11%)] Loss: 0.013655
Train Epoch: 1 [9600/60000 (16%)] Loss: 0.013620
Train Epoch: 1 [12800/60000 (21%)] Loss: 0.225101
Train Epoch: 1 [16000/60000 (27%)] Loss: 0.248218
Train Epoch: 1 [19200/60000 (32%)] Loss: 0.207354
Train Epoch: 1 [22400/60000 (37%)] Loss: 0.139395
Train Epoch: 1 [25600/60000 (43%)] Loss: 0.206405
Train Epoch: 1 [28800/60000 (48%)] Loss: 0.090241
Train Epoch: 1 [32000/60000 (53%)] Loss: 0.216764
Train Epoch: 1 [35200/60000 (59%)] Loss: 0.295801
Train Epoch: 1 [38400/60000 (64%)] Loss: 0.021000
Train Epoch: 1 [41600/60000 (69%)] Loss: 0.050552
Train Epoch: 1 [44800/60000 (75%)] Loss: 0.238085
Train Epoch: 1 [48000/60000 (80%)] Loss: 0.298676
Train Epoch: 1 [51200/60000 (85%)] Loss: 0.301436
Train Epoch: 1 [54400/60000 (91%)] Loss: 0.271787
Train Epoch: 1 [57600/60000 (96%)] Loss: 0.019811 Test set: Average loss: 0.1088, Accuracy: 9677/10000 (97%) Train Epoch: 2 [0/60000 (0%)] Loss: 0.036418
Train Epoch: 2 [3200/60000 (5%)] Loss: 0.024196
Train Epoch: 2 [6400/60000 (11%)] Loss: 0.029856
Train Epoch: 2 [9600/60000 (16%)] Loss: 0.084013
Train Epoch: 2 [12800/60000 (21%)] Loss: 0.345446
Train Epoch: 2 [16000/60000 (27%)] Loss: 0.453756
Train Epoch: 2 [19200/60000 (32%)] Loss: 0.409682
Train Epoch: 2 [22400/60000 (37%)] Loss: 0.159656
Train Epoch: 2 [25600/60000 (43%)] Loss: 0.009557
Train Epoch: 2 [28800/60000 (48%)] Loss: 0.282826
Train Epoch: 2 [32000/60000 (53%)] Loss: 0.047159
Train Epoch: 2 [35200/60000 (59%)] Loss: 0.379264
Train Epoch: 2 [38400/60000 (64%)] Loss: 0.043181
Train Epoch: 2 [41600/60000 (69%)] Loss: 0.486660
Train Epoch: 2 [44800/60000 (75%)] Loss: 0.108486
Train Epoch: 2 [48000/60000 (80%)] Loss: 0.242821
Train Epoch: 2 [51200/60000 (85%)] Loss: 0.218120
Train Epoch: 2 [54400/60000 (91%)] Loss: 0.381496
Train Epoch: 2 [57600/60000 (96%)] Loss: 0.134828 Test set: Average loss: 0.1861, Accuracy: 9496/10000 (95%) Train Epoch: 3 [0/60000 (0%)] Loss: 0.081437
Train Epoch: 3 [3200/60000 (5%)] Loss: 0.121195
Train Epoch: 3 [6400/60000 (11%)] Loss: 0.054902
Train Epoch: 3 [9600/60000 (16%)] Loss: 0.031254
Train Epoch: 3 [12800/60000 (21%)] Loss: 0.036273
Train Epoch: 3 [16000/60000 (27%)] Loss: 0.162744
Train Epoch: 3 [19200/60000 (32%)] Loss: 0.028073
Train Epoch: 3 [22400/60000 (37%)] Loss: 0.114689
Train Epoch: 3 [25600/60000 (43%)] Loss: 0.139724
Train Epoch: 3 [28800/60000 (48%)] Loss: 0.353534
Train Epoch: 3 [32000/60000 (53%)] Loss: 0.001959
Train Epoch: 3 [35200/60000 (59%)] Loss: 0.117742
Train Epoch: 3 [38400/60000 (64%)] Loss: 0.024078
Train Epoch: 3 [41600/60000 (69%)] Loss: 0.063214
Train Epoch: 3 [44800/60000 (75%)] Loss: 0.068128
Train Epoch: 3 [48000/60000 (80%)] Loss: 0.055476
Train Epoch: 3 [51200/60000 (85%)] Loss: 0.025761
Train Epoch: 3 [54400/60000 (91%)] Loss: 0.490388
Train Epoch: 3 [57600/60000 (96%)] Loss: 0.275244 Test set: Average loss: 0.1570, Accuracy: 9594/10000 (96%) Train Epoch: 4 [0/60000 (0%)] Loss: 0.150237
Train Epoch: 4 [3200/60000 (5%)] Loss: 0.049188
Train Epoch: 4 [6400/60000 (11%)] Loss: 0.008692
Train Epoch: 4 [9600/60000 (16%)] Loss: 0.061360
Train Epoch: 4 [12800/60000 (21%)] Loss: 0.004389
Train Epoch: 4 [16000/60000 (27%)] Loss: 0.027968
Train Epoch: 4 [19200/60000 (32%)] Loss: 0.075881
Train Epoch: 4 [22400/60000 (37%)] Loss: 0.074000
Train Epoch: 4 [25600/60000 (43%)] Loss: 0.069731
Train Epoch: 4 [28800/60000 (48%)] Loss: 0.330368
Train Epoch: 4 [32000/60000 (53%)] Loss: 0.393174
Train Epoch: 4 [35200/60000 (59%)] Loss: 0.318519
Train Epoch: 4 [38400/60000 (64%)] Loss: 0.164669
Train Epoch: 4 [41600/60000 (69%)] Loss: 0.161486
Train Epoch: 4 [44800/60000 (75%)] Loss: 0.017525
Train Epoch: 4 [48000/60000 (80%)] Loss: 0.104918
Train Epoch: 4 [51200/60000 (85%)] Loss: 0.000450
Train Epoch: 4 [54400/60000 (91%)] Loss: 0.128227
Train Epoch: 4 [57600/60000 (96%)] Loss: 0.005374 Test set: Average loss: 0.1227, Accuracy: 9717/10000 (97%)
核心就是标红的地方。
【colab pytorch】使用tensorboardcolab可视化的更多相关文章
- Pytorch的网络结构可视化(tensorboardX)(详细)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/xiaoxifei/article/det ...
- 【colab pytorch】使用tensorboard可视化
import datetime import torch import torch.nn as nn import torch.nn.functional as F import torch.opti ...
- Pytorch使用tensorboardX可视化。超详细!!!
tensorboard --logdir runs 改为 tensorboard --logdir=D:\model\tensorboard\runs 重点 在网上看了很多方法后发现将原本链接中的计算 ...
- pytorch模型结构可视化,可显示每层的尺寸
最近在学习一些检测方面的网络,使用的是pytorch.模型结构可视化是学习网络的有用的部分,pytorch没有原生支持这个功能,需要找一些其他方式,下面总结几种方法(推荐用4). 1. torch . ...
- Pytorch在colab和kaggle中使用TensorBoard/TensorboardX可视化
在colab和kaggle内核的Jupyter notebook中如何可视化深度学习模型的参数对于我们分析模型具有很大的意义,相比tensorflow, pytorch缺乏一些的可视化生态包,但是幸好 ...
- 【猫狗数据集】利用tensorboard可视化训练和测试过程
数据集下载地址: 链接:https://pan.baidu.com/s/1l1AnBgkAAEhh0vI5_loWKw提取码:2xq4 创建数据集:https://www.cnblogs.com/xi ...
- [源码解析] PyTorch分布式优化器(1)----基石篇
[源码解析] PyTorch分布式优化器(1)----基石篇 目录 [源码解析] PyTorch分布式优化器(1)----基石篇 0x00 摘要 0x01 从问题出发 1.1 示例 1.2 问题点 0 ...
- [源码解析] PyTorch分布式优化器(2)----数据并行优化器
[源码解析] PyTorch分布式优化器(2)----数据并行优化器 目录 [源码解析] PyTorch分布式优化器(2)----数据并行优化器 0x00 摘要 0x01 前文回顾 0x02 DP 之 ...
- 实战 | 源码入门之Faster RCNN
前言 学习深度学习和计算机视觉,特别是目标检测方向的学习者,一定听说过Faster Rcnn:在目标检测领域,Faster Rcnn表现出了极强的生命力,被大量的学习者学习,研究和工程应用.网上有很多 ...
随机推荐
- 70)PHP,cookie的安全传输和HTTPonly
(1)是否仅安全传输:
- relieved|auction|calculate|campaign|charge for |chartered
ADJ-GRADED 感到宽慰的;感到安心的;宽心的If you are relieved, you feel happy because something unpleasant has not h ...
- DSU On Tree——Codeforces 600E(E. Lomsat gelral)
有这么一类问题,要求统计一棵树上与子树相关的某些信息,比如:在一棵所有节点被染色的树上,统计每棵子树上出现次数最多的颜色编号之和. 很自然的可以想到用DFS序+主席树去求解,但是编码复杂度很高: 然后 ...
- 吴裕雄--天生自然 R语言开发学习:回归(续四)
#------------------------------------------------------------# # R in Action (2nd ed): Chapter 8 # # ...
- python保留2位小数
1 代码: 2 运行结果: 3 其中字符串形式的:print('%.2f' % (x/y))方式最好
- Python之路购物车
#coding:utf-8 wages=raw_input('what are your wages:') lise_commodity=[('apples',50),('orange',55),(' ...
- Django ORM必会13条之外的查询方法
基于双下划线的查询 # 价格 大于 小于 大于等于 小于等于 filter(price__gt=') # 筛选出大于90 filter(price__lt=') # 筛选出小于90 filter(pr ...
- APP内计费规范出台 手游乱收费现象能被遏制?
手游乱收费现象能被遏制?" title="APP内计费规范出台 手游乱收费现象能被遏制?"> 在一个混乱.无秩序的环境中竞争,虽然有可能不择手段地获取更多的利益,但 ...
- Springboot整合Dubbo和Zookeeper
Dubbo是一款由阿里巴巴开发的远程服务调用框架(RPC),其可以透明化的调用远程服务,就像调用本地服务一样简单.截至目前,Dubbo发布了基于Spring Boot构建的版本,版本号为0.2.0,这 ...
- MDEV入门
------------- MDEV入门------------- 对于我们这些人知道如何使用的mdev ,底漆可能似乎跛.为其他人的mdev是一个神秘的黑匣子,他们听到的是真棒,但不能似乎得到他们的 ...