该项目应该属于深度学习里面的“hello world”级别,比较简单试着入门操作一下
#引入必要的包,keras是在TensorFlow的基础上进行封装的库,更加简单易用
#!user/bin/python
# -*-coding: utf8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.datasets import mnist
from keras.layers.core import Dense,Dropout,Activation
from keras.utils import np_utils
#从mnist下载数据集,X是图片,Y是标签
(x_train,y_train),(x_test,y_test) = mnist.load_data()
#看一下这些手写图片大概张什么样子
for i in range(9):
plt.subplot(3,3,i+1)
plt.imshow(x_train[i], cmap='gray', interpolation='none')
plt.title("Digit: {}".format(y_train[i]))
plt.show()
#!user/bin/python
# -*-coding: utf8 -*-
print("x_train_image.shape:",x_train.shape)
print("y_train_label.shape:",y_train.shape)
print("x_test_image.shape:",x_test.shape)
print("y_test_label.shape:",y_test.shape)
print(x_train[0])
#训练集6w,测试1w,每涨图片为28*28=784pix,将原始图片数据转化为784维数据
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000,784)
y_train = y_train.reshape(60000,1)
y_test = y_test.reshape(10000,1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
#原始数据为0-255色度值(0为白色,255为黑色),进行归一化至0-1
x_train = x_train / 255
x_test = x_test / 255
print(x_train)
print(x_test)
print("x_train shape:", np.shape(x_train))
print("x_test shape:", np.shape(x_test))
#分成10类
target_class = 10
#onehot把每个label转化成一个1*10的矩阵
y_train_onehot = np_utils.to_categorical(y_train, target_class)
y_test_onehot = np_utils.to_categorical(y_test, target_class)
#建模三层的全连接模型,第一层入参28*28维,100个神经元,激活函数activation=relu
#第二层入参就是第一层的输出(100),神经元个数10个
#第三层入参是第二层的输出(10),用softmax进行多分类归类,共分为10类
model = Sequential()
model.add(Dense(input_dim=28*28,units=100,activation='relu'))
model.add(Dense(units=10, activation='relu'))
model.add(Dense(units=10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
#进行模型训练
model.fit(x_train, y_train_onehot, batch_size=30, epochs=10, verbose=1)
#模型在测试商的效果
score = model.evaluate(x_test, y_test_onehot)
y_predict = model.predict_classes(x_test)
print("在测试集上表现:Loss={:.4f}, Accuracy={:.4f}".format(score[0], score[1]))
print(y_predict)
print(y_test)
#保存模型
model.save_weights('mnist.model')
在测试集上表现:Loss=0.0949, Accuracy=0.9762
三层,每层10个神经元的情况下,准确率可以达到97.62%(还不错~),可以通过增加层数或每层神经元个数来提高准确率,但是会更耗资源