《Python深度學習》第2章——深度學習數學基礎code
阿新 • • 發佈:2019-01-03
from keras.datasets import mnist # 載入 MNIST 資料集 (train_images, train_labels), (test_images, test_labels) = mnist.load_data() # 檢視圖片 import matplotlib.pyplot as plt plt.imshow(test_images[0], cmap=plt.cm.binary) plt.show() print(train_images.shape, len(train_labels)) print(test_images.shape, len(test_labels)) 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')) network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # 展開併除以255 train_images = train_images.reshape((60000, 28*28)) train_images = train_images.astype('float32') / 255.0 test_images = test_images.reshape((10000, 28*28)) test_images = test_images.astype('float32') / 255.0 # Converts a class vector (integers) to binary class matrix. # 整數目標值改為分類目標值 from keras.utils import to_categorical train_labels = to_categorical(train_labels) print(test_labels[:10]) test_labels = to_categorical(test_labels) print(test_labels[:10]) ''' [7 2 1 0 4 1 4 9 5 9] # to_categorical 後 [[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.] [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.] [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]] ''' # 訓練(fit擬合) network.fit(train_images, train_labels, epochs=5, batch_size=128) # 測試集上的效能 test_loss, test_acc = network.evaluate(test_images, test_labels) print('test acc:', test_acc, ' test loss: ', test_loss)