人工智能编程:如何可视化神经网络算法模型的训练过程?

  
 
  

人工智能编程:如何可视化神经网络算法模型的训练过程?

文章插图
 
 
人工智能编程:如何可视化神经网络算法模型的训练过程?

文章插图
 
 
本文将介绍一个和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 Visdomimport numpy as npimport torchx=np.arange(0,10)y=np.arange(0,10)*9print(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 torchimport torch.nn as nnimport torch.nn.functional as Fimport torch.optim as optimfrom torchvision import datasets, transformsfrom visdom import Visdombatch_size=200learning_rate=0.01epochs=10train_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 xdevice = 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 = 0for epoch in range(epochs):for batch_idx, (data, target) in enumerate(train_loader):data = https://www.isolves.com/it/ai/2020-03-31/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 += 1viz.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 = 0correct = 0for 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)))
【人工智能编程:如何可视化神经网络算法模型的训练过程?】


    推荐阅读