import torch
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.optim as optim
import torchvision
import matplotlib.pyplot as plt
# prepare dataset
batch_size = 64
transform = torchvision.transforms.Compose([transforms.ToTensor()]) # 归一化,均值和方差
train_dataset = datasets.MNIST(root='./datasets', train=True, download=True, transform=transform)
#print(train_dataset[0])
train_loader = DataLoader(train_dataset, shuffle=False, batch_size=batch_size)
test_dataset = datasets.MNIST(root='./datasets', train=False, download=True, transform=transform)
test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size)
# design model using class
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = torch.nn.Conv2d(1,5,kernel_size=3,padding=1)#5 24*24
self.conv2 = torch.nn.Conv2d(5,10,kernel_size=3)#10 20*20
self.conv3 = torch.nn.Conv2d(10,20,kernel_size=5)
self.conv4 = torch.nn.Conv2d(20,20,kernel_size=3)
self.pooling1= torch.nn.MaxPool2d(kernel_size=2,stride=1)
self.pooling2 = torch.nn.MaxPool2d(kernel_size=2, stride=1)
self.pooling3 = torch.nn.MaxPool2d(kernel_size=2, stride=2)
self.pooling4 = torch.nn.MaxPool2d(kernel_size=2, stride=2)
self.l1 = torch.nn.Linear(320,110)
'''self.l2 = torch.nn.Linear(160,10)
self.l3 = torch.nn.Linear(80,40)
self.l4 = torch.nn.Linear(40,10)'''
def forward(self, x):
batch_size = x.size(0)#1 28*28
x = self.pooling1(F.relu(self.conv1(x)))#1 30*30 => 5 28*28 => 5 27*27
x = self.pooling2(F.relu(self.conv2(x)))#5 27*27 => 10 25*25 => 10 24*4
x = self.pooling3(F.relu(self.conv3(x)))#10 24*24 => 20 20*20 => 20 10*10
x = self.pooling4(F.relu(self.conv4(x)))#20 10*10 => 20 8*8 => 20 4*4
#print(x.shape)#64 20 4 4
x = x.view(batch_size,-1)#320
#print(x.shape)
x = self.l1(x)
'''x = F.relu(self.l2(x))
x = F.relu(self.l3(x))'''
#print(x.shape) 64 10
#input()
return x
model = Net()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
# construct loss and optimizer
criterion = torch.nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)
epoch_list =[]
loss_list = []
# training cycle forward, backward, update
def train():
running_loss = 0.0
for batch_idx, data in enumerate(train_loader, 1):#data[0]=64 #tain_loader = 938
# Prepare
# print(data[0])
#print(data[0].shape)
#print(data[0])
#print(data[1].shape)
#print(train_loader.__len__())
x, y = data
x, y = x.to(device), y.to(device)
#print(data[0].shape,y[1])
#input()
# Forward
#print(x)
y_pred = model(x)
'''if batch_idx ==900:
print(f'predit = {y_pred[0]}')
print(y)
input()'''
loss = criterion(y_pred, y)
#print(y)
#input()
running_loss += loss.item()
#print(running_loss)
# Backward
optimizer.zero_grad()
loss.backward()
# Update
optimizer.step()
if batch_idx % 300 == 299:
print(f'epoch {epoch + 1} loss = {running_loss/300}' )
if batch_idx == 899:
loss_list.append(running_loss / 300)
running_loss = 0.0
def test():
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
images, labels = data
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, dim=1) # dim = 1 列是第0个维度,行是第1个维度
#print(predicted)
total += labels.size(0)
correct += (predicted == labels).sum().item() # 张量之间的比较运算
print(f"Accuracy on test set:{100 * correct / total}%")
if __name__ == '__main__':
for epoch in range(100):
epoch_list.append(epoch)
train()
test()
plt.plot(epoch_list,loss_list)
plt.xlabel('epoch')
plt.ylabel('loss')
plt.show()
|