《Tensorflow + Keras深度学习人工智能实践应用》 一书第8章的完整例子,相对于第7章的感知机,第八章使用了神经网络,进一步提高了准确率。
在实现上,主要增加了卷积层和池化层。卷积层,使用了filters(即使用多少个卷积核)和卷积核的大小,以及做卷积的填充方式。池化,主要缩减了图片的大小。
import numpy as npimport pandas as pdfrom keras.utils import np_utilsfrom keras.datasets import mnistfrom keras.models import Sequentialfrom keras.layers import Densefrom keras.layers import Dropout, Flatten, Conv2D, MaxPooling2Dimport matplotlib.pyplot as pltimport os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'np.random.seed(10) (x_train_image, y_train_label), (x_test_image, y_test_label) = mnist.load_data() x_train = x_train_image.reshape(x_train_image.shape[0], 28, 28, 1).astype("float32") x_test = x_test_image.reshape(x_test_image.shape[0], 28, 28, 1).astype("float32") x_train_normal = x_train / 255x_test_normal = x_test / 255y_train_onehot = np_utils.to_categorical(y_train_label) y_test_onehot = np_utils.to_categorical(y_test_label) model = Sequential() model.add(Conv2D(filters=16, kernel_size = (5, 5), padding = 'same', input_shape = (28, 28, 1), activation = 'relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(filters=36, kernel_size = (5, 5), padding = 'same', activation = 'relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(units = 128, activation = 'relu')) model.add(Dropout(0.5)) model.add(Dense(units = 10, kernel_initializer = 'normal', activation = 'softmax')) print(model.summary()) model.compile(loss = "categorical_crossentropy", optimizer = "adam", metrics = ["accuracy"]) history = model.fit(x = x_train_normal, y = y_train_onehot, validation_split = 0.2, epochs = 10, batch_size = 300, verbose = 2)def show_train_history(train_history, train, val): plt.plot(train_history.history[train]) plt.plot(train_history.history[val]) plt.title("Train History") plt.ylabel(train) plt.xlabel("Epochs") plt.legend(["train", "validation"], loc="upper left") plt.show()def plot_image_label_prediction(images, labels, prediction, idx = 0, num = 10): fig = plt.gcf() fig.set_size_inches(12, 14) if num > 25: num = 25 for i in range(0, num): ax = plt.subplot(5, 5, 1 + i) ax.imshow(images[idx], cmap="binary") title = "label = " + str(labels[idx]) if len(prediction) > 0: title += ", prediction = " + str(prediction[idx]) ax.set_title(title, fontsize = 12) ax.set_xticks([]) ax.set_yticks([]) idx += 1 plt.show() show_train_history(history, "acc", "val_acc") show_train_history(history, "loss", "val_loss") scores = model.evaluate(x_test_normal, y_test_onehot) print("accuracy = ", scores[1]) prediction = model.predict_classes(x_test_normal)#plot_image_label_prediction(x_test_image, y_test_label, prediction, idx=340, num=25)print(pd.crosstab(y_test_label, prediction, rownames = ["label"], colnames = ["predict"])) df = pd.DataFrame({"label": y_test_label, "predict": prediction}) print(df[(df.label == 5) & (df.predict == 3)])
训练及精度:
Train on 48000 samples, validate on 12000 samples Epoch 1/10 - 43s - loss: 0.5479 - acc: 0.8286 - val_loss: 0.1138 - val_acc: 0.9670 Epoch 2/10 - 44s - loss: 0.1516 - acc: 0.9541 - val_loss: 0.0766 - val_acc: 0.9765 Epoch 3/10 - 46s - loss: 0.1127 - acc: 0.9663 - val_loss: 0.0575 - val_acc: 0.9827 Epoch 4/10 - 46s - loss: 0.0937 - acc: 0.9721 - val_loss: 0.0502 - val_acc: 0.9857 Epoch 5/10 - 46s - loss: 0.0812 - acc: 0.9756 - val_loss: 0.0457 - val_acc: 0.9870 Epoch 6/10 - 45s - loss: 0.0693 - acc: 0.9789 - val_loss: 0.0426 - val_acc: 0.9879 Epoch 7/10 - 46s - loss: 0.0617 - acc: 0.9808 - val_loss: 0.0410 - val_acc: 0.9883 Epoch 8/10 - 44s - loss: 0.0568 - acc: 0.9830 - val_loss: 0.0375 - val_acc: 0.9896 Epoch 9/10 - 44s - loss: 0.0523 - acc: 0.9845 - val_loss: 0.0358 - val_acc: 0.9898 Epoch 10/10 - 43s - loss: 0.0468 - acc: 0.9859 - val_loss: 0.0360 - val_acc: 0.9895 10000/10000 [==============================] - 4s 389us/step accuracy = 0.9922
作者:YANWeichuan
链接:https://www.jianshu.com/p/47e601c6c2e1
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦