利用主成分分析法和多层感知机(MLP)实现MNIST数据集分类

利用主成分分析法和多层感知机(MLP)实现MNIST数据集分类

# -*- coding: utf-8 -*-
"""
Created on Wed Jan  5 09:17:06 2022

@author: wzy
"""
import numpy as np
from sklearn.decomposition import PCA
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets,transforms
from torch.utils.data import Dataset

#MNIST 数据集
#设置训练的批次大小、学习率、及训练代数
batch_size=200
learning_rate=0.001
epochs=20

# 设置主成分分析法保留特征维数
n_features = 10

#下载数据集
class MyDataset(Dataset):
    def __init__(self, datas,labels):
        self.datas = datas
        self.labels = labels

    def __getitem__(self, index):
        img = self.datas[index]
        target = self.labels[index]
        return img, target

    def __len__(self):
        return len(self.datas)

# 下载Mnist数据集
train_dataset = datasets.MNIST('./data', train=True, download=True,
                   transform=transforms.Compose([
                       transforms.ToTensor(),
                       transforms.Normalize((0.1,), (0.1,))
                   ]))
test_dataset = datasets.MNIST('./data', train=False, download=True, transform=transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.1,), (0.1,))
    ]))

# 对MNIST数据集进行主成分分析
def pca_mnist(train_dataset,test_dataset,n_features):
    dataset_train = np.array([data[0].flatten().numpy() for data in train_dataset])
    dataset_test = np.array([data[0].flatten().numpy() for data in test_dataset]) 
    pca = PCA(n_components=n_features)
    dataset_all = np.concatenate((dataset_train,dataset_test),axis=0)
    pca.fit(dataset_all)
    data_all = pca.transform(dataset_all)
    data_all = torch.from_numpy(data_all)
    train_datas = data_all[:len(dataset_train)]
    test_datas = data_all[len(dataset_train):]

    train_labels = np.array([data[1] for data in train_dataset])
    train_labels = torch.tensor(train_labels) 
    test_labels = np.array([data[1] for data in test_dataset])
    test_labels = torch.tensor(test_labels) 
    return train_datas,train_labels,test_datas,test_labels

train_datas,train_labels,test_datas,test_labels = pca_mnist(train_dataset,test_dataset,n_features)

train_dataset = MyDataset(train_datas,train_labels)
test_dataset = MyDataset(test_datas,test_labels)

train_loader = torch.utils.data.DataLoader(
    train_dataset,
    batch_size=batch_size, shuffle=True)

test_loader = torch.utils.data.DataLoader(
    test_dataset,
    batch_size=batch_size, shuffle=False)

# 定义多层感知机模型
class MLP(nn.Module):
    def __init__(self,n_features):
        super(MLP,self).__init__()
        self.flatten = nn.Flatten()
        self.linear1 = nn.Linear(n_features,200)
        self.linear2 = nn.Linear(200,100)
        self.linear3 = nn.Linear(100,10)
    def forward(self,x):
        x = self.flatten(x)
        x = self.linear1(x)
        x = F.relu(x)
        x = self.linear2(x)
        x = F.relu(x)
        x = self.linear3(x)
        return x


net = MLP(n_features)

#定义优化器,采用SGD随机梯度下降的方式对w1, b1, w2, b2, w3, b3进行优化
optimizer = optim.SGD(net.parameters(), lr=learning_rate)
#定义采用交叉熵作为损失函数
loss_fn = nn.CrossEntropyLoss()

# 开始训练
for epoch in range(epochs):

    for batch_idx, (data, target) in enumerate(train_loader):
        # 将数据输入到网络中
        cal_data = net(data)
        # 将计算的数据与目标数据求误差损失
        loss = loss_fn(cal_data, target.long())

        # 将梯度值初始化为0
        optimizer.zero_grad()
        # pytorch计算梯度值
        loss.backward()
        # 更新梯度值
        optimizer.step()
        # 每隔25*batcsize(200) = 5000 打印输出结果
        if batch_idx % 25 == 0:
            print('训练代数: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch+1, batch_idx * len(data), len(train_loader.dataset),
                       100. * batch_idx / len(train_loader), loss.item()))

    # 将测试误差及正确率清0
    test_loss = 0
    correct = 0
    # 取测试集数据及目标数据
    for data, target in test_loader:
        logits = net(data)
        # 误差累加
        test_loss += loss_fn(logits, target.long()).item()
        # 取出预测最大值的索引编号,即预测值
        pred = logits.data.argmax(dim=1)
        # 统计正确预测的个数
        correct += pred.eq(target.data).sum()

    test_loss /= len(test_loader.dataset)
    # 打印输出测试误差及准确率
    print('\n测试集: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))

最终运行结果如下:
利用主成分分析法和多层感知机(MLP)实现MNIST数据集分类_第1张图片

你可能感兴趣的:(课程学习,机器学习,深度学习,神经网络,数据挖掘)