Pytorch学习笔记——mnist数据集训练及测试

Pytorch学习笔记

本次需要下载mnist数据集,四个文件都需要下载。
在这里插入图片描述

下载后,使用gunzip + 文件名的方式解压,然后在python文件的路径下新建一个‘data’文件夹,把这四个文件放进去。

话不多说,直接放代码:

"""
Trains a simple convnet on the MNIST dataset.
"""

from __future__ import print_function
import os
import struct
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable

def load_mnist(path,kind='train'):
    """Load MNIST data from path"""
    labels_path = os.path.join(path,'%s-labels-idx1-ubyte' %kind)
    images_path = os.path.join(path,'%s-images-idx3-ubyte' %kind)
    with open(labels_path,'rb') as lbpath:
        magic, n = struct.unpack('>II',lbpath.read(8))
        labels = np.fromfile(lbpath,dtype=np.uint8)
    with open(images_path,'rb') as imgpath:
        magic, num, rows, cols = struct.unpack(">IIII",imgpath.read(16))
        images = np.fromfile(imgpath,dtype=np.uint8).reshape(len(labels),784)
    return images,labels

X_train,Y_train = load_mnist('./data',kind='train')
print("shape: ",X_train.shape)
print("Rows: %d,columns: %d" %(X_train.shape[0],X_train.shape[1]))
X_test,Y_test = load_mnist('./data',kind='t10k')
print('Rows: %d,columns: %d' %(X_test.shape[0],X_test.shape[1]))

batch_size = 100
num_classes = 10
epochs = 5

#input image dimensions
img_rows, img_cols = 28,28
x_train = X_train
x_test = X_test

if 'channels_first' == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0],1,img_rows,img_cols)
    x_test = x_test.reshape(x_test.shape[0],1,img_rows,img_cols)
    input_shape = (1,img_rows,img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0],img_rows,img_cols,1)
    x_test = x_test.reshape(x_test.shape[0],img_rows,img_cols,1)
    input_shape = (img_rows,img_cols,1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape: ',x_train.shape)
print(x_train.shape[0],'train samples')
print(x_test.shape[0],'test samples')
num_samples = x_train.shape[0]
print("num_samples: ",num_samples)

"""
build torch model
"""
class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.conv1 = nn.Conv2d(1,32,kernel_size=5)
        self.conv2 = nn.Conv2d(32,64,kernel_size=5)
        self.conv2_drop = nn.Dropout2d(p=0.2)
        self.fc1 = nn.Linear(1024,50)
        self.fc2 = nn.Linear(50,10)

    def forward(self,x):
        x = F.relu(F.max_pool2d(self.conv1(x),2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)),2))
        x = x.view(-1,1024)
        x = F.relu(self.fc1(x))
        x = F.dropout(x,training=self.training)
        x = self.fc2(x)
        return F.log_softmax(x,dim=1)

model = Net()
if os.path.exists('mnist_torch.pkl'):
    model = torch.load('mnist_torch.pkl')

print(model)

"""
training
"""
optimizer = optim.SGD(model.parameters(),lr=0.01,momentum=0.5)
#loss = torch.nn.CrossEntropyLoss(size_average=True)
def train(epoch,x_train,y_train):
    num_batchs = int(num_samples/batch_size)
    model.train()
    for k in range(num_batchs):
        start,end = k*batch_size,(k+1)*batch_size
        data,target = Variable(x_train[start:end],requires_grad=False),Variable(y_train[start:end])
        optimizer.zero_grad()
        output = model(data)
        loss = F.nll_loss(output,target)
        loss.backward()
        optimizer.step()
        if k%10 == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(epoch,k*len(data),num_samples,100.*k/num_samples,loss.item()))
    torch.save(model,'mbist_torch.pkl')

"""
evaluate
"""
def test(epoch):
    model.eval()
    test_loss = 0
    correct = 0
    if 2>1:
        with torch.no_grad():
            data,target = Variable(x_test),Variable(y_test)
        #data,target = Variable(x_test,volatile=True),Variable(y_test)
        output = model(data)
        test_loss += F.nll_loss(output,target).item()
        pred = output.data.max(1)[1]
        correct += pred.eq(target.data).cpu().sum()

    test_loss = test_loss
    test_loss /= len(x_test)
    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(test_loss,correct,len(x_test),100.*correct/len(x_test)))

x_train = torch.from_numpy(x_train).float()
x_test = torch.from_numpy(x_test).float()
y_train = torch.from_numpy(Y_train).long()
y_test = torch.from_numpy(Y_test).long()
for epoch in range(1,epochs):
    train(epoch,x_train,y_train)
    test(epoch)

通过修改epochs参数,可以修改迭代的次数,以提高准确率;
修改网络层参数,可以更改单次迭代效率。

运行结果:

  • epoch1:
    在这里插入图片描述

  • epoch2:
    在这里插入图片描述

  • epoch3:
    在这里插入图片描述

  • epoch4:
    在这里插入图片描述
    可以看出,随着迭代次数的增加,准确率越来越高。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是对 MNIST 数据集进行深度神经网络搭建、标准化和归一化、模型搭建、训练、评估、调参优化的代码: ```python import tensorflow as tf from sklearn.preprocessing import StandardScaler, MinMaxScaler import pandas as pd import numpy as np from sklearn.model_selection import train_test_split # 加载数据集 train_data = pd.read_csv('mnist_dataset/mnist_train.csv') test_data = pd.read_csv('mnist_dataset/mnist_test.csv') # 数据预处理 X_train = train_data.drop('label', axis=1).values y_train = train_data['label'].values X_test = test_data.drop('label', axis=1).values y_test = test_data['label'].values # 标准化和归一化 scaler = StandardScaler() X_train = scaler.fit_transform(X_train.astype(np.float32)) X_test = scaler.transform(X_test.astype(np.float32)) min_max_scaler = MinMaxScaler() X_train = min_max_scaler.fit_transform(X_train) X_test = min_max_scaler.transform(X_test) # 划分训练集和验证集 X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42) # 构建模型 model = tf.keras.models.Sequential([ tf.keras.layers.Dense(128, activation='relu', input_shape=(784,)), tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(32, activation='relu'), tf.keras.layers.Dense(10, activation='softmax') ]) # 编译模型 model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # 训练模型 history = model.fit(X_train, y_train, epochs=20, validation_data=(X_val, y_val)) # 评估模型 test_loss, test_acc = model.evaluate(X_test, y_test) print("Test Loss:", test_loss) print("Test Accuracy:", test_acc) ``` 以上代码中,我们首先加载了 MNIST 数据集,然后进行了标准化和归一化的处理。接着,我们将数据集分为训练集、验证集和测试集。然后,我们构建了包含 4 个全连接层的深度神经网络,并使用了 Adam 优化器和交叉熵损失函数。最后,我们训练了模型,并评估了模型在测试集上的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值