其他分享
首页 > 其他分享> > DL-IN-ACT-01 第一个网络

DL-IN-ACT-01 第一个网络

作者:互联网

1. 导入MNIST数据集

from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images.shape
(60000, 28, 28)
train_labels
array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)
test_images.shape
(10000, 28, 28)
test_labels
array([7, 2, 1, ..., 4, 5, 6], dtype=uint8)

2. 构建网络结构

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')) # 10路的softmax层,返回10个概率
network.compile(optimizer='rmsprop',
               loss='categorical_crossentropy',
               metrics=['accuracy']) # 选择loss function, optimizer, metrics

3. 准备图像数据

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

4. 准备标签

from keras.utils import to_categorical

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

5. 训练模型

network.fit(train_images, train_labels, epochs=5, batch_size=128)
Epoch 1/5
60000/60000 [==============================] - 5s 79us/step - loss: 0.2603 - acc: 0.9248
Epoch 2/5
60000/60000 [==============================] - 5s 76us/step - loss: 0.1049 - acc: 0.9687
Epoch 3/5
60000/60000 [==============================] - 5s 77us/step - loss: 0.0686 - acc: 0.9794
Epoch 4/5
60000/60000 [==============================] - 4s 73us/step - loss: 0.0504 - acc: 0.9849
Epoch 5/5
60000/60000 [==============================] - 5s 81us/step - loss: 0.0372 - acc: 0.9886





<keras.callbacks.History at 0x20f3012a390>

6. 评估模型

test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc', test_acc)
10000/10000 [==============================] - 1s 53us/step
test_acc 0.9805

标签:DL,01,train,labels,28,images,60000,ACT,test
来源: https://www.cnblogs.com/jmhwsrr/p/15755938.html