keras识别mnist

import numpy as np
np.random.seed(1337) #重复性
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Activation
from keras.optimizers import RMSprop

#数据的预处理
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/Users/wutao/PycharmProjects/手写数字识别/MNIST_data", one_hot=True)
X_train, Y_train = mnist.train.images, mnist.train.labels
X_test, Y_test = mnist.test.images, mnist.test.labels
X_train = X_train.reshape(X_train.shape[0],-1)/255
X_test = X_test.reshape(X_test.shape[0],-1)/255
# 搭建神经网络
model = Sequential([
    Dense(32,input_dim=784),
    Activation('relu'),
    Dense(10),
    Activation('softmax'),
    ])

# 优化器
rmsporp = RMSprop(lr=0.001,rho=0.9,epsilon=1e-08)

model.compile(
    optimizer= rmsporp,
    loss = 'categorical_crossentropy',
    metrics = ['accuracy'],
    )

print('Training...................')
model.fit(X_train,Y_train,nb_epoch=1,batch_size=32)

print('\nTesting..................')
loss,accuracy = model.evaluate(X_test,Y_test)

print('test loss:',loss)
print('test accuracy:',accuracy)
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页