pytorch教程网络和损失函数的可视化代码示例
目录
- 1.效果
- 2.环境
- 3.用到的代码
1.效果
文章图片
2.环境 1.pytorch
2.visdom
3.python3.5
3.用到的代码
# coding:utf8import torchfrom torch import nn, optim# nn 神经网络模块 optim优化函数模块from torch.utils.data import DataLoaderfrom torch.autograd import Variablefrom torchvision import transforms, datasetsfrom visdom import Visdom# 可视化处理模块import timeimport numpy as np# 可视化appviz = Visdom()# 超参数BATCH_SIZE = 40LR = 1e-3EPOCH = 2# 判断是否使用gpuUSE_GPU = Trueif USE_GPU:gpu_status = torch.cuda.is_available()else:gpu_status = Falsetransform=transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.1307,), (0.3081,))])# 数据引入train_dataset = datasets.MNIST('../data', True, transform, download=False)test_dataset = datasets.MNIST('../data', False, transform)train_loader = DataLoader(train_dataset, BATCH_SIZE, True)# 为加快测试,把测试数据从10000缩小到2000test_data = https://www.it610.com/article/torch.unsqueeze(test_dataset.test_data, 1)[:1500]test_label = test_dataset.test_labels[:1500]# visdom可视化部分数据viz.images(test_data[:100], nrow=10)#viz.images(test_data[:100], nrow=10)# 为防止可视化视窗重叠现象,停顿0.5秒time.sleep(0.5)if gpu_status:test_data = test_data.cuda()test_data = Variable(test_data, volatile=True).float()# 创建线图可视化窗口line = viz.line(np.arange(10))# 创建cnn神经网络class CNN(nn.Module):def __init__(self, in_dim, n_class):super(CNN, self).__init__()self.conv = nn.Sequential(# channel 为信息高度 padding为图片留白 kernel_size 扫描模块size(5x5)nn.Conv2d(in_channels=in_dim, out_channels=16,kernel_size=5,stride=1, padding=2),nn.ReLU(),# 平面缩减 28x28>> 14*14nn.MaxPool2d(kernel_size=2),nn.Conv2d(16, 32, 3, 1, 1),nn.ReLU(),# 14x14 >> 7x7nn.MaxPool2d(2))self.fc = nn.Sequential(nn.Linear(32*7*7, 120),nn.Linear(120, n_class))def forward(self, x):out = self.conv(x)out = out.view(out.size(0), -1)out = self.fc(out)return outnet = CNN(1,10)if gpu_status :net = net.cuda()#print("#"*26, "使用gpu", "#"*26)else:#print("#" * 26, "使用cpu", "#" * 26)pass# loss、optimizer 函数设置loss_f = nn.CrossEntropyLoss()optimizer = optim.Adam(net.parameters(), lr=LR)# 起始时间设置start_time = time.time()# 可视化所需数据点time_p, tr_acc, ts_acc, loss_p = [], [], [], []# 创建可视化数据视窗text = viz.text("convolution Nueral Network")for epoch in range(EPOCH):# 由于分批次学习,输出loss为一批平均,需要累积or平均每个batch的loss,accsum_loss, sum_acc, sum_step = 0., 0., 0.for i, (tx, ty) in enumerate(train_loader, 1):if gpu_status:tx, ty = tx.cuda(), ty.cuda()tx = Variable(tx)ty = Variable(ty)out = net(tx)loss = loss_f(out, ty)#print(tx.size())#print(ty.size())#print(out.size())sum_loss += loss.item()*len(ty)#print(sum_loss)pred_tr = torch.max(out,1)[1]sum_acc += sum(pred_tr==ty).item()sum_step += ty.size(0)# 学习反馈optimizer.zero_grad()loss.backward()optimizer.step()# 每40个batch可视化一下数据if i % 40 == 0:if gpu_status:test_data = https://www.it610.com/article/test_data.cuda()test_out = net(test_data)print(test_out.size())# 如果用gpu运行out数据为cuda格式需要.cpu()转化为cpu数据 在进行比较pred_ts = torch.max(test_out, 1)[1].cpu().data.squeeze()print(pred_ts.size())rightnum = pred_ts.eq(test_label.view_as(pred_ts)).sum().item()#rightnum =sum(pred_tr==ty).item()#sum_acc += sum(pred_tr==ty).item()acc =rightnum/float(test_label.size(0))print("epoch: [{}/{}] | Loss: {:.4f} | TR_acc: {:.4f} | TS_acc: {:.4f} | Time: {:.1f}".format(epoch+1, EPOCH,sum_loss/(sum_step), sum_acc/(sum_step), acc, time.time()-start_time))# 可视化部分time_p.append(time.time()-start_time)tr_acc.append(sum_acc/sum_step)ts_acc.append(acc)loss_p.append(sum_loss/sum_step)viz.line(X=np.column_stack((np.array(time_p), np.array(time_p), np.array(time_p))),Y=np.column_stack((np.array(loss_p), np.array(tr_acc), np.array(ts_acc))),win=line,opts=dict(legend=["Loss", "TRAIN_acc", "TEST_acc"]))# visdom text 支持html语句viz.text("epoch:{}
Loss:{:.4f}
""TRAIN_acc:{:.4f}
TEST_acc:{:.4f}
""Time:{:.2f}
".format(epoch, sum_loss/sum_step, sum_acc/sum_step, acc,time.time()-start_time),win=text)sum_loss, sum_acc, sum_step = 0., 0., 0.
【pytorch教程网络和损失函数的可视化代码示例】以上就是pytorch教程网络和损失函数的可视化代码示例的详细内容,更多关于pytorch教程网络和损失函数的可视化的资料请关注脚本之家其它相关文章!
推荐阅读
- parallels|parallels desktop 解决网络初始化失败问题
- 2.6|2.6 Photoshop操作步骤的撤消和重做 [Ps教程]
- 猎杀IP
- 漫画初学者如何学习漫画背景的透视画法(这篇教程请收藏好了!)
- 自媒体形势分析
- 数学大作战
- 2018.03.18
- 星期天的下午茶(一)
- pytorch|使用pytorch从头实现多层LSTM
- 08黑龙江迟淑荣弯柳树网络学院第五期学习赵宗瑞老师主讲的(传统文化与身心健康)教育体系心得体会