神经网络初体验-多层感知机

纪念一下我写(doge:抄)的第一个神经网络,ヾ(◍°∇°◍)ノ゙

from keras.datasets import mnist
from keras import models
from keras import layers
from keras.utils import to_categorical

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# 创建一个模型
network = models.Sequential()
# 在模型中新增一个全连接层,512个神经元,激活函数是relu函数,输入矩阵的形状是28*28
network.add(layers.Dense(512,activation='relu',input_shape=(28*28,)))
# 全连接层厚新增另一个全连接层,10个神经元,激活函数是softmax用来实现手写体的多分类
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
# 准备标签
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
# 拟合
network.fit(train_images, train_labels, epochs=5, batch_size=256)
# 获取拟合结果
test_loss, test_acc = network.evaluate(test_images,test_labels)
print("test_acc: ", test_acc)

测试结果:
神经网络初体验-多层感知机_第1张图片

你可能感兴趣的:(神经网络,深度学习,机器学习)