• matplotlib.pyplot展示MNIST图片


    import torch
    import torch.utils.data as Data
    import torchvision
    import torchvision.transforms as transforms
    import matplotlib.pyplot as plt
    import numpy as np
     
    #torch.manual_seed(1)    # reproducible
     
    # Hyper Parameters
    EPOCH = 1               # train the training data n times, to save time, we just train 1 epoch
    batch_size = 4
    LR = 0.001              # learning rate
    DOWNLOAD_MNIST = True   # set to False if you have downloaded
     
    classes = ['0','1','2','3','4','5','6','7','8','9']
    
    
    # Mnist digits dataset
    train_dataset = torchvision.datasets.MNIST(
        root='../../data/',
        train=True,                                     # this is training data
        transform=torchvision.transforms.ToTensor(),    # Converts a PIL.Image or numpy.ndarray to
                                                        # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
        download=DOWNLOAD_MNIST,                        # download it if you don't have it
    )
    
    # Data loader
    train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                               batch_size=batch_size, 
                                               shuffle=True)
    
    print(type(train_dataset))
    def imshow(img):
        img = img / 2 + 0.5
        npimg = img.numpy()
        plt.imshow(np.transpose(npimg,(1,2,0)))
        plt.show()
    
    dataiter = iter(train_loader)
    images,labels = dataiter.next()
    print(images.shape)#[50,1,28,28]
    
    imshow(torchvision.utils.make_grid(images))
    print(''.join('%5s' % classes[labels[j]] for j in range(4)))
    

      

    -------------------------------------------------------------逆水行舟,不进则退。
  • 相关阅读:
    Win7旗舰版中的IIS配置asp.net的运行环境
    jquery $(document).ready() 与window.onload的区别
    DEFAULT CURRENT_TIMESTAMP
    存储过程 跳出
    rabbitMQ 重试
    oracle update left join 写法
    error: snap "eclipse" has "install-snap" change in progress
    数据库去空格 去table 去回车符号 去重
    分组 拼接字段
    msyql 去重
  • 原文地址:https://www.cnblogs.com/alilliam/p/10256927.html
Copyright © 2020-2023  润新知