The import numpy
The import torch
The import torch. The nn as nn
The import torch. Optim as optim
The import matplotlib. Pyplot as PLT
The import torchvision
The import torchvision. Transforms as transforms
The import time
Time_open=time. Time ()
Transform=transforms.Com pose ([transforms ToTensor (), transforms the Normalize (mean=[0.5], STD=[0.5]])
# train_data=https://bbs.csdn.net/topics/torchvision.datasets.MNIST (root='./data, download=True, transform=transforms. ToTensor (), "train"=True)
# this function directly transforms. ToTensor () pixel values between 0.5-1 is not fall color is gray to white
# test_data=https://bbs.csdn.net/topics/torchvision.datasets.MNIST (root='./data, download=True, transform=transforms. ToTensor (), "train"=True)
Train_data=https://bbs.csdn.net/topics/torchvision.datasets.MNIST (root='./data, download=True, the transform=transform, "train"=True)
Test_data=https://bbs.csdn.net/topics/torchvision.datasets.MNIST (root='./data, download=True, the transform=transform, "train"=True)
The transform function called # adjust pixel values to fall between 0-1 black is 000 to 1 grey 0.5
Train_data_sets=torch. Utils. Data. DataLoader (dataset=train_data, shuffle=True, batch_size=64)
Test_data_sets=torch. Utils. Data. DataLoader (dataset=test_data, shuffle=True, batch_size=64)
Images, label=next (iter (train_data_sets))
Img=torchvision. Utils. Make_grid (images)
Img=img. Numpy (.) transpose (1, 0)
Img=img/2 + 0.5
Print ([label [I] the item (s) for I in range (64)])
PLT. Imshow (img)
PLT. The show ()
Class.net (nn Module) :
Def __init__ (self) :
The self, super (Net) __init__ ()
The self. The conv1=nn. Sequential (
Nn. Conv2d (1 (kernel_size=3, stride=1, padding=1),
Nn. ReLU (),
Nn. Conv2d (64128, kernel_size=3, stride=1, padding=1),
Nn. ReLU (),
Nn. MaxPool2d (kernel_size=2, stride=2))
The self. The dense=nn. Sequential (
Nn. Linear (14 * 14 * 128102 4),
Nn. ReLU (),
Nn. Dropout (p=0.5),
# nn. Linear (1024512),
# nn. ReLU (),
Nn. Linear (1024, 10))
Def forward (self, x) :
X=self. Conv1 (x)
X=x.v iew (1, 14 * 14 * 128)
X=self. DE