MNIST全连接实现(keras)

keras全连接网络实现(Sequential)

一 加载keras中的MNIST数据集

from keras.datasets import mnist
Using TensorFlow backend.
(train_images,train_labels),(test_images,test_labels)=mnist.load_data()
A local file was found, but it seems to be incomplete or outdated because the auto file hash does not match the original value of 8a61469f7ea1b51cbae51d4f78837e45 so we will re-download the data.
Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz
11493376/11490434 [==============================] - 700s 61us/step
train_images.shape
(60000, 28, 28)
len(train_labels)
60000
train_labels
array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)
test_images.shape
(10000, 28, 28)
len(test_labels)
10000
test_labels
array([7, 2, 1, ..., 4, 5, 6], dtype=uint8)

二 网络架构

from keras import models #模型
from keras import layers #层
network = models.Sequential()
network.add(layers.Dense(512,activation='relu',input_shape=(28*28,))) #添加全连接层
network.add(layers.Dense(10,activation='softmax'))

三 编译步骤

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 import to_categorical

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

开始训练(fit)

network.fit(train_images,train_labels,epochs=5,batch_size=128)
Epoch 1/5
60000/60000 [==============================] - 4s 66us/step - loss: 0.2578 - acc: 0.9251
Epoch 2/5
60000/60000 [==============================] - 4s 66us/step - loss: 0.1039 - acc: 0.9691
Epoch 3/5
60000/60000 [==============================] - 4s 67us/step - loss: 0.0683 - acc: 0.9795
Epoch 4/5
60000/60000 [==============================] - 4s 67us/step - loss: 0.0498 - acc: 0.9849
Epoch 5/5
60000/60000 [==============================] - 4s 67us/step - loss: 0.0374 - acc: 0.9884





<keras.callbacks.History at 0x7f596a807dd8>

开始测试

test_loss,test_acc = network.evaluate(test_images,test_labels)
print('test_acc:',test_acc)
10000/10000 [==============================] - 0s 41us/step
test_acc: 0.9798

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是使用Keras实现MNIST分类程序的代码: ```python # 导入相关库 import numpy as np import keras.datasets.mnist as mnist from keras.utils import np_utils from keras.models import Sequential from keras.layers import Dense, Dropout # 加载数据集 (X_train, y_train), (X_test, y_test) = mnist.load_data() # 将数据集转换为指定类型 X_train = X_train.reshape(X_train.shape[0], -1).astype('float32') / 255. X_test = X_test.reshape(X_test.shape[0], -1).astype('float32') / 255. y_train = np_utils.to_categorical(y_train, num_classes=10) y_test = np_utils.to_categorical(y_test, num_classes=10) # 建立模型 model = Sequential() model.add(Dense(units=512, input_dim=784, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(units=512, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(units=10, activation='softmax')) # 编译模型 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # 训练模型 model.fit(X_train, y_train, epochs=20, batch_size=128) # 评估模型 score = model.evaluate(X_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1]) ``` 这个程序使用了keras库来实现MNIST分类。首先,我们使用mnist.load_data()函数来加载MNIST数据集。然后将数据集转换为指定的类型,并对标签进行one-hot编码。接下来,我们建立了一个包含3个全连接层的神经网络。其中前2个层包含了512个神经元和relu激活函数,并添加了0.2的dropout层用于防止过拟合。第3个层包含10个神经元和softmax激活函数,用于输出分类结果。最后,我们编译模型并训练它,最后评估模型的准确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mr.Ma.01

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

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

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

打赏作者

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

抵扣说明:

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

余额充值