mnist手写数字识别

2021/9/15 23:07:06

本文主要是介绍mnist手写数字识别,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

# -*- coding: utf-8 -*-
"""
Created on Mon May 27 15:07:23 2019
@author: AugustMe
"""
import numpy as np
import os
import gzip
import pylab
from matplotlib import pyplot
import tensorflow as tf
from tensorflow.keras import layers
import os
from sklearn import preprocessing #用于标准化
#开启gpu
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
# 定义加载数据的函数,data_folder为保存gz数据的文件夹,该文件夹下有4个文件
# 'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
# 't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'

def load_data(data_folder):

  files = [
      'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
      't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
  ]

# rombuffer将data以流的形式读入转化成ndarray对象
# numpy.frombuffer(buffer, dtype=float, count=-1, offset=0)
# buffer:缓冲区,它表示暴露缓冲区接口的对象。
# dtype:代表返回的数据类型数组的数据类型。默认值为0。
# count:代表返回的ndarray的长度。默认值为-1。
# offset:偏移量,代表读取的起始位置。默认值为0。



  paths = []
  for fname in files:
    paths.append(os.path.join(data_folder,fname))

  with gzip.open(paths[0], 'rb') as lbpath:
    y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)

  with gzip.open(paths[1], 'rb') as imgpath:
    x_train = np.frombuffer(
        imgpath.read(), np.uint8, offset=16).reshape(len(y_train),784)

  with gzip.open(paths[2], 'rb') as lbpath:
    y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)

  with gzip.open(paths[3], 'rb') as imgpath:
    x_test = np.frombuffer(
        imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 784)

  return (x_train, y_train), (x_test, y_test)

(train_images, train_labels), (test_images, test_labels) = load_data('MNIST/')


# pyplot.imshow(train_images[1].reshape((28,28)),cmap="gray")
# pylab.show()
# print(train_images.shape)
# print(train_labels[1])

train_images, test_images = train_images / 255, test_images / 255
# model=tf.keras.Sequential()
# model.add(layers.Dense(32,activation='relu'))
# model.add(layers.Dense(32,activation='relu'))
# model.add(layers.Dense(10,activation='softmax'))
model = tf.keras.Sequential()
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(10,activation='softmax'))


#设定模型的学习率和损失函数,Metrics标注网络评价指标
# "accuracy" : y_ 和 y 都是数值,如y_ = [1] y = [1]  #y_为真实值,y为预测值
# “sparse_accuracy":y_和y都是以独热码 和概率分布表示,如y_ = [0, 1, 0], y = [0.256, 0.695, 0.048]
# "sparse_categorical_accuracy" :y_是以数值形式给出,y是以 独热码给出,如y_ = [1], y = [0.256 0.695, 0.048]

# categorical_crossentropy 和 sparse_categorical_crossentropy 都是交叉熵损失函数,使用哪种函数要根据标签的结构来选择
#
# 如果样本标签是one-hot编码,则用 categorical_crossentropy函数
#   one-hot 编码:[0, 0, 1], [1, 0, 0], [0, 1, 0]
# 如果样本标签是数字编码 ,则用sparse_categorical_crossentropy函数
#   数字编码:2, 0, 1

model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(),
              metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])


#设定模型的输入值,x和y,训练的样本,轮数,
#validation_data用来在每个epoch之后,或者每几个epoch,验证一次验证集,用来及早发现问题,比如过拟合,或者超参数设置有问题。
model.fit(train_images,train_labels,epochs=10,batch_size=64)


这篇关于mnist手写数字识别的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程