手写数字识别mnist

#第一步:加载keras中的mnist集
from keras.datasets import mnist
(train_images, train_labels),(test_images, test_labels) = mnist.load_data()

print(train_images.shape)#查看效果的,这两步可以忽略
print(test_images.shape)

#熟悉一下matplotlib.pyplot数据显示
import matplotlib.pyplot as plt
plt.figure()#新建一个图层
plt.imshow(train_images[0],cmap=“gray”)
plt.show()

#第二步:建立网络架构,
from keras import models
from keras import layers
#层layer就像数据处理的筛子
#本例子含有两个Dense层,最后是一个10路的激活层softmax层,返回一个由10个概率值组成的数组,表示10个数字类别中某一个的概率
network=models.Sequential()
network.add(layers.Dense(512,activation=‘relu’,input_shape=(28*28,)))
network.add(layers.Dense(10,activation=‘softmax’))

#第三步:编译
#optimizer 优化器,loss损失函数,metrics代码级数据监控
network.compile(optimizer=‘rmsprop’,loss=‘categorical_crossentropy’,metrics=[‘accuracy’])

#第四步:准备图像数据和标签
train_images = train_images.reshape((60000, 28 * 28))#图像处理
train_images = train_images.astype(‘float32’) / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype(‘float32’) / 255

#通过二维关系矩阵的方式,生成一个对应关系
from keras.utils.all_utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

#第五步:拟合(FIT)模型
network.fit(train_images,train_labels,batch_size=64,epochs=5)

#第六步:评估(EVALUATE)模型
test_loss,test_acc=network.evaluate(test_images,test_labels)

#第七步:查看测试集的精度
print(‘test_acc’,test_acc)
#训练精度和测试精度之间的这种差距是过拟合(overfit)造成的

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风吹落叶花飘荡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值