参考资料
- 《python深度学习》
这个程序就是AI入门的“Hello World”,大家可以自己安装一下Keras,参考链接。在保证安装正确的前提下,程序代码如下图所示:
import keras
from keras.datasets import mnist
from keras import models
from keras import layers
from keras.utils import np_utils
#Load dataset
(train_images, train_labels),(test_images, test_labels) = mnist.load_data()
print(train_images.shape)
print(test_images.shape)
#Network architecture
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))
network.add(layers.Dense(10, activation='softmax'))
#Compile
network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
#Prepare image data
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
#Prepare labels
train_labels = np_utils.to_categorical(train_labels)
test_labels = np_utils.to_categorical(test_labels)
#Begin to train
network.fit(train_images, train_labels, epochs=5, batch_size=128)
运行效果如下:
在import utils的过程中可能会遇到一些问题,可以参考这篇文章的解决方法。我们对训练结果进行测试,测试代码如下所示:
test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc:',test_acc)
最终测试结果为:
可见测试集的京都为97.8%,比训练集低了很多。训练精度和测试精度之间的这种差距是过拟合(overfit)造成的,后文我们将对过拟合问题进行深入学习和分析。
我还可以将数据集中的某个图片用matplotlib打开展示出来,代码如下所示:
import matplotlib.pyplot as plt
#Show image
digit = train_images[4]
plt.imshow(digit, cmap= plt.cm.binary)
plt.show()
运行效果如下图所示: