開源快速網(wǎng)站搭建平臺磁力寶最佳搜索引擎入口

希望對你有幫助呀!!💜💜 如有更好理解的思路,歡迎大家留言補充 ~ 一起加油叭 💦
歡迎關(guān)注、訂閱專欄 【深度學(xué)習從 0 到 1】謝謝你的支持!
? 手寫數(shù)字分類: Keras + MNIST 數(shù)據(jù)集
手寫數(shù)字分類任務(wù)
任務(wù):將手寫數(shù)字的灰度圖像(28像素×28像素)劃分到10個類別中(0~9)
MNIST數(shù)據(jù)集包含60 000張訓(xùn)練圖像和10 000張測試圖像,由美國國家標準與技術(shù)研究院(National Institute of Standards and Technology,即 MNIST 中的NIST)在20世紀80年代收集得到
- 樣本示例如下:(hint: 顯示數(shù)據(jù)集的第一個數(shù)字的代碼:
plt.imshow(train_images[0], cmap=plt.cm.binary)
)![]()
💜步驟一 : 加載Keras中的MNIST數(shù)據(jù)集
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data() # 包括4個Numpy數(shù)組# 準備數(shù)據(jù)
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# 準備標簽
from keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
💜步驟二 : 構(gòu)建網(wǎng)絡(luò)架構(gòu) (兩層全連接層為例)
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'))
💜步驟三: 編譯步驟 (optimizer + loss + metrics)
network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
💜步驟四:訓(xùn)練網(wǎng)絡(luò)
network.fit(train_images, train_labels, epochs=5, batch_size=128)
💜步驟五:測試網(wǎng)絡(luò)
test_loss, test_acc = network.evaluate(test_images, test_labels)
完整代碼參考:
from keras.datasets import mnist
from keras import models
from keras import layers (train_images, train_labels), (test_images, test_labels) = mnist.load_data() # 包括4個Numpy數(shù)組# 準備數(shù)據(jù)
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# 準備標簽
from keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)# 構(gòu)建網(wǎng)絡(luò)架構(gòu)
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'])# 訓(xùn)練網(wǎng)絡(luò)
network.fit(train_images, train_labels, epochs=5, batch_size=128)# 測試網(wǎng)絡(luò)
test_loss, test_acc = network.evaluate(test_images, test_labels) print("Loss: {}, Acc: {}".format(test_loss, test_acc))
----- 結(jié)束后會得到類似如下結(jié)果:
Epoch 1/5
469/469 [==============================] - 2s 5ms/step - loss: 0.2598 - accuracy: 0.9253
Epoch 2/5
469/469 [==============================] - 2s 5ms/step - loss: 0.1041 - accuracy: 0.9692
Epoch 3/5
469/469 [==============================] - 2s 5ms/step - loss: 0.0684 - accuracy: 0.9795
Epoch 4/5
469/469 [==============================] - 2s 5ms/step - loss: 0.0492 - accuracy: 0.9848
Epoch 5/5
469/469 [==============================] - 2s 5ms/step - loss: 0.0367 - accuracy: 0.9892
313/313 [==============================] - 0s 702us/step - loss: 0.0665 - accuracy: 0.9803
Loss: 0.06652633100748062, Acc: 0.9803000092506409
參考書籍:Python 深度學(xué)習