ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

mnist手写数字识别

2021-09-15 22:33:26  阅读:143  来源: 互联网

标签:gz test train images import 手写 识别 model 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)

标签:gz,test,train,images,import,手写,识别,model,mnist
来源: https://blog.csdn.net/weixin_46664413/article/details/120318533

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有