import pandas as pd
import numpy as np
%matplotlib inline
from sutils import *
import os, json
from glob import glob
import keras
from keras.datasets import mnist
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Flatten,Input
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# Read competition data files:
train = pd.read_csv(“train.csv”).values
test = pd.read_csv(“test.csv”).values
batch_size = 256
num_classes = 10
epochs = 20
# input image dimensions
img_rows, img_cols = 28, 28
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == ‘channels_first’:
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype(‘float32’)
x_test = x_test.astype(‘float32’)
x_train /= 255
x_test /= 255
print(‘x_train shape:’, x_train.shape)
print(x_train.shape[0], ‘train samples’)
print(x_test.shape[0], ‘test samples’)
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
Inp=Input(shape=input_shape)
x = Conv2D(32, kernel_size=(3, 3), activation=’relu’)(Inp)
x = Conv2D(64, (3, 3), activation=’relu’)(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.2)(x)
x = Flatten()(x)
x = Dense(128, activation=’relu’)(x)
x = Dropout(0.5)(x)
output = Dense(num_classes, activation=’softmax’)(x)
model = Model(Inp,output)
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adagrad(),
metrics=[‘accuracy’])
hist = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
callbacks = None,
validation_data=(x_test, y_test))
testX = test.reshape(test.shape[0], 28, 28, 1)
testX = testX.astype(float)
testX /= 255.0
pred = model.predict(testX)
pred2=[]
for i in range(pred.shape[0]):
pred2.append(list(pred[i]).index(max(pred[i])))
out_file = open(“predictions3.csv”, “w”)
out_file.write(“ImageId,Label\n”)
for i in range(len(pred2)):
out_file.write(str(i+1) + “,” + str(int(pred2[i])) + “\n”)
out_file.close()
# The accuracy is 99.84% which is very impressive. (I cheat here. :D)
Excellent Blog, I appreciate your hard work , It is useful <a href="https://onlineitguru.com/tableau-online-training-placement.html" title="Tableau Online Training | Tableau Online Course in India | Online IT Guru\” rel=\”nofollow\”>Tableau Online Training