基于多层感知机的手写数字识别(Tensorflow实现)

import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os

mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

class MNISTModel(object):
    def __init__(self, lr, batch_size, iter_num):
        self.lr = lr
        self.batch_size = batch_size
        self.iter_num = iter_num
        # 定义模型结构
        # 输入张量,这里还没有数据,先占个地方,所以叫“placeholder”
        self.x = tf.placeholder(tf.float32, [None, 784])   # 图像是28*28的大小
        self.y = tf.placeholder(tf.float32, [None, 10])    # 输出是0-9的one-hot向量
        self.h = tf.layers.dense(self.x, 100, activation=tf.nn.relu, use_bias=True, kernel_initializer=tf.truncated_normal_initializer) # 一个全连接层
        self.y_ = tf.layers.dense(self.h, 10, use_bias=True, kernel_initializer=tf.truncated_normal_initializer) # 全连接层
        
        # 使用交叉熵损失函数
        self.loss = tf.losses.softmax_cross_entropy(self.y, self.y_)
        self.optimizer = tf.train.AdamOptimizer()
        self.train_step = self.optimizer.minimize(self.loss)
        
        # 用于模型训练
        self.correct_prediction = tf.equal(tf.argmax(self.y, axis=1), tf.argmax(self.y_, axis=1))
        self.accuracy = tf.reduce_mean(tf.cast(self.correct_prediction, tf.float32))
        
        # 用于保存训练好的模型
        self.saver = tf.train.Saver()
        
    def train(self):
        with tf.Session() as sess:            #  打开一个会话。可以想象成浏览器打开一个标签页一样,直观地理解一下
            sess.run(tf.global_variables_initializer())  # 先初始化所有变量。
            for i in range(self.iter_num):
                batch_x, batch_y = mnist.train.next_batch(self.batch_size)   # 读取一批数据
                loss, _ = sess.run([self.loss, self.train_step], feed_dict={self.x: batch_x, self.y: batch_y})   # 每调用一次sess.run,就像拧开水管一样,所有self.loss和self.train_step涉及到的运算都会被调用一次。
                if i%1000 == 0:    
                    train_accuracy = sess.run(self.accuracy, feed_dict={self.x: batch_x, self.y: batch_y})  # 把训练集数据装填进去
                    test_x, test_y = mnist.test.next_batch(self.batch_size)
                    test_accuracy = sess.run(self.accuracy, feed_dict={self.x: test_x, self.y: test_y})   # 把测试集数据装填进去
                    print( 'iter\t%i\tloss\t%f\ttrain_accuracy\t%f\ttest_accuracy\t%f' % (i,loss,train_accuracy,test_accuracy))
            self.saver.save(sess, 'model/mnistModel') # 保存模型

    def test(self):
        with tf.Session() as sess:
            self.saver.restore(sess, 'model/mnistModel')
            Accuracy = []
            for i in range(150):
                test_x, test_y = mnist.test.next_batch(self.batch_size)
                test_accuracy = sess.run(self.accuracy, feed_dict={self.x: test_x, self.y: test_y})
                Accuracy.append(test_accuracy)
            print ('==' * 15)
            print ('Test Accuracy: ', np.mean(np.array(Accuracy)))

model = MNISTModel(0.001, 64, 40000)   # 学习率为0.001,每批传入64张图,训练40000次
model.train()      # 训练模型
model.test()       #测试模型

转载于:https://www.cnblogs.com/shayue/p/10386107.html

好的,下面是基于 Python 和 PyTorch 的多层感知机实现手写数字识别的代码示例。 首先,我们需要导入必要的库和数据集。这里我们使用 PyTorch 自带的 MNIST 数据集。 ```python import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader from torchvision import datasets, transforms # 定义数据集路径 data_path = './data' # 定义数据预处理方式 transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=(0.5,), std=(0.5,)) ]) # 加载数据集 train_dataset = datasets.MNIST(root=data_path, train=True, download=True, transform=transform) test_dataset = datasets.MNIST(root=data_path, train=False, download=True, transform=transform) # 定义批次大小 batch_size = 128 # 创建数据加载器 train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True) test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=True) ``` 接着,我们定义多层感知机模型,这里使用两层全连接层。 ```python class MLP(nn.Module): def __init__(self, input_dim, hidden_dim, output_dim): super(MLP, self).__init__() self.fc1 = nn.Linear(input_dim, hidden_dim) self.fc2 = nn.Linear(hidden_dim, output_dim) self.relu = nn.ReLU() def forward(self, x): x = x.view(x.size(0), -1) x = self.fc1(x) x = self.relu(x) x = self.fc2(x) return x ``` 然后,我们定义模型的超参数,并实例化模型和优化器。 ```python # 定义模型的超参数 input_dim = 784 hidden_dim = 128 output_dim = 10 learning_rate = 0.001 num_epochs = 10 # 实例化模型和优化器 model = MLP(input_dim, hidden_dim, output_dim) optimizer = optim.Adam(model.parameters(), lr=learning_rate) ``` 接下来,我们定义训练和测试的函数。 ```python def train(model, train_loader, optimizer, criterion): model.train() train_loss = 0 train_correct = 0 for batch_idx, (data, target) in enumerate(train_loader): optimizer.zero_grad() output = model(data) loss = criterion(output, target) train_loss += loss.item() pred = output.argmax(dim=1, keepdim=True) train_correct += pred.eq(target.view_as(pred)).sum().item() loss.backward() optimizer.step() train_loss /= len(train_loader.dataset) train_acc = train_correct / len(train_loader.dataset) return train_loss, train_acc def test(model, test_loader, criterion): model.eval() test_loss = 0 test_correct = 0 with torch.no_grad(): for data, target in test_loader: output = model(data) test_loss += criterion(output, target).item() pred = output.argmax(dim=1, keepdim=True) test_correct += pred.eq(target.view_as(pred)).sum().item() test_loss /= len(test_loader.dataset) test_acc = test_correct / len(test_loader.dataset) return test_loss, test_acc ``` 最后,我们进行训练并测试模型。 ```python criterion = nn.CrossEntropyLoss() for epoch in range(1, num_epochs+1): train_loss, train_acc = train(model, train_loader, optimizer, criterion) test_loss, test_acc = test(model, test_loader, criterion) print('Epoch [{}/{}], Train Loss: {:.4f}, Train Acc: {:.4f}, Test Loss: {:.4f}, Test Acc: {:.4f}' .format(epoch, num_epochs, train_loss, train_acc, test_loss, test_acc)) ``` 训练完成后,我们可以使用模型进行预测。 ```python import matplotlib.pyplot as plt import numpy as np # 随机选择一张测试图片 index = np.random.randint(0, len(test_dataset)) image, target = test_dataset[index] image = image.unsqueeze(0) # 使用模型进行预测 output = model(image) pred = output.argmax(dim=1, keepdim=True) # 显示图片和预测结果 plt.imshow(image.squeeze(), cmap='gray') plt.title('Ground Truth: {}, Prediction: {}'.format(target, pred.item())) plt.axis('off') plt.show() ``` 以上就是基于 Python 和 PyTorch 的多层感知机实现手写数字识别的代码示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值