Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import keras
- from keras.datasets import mnist
- import matplotlib.pyplot as plt
- from keras.models import Sequential
- from keras.layers import Dense, Dropout, Activation, Flatten
- from keras.layers import Conv2D, MaxPooling2D
- (x_train, y_train), (x_test, y_test) = mnist.load_data()
- img_rows = x_train.shape[1]
- img_cols = x_train.shape[2]
- channels = 1 # since images are in greyscale
- print('Training data oryginal shape', x_train.shape)
- print('Testing data oryginal shape', x_test.shape)
- # reshape data to be in form (num,row,col,chanel)
- x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, channels)
- x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, channels)
- target_train = keras.utils.to_categorical(y_train, 10)
- target_test = keras.utils.to_categorical(y_test, 10)
- data_train = x_train.astype('float32')
- data_test = x_test.astype('float32')
- convNN = Sequential()
- convNN.add(Conv2D(5, (5, 5), strides = (1, 1), padding='same', input_shape=(28, 28, 1), \
- activation='relu'))
- convNN.add(Conv2D(5, (3, 3), strides = (3, 3)))
- convNN.add(Activation('relu'))
- convNN.add(MaxPooling2D(pool_size=(3, 3)))
- convNN.add(Flatten())
- convNN.add(Dense(50))
- convNN.add(Activation('relu'))
- convNN.add(Dense(10))
- convNN.add(Activation('softmax'))
- convNN.summary()
- #hyberparameters parameters
- batch_size = 500
- epochs = 10
- # select and initiate optimizer
- opt_sgd = keras.optimizers.sgd(lr=0.05)
- convNN.compile(loss='categorical_crossentropy', optimizer=opt_sgd, metrics=['accuracy'])
- run_hist_sgd = convNN.fit(data_train, target_train, batch_size=batch_size, epochs=epochs,
- validation_data=(data_test, target_test), shuffle=True,
- verbose=1) #verbose = 0: no verbose; 1: progress barr; 2: summary afer epch
- #model accuracy
- scores = convNN.evaluate(data_test, target_test, verbose=True)
- print('Test accuracy:', scores[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement