本文将介绍一个和pytorch紧密结合的机器学习库,visdom
Visdom的安装
Pip install visdom
如果安装失败
pip install --upgrade visdom
安装好之后,我们需要实时开启
Python -m visdom.server
然后会出现
在浏览输入这个网址就可以开启visdom了
当我们使用visdom画图的时候,我们需要
from visdom import Visdom
viz=Visdom()
然后就可以使用viz来进行画图了
画线的话可以使用viz.line
画图片的话可以使用viz.image
画文字的话可以使用viz.text
画线的时候,要先画一个起始点,然后后面的对它进行覆盖操作
from visdom import Visdom
import numpy as np
import torch
x=np.arange(0,10)
y=np.arange(0,10)*9
print(x)
viz=Visdom()
viz.line([0.],[0.],win="first",opts=dict(title='first'))
viz.line(y,x,win="first",update='Append')
画线的时候,可以先画一个其实的图,然后后面的对它进行添加操作,当然也可以直接来画图
viz.line([0.],[0.],win="first",opts=dict(title='first'))
表示画起始点
viz.line(y,x,win="first",update='append')
表示添加操作
其中win=""first"表示画在first的区域,主题名为first
然后
viz.line(y,x,win="first",update='append')
win="first"表示对first区域添加画图,append表示添加
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from visdom import Visdom
batch_size=200
learning_rate=0.01
epochs=10
train_loader = torch.utils.data.DataLoader(
datasets.MNIST('./data', train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
])),
batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('./data', train=False, transform=transforms.Compose([
transforms.ToTensor(),
])),
batch_size=batch_size, shuffle=True)
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.model = nn.Sequential(
nn.Linear(784, 200),
nn.LeakyReLU(inplace=True),
nn.Linear(200, 200),
nn.LeakyReLU(inplace=True),
nn.Linear(200, 10),
nn.LeakyReLU(inplace=True),
)
def forward(self, x):
x = self.model(x)
return x
device = torch.device('cpu')
net = MLP().to(device)
optimizer = optim.SGD(net.parameters(), lr=learning_rate)
criteon = nn.CrossEntropyLoss()
viz = Visdom()
viz.line([0.], [0.], win='train_loss', opts=dict(title='train loss'))
viz.line([[0.0, 0.0]], [0.], win='test', opts=dict(title='test loss&acc.',
legend=['loss', 'acc.']))
global_step = 0
for epoch in range(epochs):
for batch_idx, (data, target) in enumerate(train_loader):
data = data.view(-1, 28*28)
data, target = data.to(device), target.to(device)
logits = net(data)
#print(target)
loss = criteon(logits, target)
optimizer.zero_grad()
loss.backward()
# print(w1.grad.norm(), w2.grad.norm())
optimizer.step()
global_step += 1
viz.line([loss.item()], [global_step], win='train_loss', update='append')
if batch_idx % 100 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
test_loss = 0
correct = 0
for data, target in test_loader:
data = data.view(-1, 28 * 28)
data, target = data.to(device), target.to(device)
logits = net(data)
test_loss += criteon(logits, target).item()
pred = logits.argmax(dim=1)
correct += pred.eq(target).float().sum().item()
viz.line([[test_loss, correct / len(test_loader.dataset)]],
[global_step], win='test', update='append')
viz.images(data.view(-1, 1, 28, 28), win='x')
viz.text(str(pred.detach().cpu().numpy()), win='pred',
opts=dict(title='pred'))
test_loss /= len(test_loader.dataset)
print('nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))