keras加載mnist數據集


from keras.datasets import mnist
(train_images,train_labels),(test_images,test_labels)=mnist.load_data()

此處會報 SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed 錯誤

通過下面命令解決

> cd "/Applications/Python 3.7/"
> sudo "./Install Certificates.command"

 載入並訓練數據集

from keras.datasets import mnist

(train_images,train_labels),(test_images,test_labels)=mnist.load_data()

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'])

#准備圖像數據
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)

#訓練數據
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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM