Example Analysis of Visualization of Network and loss function in pytorch
Editor to share with you the pytorch network and loss function visualization example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
1. Effect.
two。 Environment
1.pytorch
2.visdom
3.python3.5
3. The code used is # coding:utf8import torchfrom torch import nn, optim # nn neural network module optim optimization function module from torch.utils.data import DataLoaderfrom torch.autograd import Variablefrom torchvision import transforms Datasetsfrom visdom import Visdom # Visualization processing module import timeimport numpy as np# Visualization appviz = Visdom () # Hyperparameter BATCH_SIZE = 40LR = 1e-3EPOCH = judge whether to use 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) ))]) # data introduction train_dataset = datasets.MNIST ('.. / data', True, transform, download=False) test_dataset = datasets.MNIST ('.. / data', False, transform) train_loader = DataLoader (train_dataset, BATCH_SIZE, True) # to speed up testing Reduce the test data from 10000 to 2000test_data = torch.unsqueeze (test_dataset.test_data, 1) [: 1500] test_label = test_dataset.test_labels [: 1500] # visdom Visualization part data viz.images (test_data [: 100], nrow=10) # viz.images (test_data [: 100], nrow=10) # to prevent visual window overlap Pause 0.5 seconds time.sleep (0.5) if gpu_status: test_data = test_data.cuda () test_data = Variable (test_data, volatile=True). Float () # create line graph visualization window line = viz.line (np.arange (10)) # create cnn neural network class CNN (nn.Module): def _ init__ (self, in_dim, n_class): super (CNN Self). _ init__ () self.conv = nn.Sequential (# channel for information height padding leaves blank for picture kernel_size scanning module size (5x5) nn.Conv2d (in_channels=in_dim, out_channels=16,kernel_size=5,stride=1, padding=2), nn.ReLU (), # plane reduction 28x28 > > 14014 nn.MaxPool2d (kernel_size=2) Nn.Conv2d (16,32,3,1,1), nn.ReLU (), # 14x14 > > 7x7 nn.MaxPool2d (2) self.fc = nn.Sequential (nn.Linear (327,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 (1Power10) if gpu_status: net = net.cuda () # print ("#" * 26, "use gpu", "#" * 26) else: # print ("#" * 26, "use cpu" "#" * 26) pass# loss and optimizer functions set loss_f = nn.CrossEntropyLoss () optimizer = optim.Adam (net.parameters (), lr=LR) # start time sets start_time = time.time () # data points required for visualization time_p, tr_acc, ts_acc, loss_p = [] [] # create visual data window text = viz.text ("convolution Nueral Network") for epoch in range (EPOCH): # because of batch learning The output loss is a batch average, and it is necessary to accumulate the average loss,acc sum_loss, sum_acc, sum_step of each batch of or. 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) # Learning feedback optimizer.zero_grad () loss.backward () optimizer.step () # visualize the data if I% 40 = 0: if gpu_status every 40 batch: Test_data = test_data.cuda () test_out = net (test_data) print (test_out.size ()) # if you use gpu to run out data in cuda format, you need .CPU () converted to cpu data before comparing pred_ts = torch.max (test_out) 1) [1] .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) # Visualization part 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 supports html statement 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. The above is all the contents of the article "sample Analysis of Network and loss function Visualization in pytorch". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!