keras--卷积mnist

 

from keras.models import Sequential
from keras.layers import Dense,Deconv2D,Flatten,MaxPooling2D,Activation
import numpy as np
from keras.optimizers import Adam
from keras.utils import np_utils
from keras.datasets import mnist

(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train = x_train.reshape(-1,1,28,28)
x_test = x_test.reshape(-1,1,28,28)
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)

# built model
model = Sequential()
model.add(Deconv2D(
    filters=32,kernel_size=(5,5),padding='same',
    input_shape=(1,28,28)
))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same'))
model.add(Deconv2D(64,kernel_size=(5,5),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2),padding='same'))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(10))
model.add(Activation('softmax'))

adam = Adam(lr=1e-4)
model.compile(loss='categorical_crossentropy',optimizer=adam,metrics=['accuracy'])

print('training...')
model.fit(x_train,y_train,epochs=1,batch_size=32)
cost,accuracy = model.evaluate(x_test,y_test)
print('cost = ',cost,' accuracy = ',accuracy)

 

 

 

 

你可能感兴趣的:(keras)