paddle paddle手写数字识别

import paddle
from paddle.nn import Linear
import paddle.nn.functional as F
import os
import numpy as np
import matplotlib.pyplot as plt
from paddle.vision.datasets import MNIST

train_dataset = paddle.vision.datasets.MNIST(mode='train')
train_data0 = np.array(train_dataset[0][0])
train_label_0 = np.array(train_dataset[0][1])

plt.figure("Image")
plt.figure(figsize=(2, 2))
plt.imshow(train_data0, cmap=plt.cm.binary)
plt.axis('on')
plt.title('image')
plt.show()

print("图像数据形状和对应数据为:", train_data0.shape)
print("图像标签形状和对应数据为:", train_label_0.shape, train_label_0)
print("\n打印第一个batch的第一个图像,对应标签数字为{}".format(train_label_0))


class MNIST(paddle.nn.Layer):
    def __init__(self):
        super(MNIST, self).__init__()

        self.fc = paddle.nn.Linear(in_features=784,out_features=1)

    def forward(self, inputs):
        outputs = self.fc(inputs)
        return outputs

model = MNIST()

def train(model):
    model.train()
    train_loader = paddle.io.DataLoader(paddle.vision.datasets.MNIST(mode='train'),
                                        batch_size=16,
                                        shuffle=True)
    opt = paddle.optimizer.SGD(learning_rate=0.001, parameters=model.parameters())

def norm_img(img):
    assert len(img.shape) == 3
    batch_size, img_h, img_w = img.shape[0], img.shape[1],img.shape[2]
    img = img / 255
    img = paddle.reshape(img, [batch_size, img_h*img_w])
    return img

paddle.vision.set_image_backend('cv2')

model = MNIST()

def train(model):
    model.train()
    train_loader = paddle.io.DataLoader(paddle.vision.datasets.MNIST(mode='train'),
                                        batch_size=16,
                                        shuffle=True)
    opt = paddle.optimizer.SGD(learning_rate=0.001, parameters=model.parameters())
    EPOCH_NUM = 10
    for epoch in range(EPOCH_NUM):
        for batch_id,data in enumerate(train_loader()):
            images = norm_img(data[0]).astype('float32')
            labels = data[1].astype('float32')

            predicts = model(images)

            loss = F.square_error_cost(predicts, labels)
            avg_loss = paddle.mean(loss)

            if batch_id % 1000 == 0:
                print("epoch_id: {}, batch_id: {}, loss is: {}".format(epoch, batch_id, avg_loss.numpy()))

            avg_loss.backward()
            opt.step()
            opt.clear_grad()
train(model)
paddle.save(model.state_dict(), './mnist.pdparams')

运行结果

D:\python\python.exe "D:/py project/pythonPaddlepaddle/Handwritten numeral.py"
D:\python\lib\site-packages\pkg_resources\_vendor\pyparsing.py:943: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  collections.MutableMapping.register(ParseResults)
D:\python\lib\site-packages\setuptools\depends.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
图像数据形状和对应数据为: (28, 28)
图像标签形状和对应数据为: (1,) [5]

打印第一个batch的第一个图像,对应标签数字为[5]
epoch_id: 0, batch_id: 0, loss is: [27.945545]
epoch_id: 0, batch_id: 1000, loss is: [6.517153]
epoch_id: 0, batch_id: 2000, loss is: [2.6315107]
epoch_id: 0, batch_id: 3000, loss is: [2.7354944]
epoch_id: 1, batch_id: 0, loss is: [2.4119804]
epoch_id: 1, batch_id: 1000, loss is: [4.966941]
epoch_id: 1, batch_id: 2000, loss is: [2.2226493]
epoch_id: 1, batch_id: 3000, loss is: [3.542047]
epoch_id: 2, batch_id: 0, loss is: [5.202344]
epoch_id: 2, batch_id: 1000, loss is: [5.424559]
epoch_id: 2, batch_id: 2000, loss is: [3.8883042]
epoch_id: 2, batch_id: 3000, loss is: [3.9588048]
epoch_id: 3, batch_id: 0, loss is: [3.1250522]
epoch_id: 3, batch_id: 1000, loss is: [4.124708]
epoch_id: 3, batch_id: 2000, loss is: [2.9321508]
epoch_id: 3, batch_id: 3000, loss is: [3.0870695]
epoch_id: 4, batch_id: 0, loss is: [5.5718822]
epoch_id: 4, batch_id: 1000, loss is: [2.5728583]
epoch_id: 4, batch_id: 2000, loss is: [4.3552656]
epoch_id: 4, batch_id: 3000, loss is: [3.4780154]
epoch_id: 5, batch_id: 0, loss is: [2.984262]
epoch_id: 5, batch_id: 1000, loss is: [3.5649102]
epoch_id: 5, batch_id: 2000, loss is: [4.7638226]
epoch_id: 5, batch_id: 3000, loss is: [1.1115279]
epoch_id: 6, batch_id: 0, loss is: [4.5162864]
epoch_id: 6, batch_id: 1000, loss is: [1.3654096]
epoch_id: 6, batch_id: 2000, loss is: [6.1951294]
epoch_id: 6, batch_id: 3000, loss is: [2.7592826]
epoch_id: 7, batch_id: 0, loss is: [1.3672054]
epoch_id: 7, batch_id: 1000, loss is: [3.382936]
epoch_id: 7, batch_id: 2000, loss is: [2.0015998]
epoch_id: 7, batch_id: 3000, loss is: [1.6311438]
epoch_id: 8, batch_id: 0, loss is: [4.1808968]
epoch_id: 8, batch_id: 1000, loss is: [3.188591]
epoch_id: 8, batch_id: 2000, loss is: [4.150716]
epoch_id: 8, batch_id: 3000, loss is: [2.359276]
epoch_id: 9, batch_id: 0, loss is: [3.0870986]
epoch_id: 9, batch_id: 1000, loss is: [1.9162016]
epoch_id: 9, batch_id: 2000, loss is: [2.2735453]
epoch_id: 9, batch_id: 3000, loss is: [3.6740565]

Process finished with exit code 0
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值