美文网首页
PyTrch深度学习简明实战35 - Tensorboard可视

PyTrch深度学习简明实战35 - Tensorboard可视

作者: 薛东弗斯 | 来源:发表于2023-09-10 06:19 被阅读0次

    tensorboard使用详解 - 知乎 (zhihu.com)

    pip install tensorboard -i https://pypi.doubanio.com/smple
    
    import torch
    import torch.nn as nn
    import torch.nn.functional as F
    import torch.optim as optim
    import numpy as np
    import matplotlib.pyplot as plt
    %matplotlib inline
    
    torch.__version__
    # 1.6.0
    
    torch.cuda.is_available()
    # True
    
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    device
    # device(type='cuda', index=0)
    
    import torchvision
    from torchvision import datasets, transforms
    
    transformation = transforms.Compose([
                                    transforms.ToTensor(), 
    ])
    
    train_ds = datasets.MNIST(
                              'data/',
                              train=True,
                              transform=transformation,
                              download=True  
    )
    
    test_ds = datasets.MNIST(
                              'data/',
                              train=False,
                              transform=transformation,
                              download=True  
    )
    
    train_dl = torch.utils.data.DataLoader(train_ds, batch_size=64, shuffle=True)
    test_dl = torch.utils.data.DataLoader(test_ds, batch_size=256)
    
    imgs, labels = next(iter(train_dl)
    imgs.shape 
    # torch.Size([64, 1, 28, 28])
    

    在pytorch里面图片的表示形式: 【batch, channel, hight, width】

    img = imgs[0]
    img.shape
    # torch.Size([1, 28, 28])
    
    img = img.numpy()
    img = np.squeeze(img)   # 删除一维信息
    img.shape
    # (28,28)
    
    plt.imshow(img)
    
    labels[0]
    # tensor(9)
    
    labels[:10]
    # tensor([9, 9, 0, 1, 9, 5, 9, 7, 0, 6])
    
    def imshow(img):
        npimg = img.numpy()
        npimg = np.squeeze(npimg)
        plt.imshow(npimg)
    
    # 显示10张独立的小图片
    plt.figure(figsize=(10, 1))
    for i, img in enumerate(imgs[:10]):
        plt.subplot(1, 10, i+1)
        imshow(img)
    
    labels[:10]
    # ([9, 9, 0, 1, 9, 5, 9, 7, 0, 6])
    

    Tensorboard 导入

    from torch.utils.tensorboard import SummaryWriter
    
    writer = SummaryWriter('my_log/mnist')
    
    # 显示图片
    images, labels = next(iter(train_dl))
    
    # create grid of images
    img_grid = torchvision.utils.make_grid(images[-8:])
    
    npimg = img_grid.permute(1, 2, 0).numpy()
    plt.imshow(npimg)   # 所有数字在一张图上面显示
    
    writer.add_image('eight_mnist_images', img_grid)   # 保存图片
    
    # 动态显示
    writer.add_image('eight_mnist_images_last', img_grid)
    
    # 在windows命令行中设定log路径
     > tensorboard --logdir=D:\163\tch\jk\tensorb\my_log
    # 设定完成后,后面会显示tensorboard显示图片的网页
    TensorBoard 2.4.1 at http://localhost:6006/ (Press CTRL+C to quit)
    

    创建模型

    class Model(nn.Module):
        def __init__(self):
            super().__init__()
            self.conv1 = nn.Conv2d(1, 6, 5)   
            self.pool = nn.MaxPool2d((2, 2))
            self.conv2 = nn.Conv2d(6, 16, 5) 
            self.liner_1 = nn.Linear(16*4*4, 256)
            self.liner_2 = nn.Linear(256, 10)
        def forward(self, input):
            x = F.relu(self.conv1(input))
            x = self.pool(x)
            x = F.relu(self.conv2(x))
            x = self.pool(x)
    #        print(x.size())    # torch.Size([64, 16, 4, 4])
            x = x.view(-1, 16*4*4)
            x = F.relu(self.liner_1(x))
            x = self.liner_2(x)
            return x
    
    model = Model()
    
    # 显示模型
    writer.add_graph(model, images)
    

    动态显示训练过程中的loss 和 acc的变化

    model.to(device)
    # Model(
    #  (conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))
    #  (pool): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0, dilation=1, ceil_mode=False)
    #  (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
    #  (liner_1): Linear(in_features=256, out_features=256, bias=True)
    #  (liner_2): Linear(in_features=256, out_features=10, bias=True)
    # )
    
    loss_fn = torch.nn.CrossEntropyLoss()  # 损失函数
    
    def fit(epoch, model, trainloader, testloader):
        correct = 0
        total = 0
        running_loss = 0
        for x, y in trainloader:
            x, y = x.to(device), y.to(device)
            y_pred = model(x)
            loss = loss_fn(y_pred, y)
            optim.zero_grad()
            loss.backward()
            optim.step()
            with torch.no_grad():
                y_pred = torch.argmax(y_pred, dim=1)
                correct += (y_pred == y).sum().item()
                total += y.size(0)
                running_loss += loss.item()
            
        epoch_loss = running_loss / len(trainloader.dataset)
        epoch_acc = correct / total
        
        writer.add_scalar('training loss',
                            epoch_loss,
                            epoch)
            
            
        test_correct = 0
        test_total = 0
        test_running_loss = 0 
        
        with torch.no_grad():
            for x, y in testloader:
                x, y = x.to(device), y.to(device)
                y_pred = model(x)
                loss = loss_fn(y_pred, y)
                y_pred = torch.argmax(y_pred, dim=1)
                test_correct += (y_pred == y).sum().item()
                test_total += y.size(0)
                test_running_loss += loss.item()
        
        epoch_test_loss = test_running_loss / len(testloader.dataset)
        epoch_test_acc = test_correct / test_total
        
        writer.add_scalar('test loss',
                            epoch_test_loss,
                            epoch)
        
            
        print('epoch: ', epoch, 
              'loss: ', round(epoch_loss, 3),
              'accuracy:', round(epoch_acc, 3),
              'test_loss: ', round(epoch_test_loss, 3),
              'test_accuracy:', round(epoch_test_acc, 3)
                 )
            
        return epoch_loss, epoch_acc, epoch_test_loss, epoch_test_acc
    
    optim = torch.optim.Adam(model.parameters(), lr=0.001)
    
    epochs = 20
    
    train_loss = []
    train_acc = []
    test_loss = []
    test_acc = []
    
    for epoch in range(epochs):
        epoch_loss, epoch_acc, epoch_test_loss, epoch_test_acc = fit(epoch,
                                                                     model,
                                                                     train_dl,
                                                                     test_dl)
        train_loss.append(epoch_loss)
        train_acc.append(epoch_acc)
        test_loss.append(epoch_test_loss)
        test_acc.append(epoch_test_acc)
    
    # epoch:  0 loss:  0.004 accuracy: 0.919 test_loss:  0.0 test_accuracy: 0.97
    # epoch:  1 loss:  0.001 accuracy: 0.975 test_loss:  0.0 test_accuracy: 0.976
    # epoch:  2 loss:  0.001 accuracy: 0.982 test_loss:  0.0 test_accuracy: 0.985
    # epoch:  3 loss:  0.001 accuracy: 0.986 test_loss:  0.0 test_accuracy: 0.987
    # epoch:  4 loss:  0.001 accuracy: 0.988 test_loss:  0.0 test_accuracy: 0.987
    # epoch:  5 loss:  0.0 accuracy: 0.99 test_loss:  0.0 test_accuracy: 0.989
    # epoch:  6 loss:  0.0 accuracy: 0.992 test_loss:  0.0 test_accuracy: 0.99
    # epoch:  7 loss:  0.0 accuracy: 0.993 test_loss:  0.0 test_accuracy: 0.99
    # epoch:  8 loss:  0.0 accuracy: 0.994 test_loss:  0.0 test_accuracy: 0.987
    # epoch:  9 loss:  0.0 accuracy: 0.995 test_loss:  0.0 test_accuracy: 0.989
    # epoch:  10 loss:  0.0 accuracy: 0.996 test_loss:  0.0 test_accuracy: 0.991
    # epoch:  11 loss:  0.0 accuracy: 0.996 test_loss:  0.0 test_accuracy: 0.988
    # epoch:  12 loss:  0.0 accuracy: 0.997 test_loss:  0.0 test_accuracy: 0.988
    # epoch:  13 loss:  0.0 accuracy: 0.997 test_loss:  0.0 test_accuracy: 0.99
    # epoch:  14 loss:  0.0 accuracy: 0.997 test_loss:  0.0 test_accuracy: 0.989
    # epoch:  15 loss:  0.0 accuracy: 0.997 test_loss:  0.0 test_accuracy: 0.988
    # epoch:  16 loss:  0.0 accuracy: 0.998 test_loss:  0.0 test_accuracy: 0.991
    # epoch:  17 loss:  0.0 accuracy: 0.997 test_loss:  0.0 test_accuracy: 0.989
    # epoch:  18 loss:  0.0 accuracy: 0.998 test_loss:  0.0 test_accuracy: 0.99
    # epoch:  19 loss:  0.0 accuracy: 0.998 test_loss:  0.0 test_accuracy: 0.988
    
    plt.plot(range(1, epochs+1), train_loss, label='train_loss')
    plt.plot(range(1, epochs+1), test_loss, label='test_loss')
    plt.legend()
    
    plt.plot(range(1, epochs+1), train_acc, label='train_acc')
    plt.plot(range(1, epochs+1), test_acc, label='test_acc')
    plt.legend()
    
    
    

    相关文章

      网友评论

          本文标题:PyTrch深度学习简明实战35 - Tensorboard可视

          本文链接:https://www.haomeiwen.com/subject/jbfsvdtx.html