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)

# 使用模型
img = test_images[1]
print(img.shape)

# tf.keras模型经过了优化,可同时对一个批或一组样本进行预测。因此,即便只使用一个图像,也需要将其添加到列表中。
img = (np.expand_dims(img, 0))
print(img.shape)


# 预测图像的正确标签
pyplot.imshow(test_images[1].reshape((28,28)),cmap="gray")
pylab.show()
predictions_single = model.predict(img)
print(np.argmax(predictions_single[0]))


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值