手写数字识别

from keras.datasets import mnist
from keras import models
from keras import layers
from keras.utils import to_categorical


import numpy as np
def loadData(path="mnist.npz"):
    f = np.load(path)
    x_train, y_train = f['x_train'], f['y_train']
    x_test, y_test = f['x_test'], f['y_test']
    f.close()
    return (x_train, y_train), (x_test, y_test)


(train_images, train_lables),(test_images,test_labels) = loadData()

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

train_lables=to_categorical(train_lables)#对标签进行分类编码,one hot编码
test_labels=to_categorical(test_labels)


network.fit(train_images,train_lables, epochs=5,batch_size=128)

出现了一个问题:https://blog.csdn.net/ouening/article/details/85065709

后来发现是标签分类编码那里写错了。。

posted @ 2020-03-25 16:19  Tomorrow1126  阅读(170)  评论(0编辑  收藏  举报