使用的数据集:MNIST
import torch
import numpy as np
import pandas as pd
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from torchvision.datasets import MNIST
from torch.utils.data import random_split
from torch.utils.data import TensorDataset
import torchvision.transforms as transforms
# 定义CNN模型,如果看不懂,需要先学习CNN过程
class Net(nn.Module):
def __init__(self):
super(Net,self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(
in_channels=1,
out_channels=16,
kernel_size=5,
stride=1,
padding=2,
),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
self.conv2 = nn.Sequential(
nn.Conv2d(16,32,5,1,2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
self.out = nn.Linear(32 * 7 * 7,10)
def forward(self,x):
x = self.conv1(x)
x = self.conv2(x