<jupyter_start><jupyter_text>**Import Libraries and modules** MNIST - Using DNN- In the following, i tried to achieve Testing/Evaluation accuracy of atleast 99% using the Deep Neural Network.- Below is the network that achieved 99% accuracy- Trails which i attempted are listed in the Trails section<jupyter_code># https://keras.io/
!pip install -q keras
import keras

import numpy as np

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, Add
from keras.layers import Convolution2D, MaxPooling2D, Conv2D
from keras.utils import np_utils

from keras.datasets import mnist
print(keras.__version__)<jupyter_output>2.2.2<jupyter_text>Load pre-shuffled MNIST data into train and test sets<jupyter_code>(X_train, y_train), (X_test, y_test) = mnist.load_data()
print (X_train.shape)
from matplotlib import pyplot as plt
%matplotlib inline

random_value = np.random.randint(low=0, high=len(X_train), dtype='int64')

plt.imshow(X_train[random_value], cmap='gray')
plt.title(y_train[random_value])
print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)
X_train = X_train.reshape(X_train.shape[0], 28, 28,1)
X_test = X_test.reshape(X_test.shape[0], 28, 28,1)
print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
y_train[:10]
# Convert 1-dimensional class arrays to 10-dimensional class matrices
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)
Y_train[:10]<jupyter_output><empty_output><jupyter_text>Below is the model which achieved 99% accuracy on the Test data<jupyter_code>from keras.layers import Activation
model = Sequential()
 
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28,28,1))) #26
model.add(Conv2D(64, (3, 3), activation='relu')) #24
model.add(MaxPooling2D(pool_size=(2,2), strides=None, padding='valid')) #12
model.add(Conv2D(128, (3, 3))) # 10
model.add(Conv2D(256, (3, 3))) # 8
model.add(MaxPooling2D(pool_size=(2,2), strides=None, padding='valid')) #4
model.add(Conv2D(10, (1, 1), activation='relu'))
model.add(Conv2D(10, 4))
model.add(Flatten())
model.add(Activation('softmax'))
model.summary()
model.compile(loss='categorical_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=32, epochs=10, verbose=1)
score = model.evaluate(X_test, Y_test, verbose=0)
print(score)
print(score)
y_pred = model.predict(X_test)
print(y_pred[:9])
print(y_test[:9])
pred_arr = list(np.argmax(arr) for arr in y_pred[:9])

print(pred_arr)
print(y_test[:9])
layer_dict = dict([(layer.name, layer) for layer in model.layers])
import numpy as np
from matplotlib import pyplot as plt
from keras import backend as K
%matplotlib inline
# util function to convert a tensor into a valid image
def deprocess_image(x):
    # normalize tensor: center on 0., ensure std is 0.1
    x -= x.mean()
    x /= (x.std() + 1e-5)
    x *= 0.1

    # clip to [0, 1]
    x += 0.5
    x = np.clip(x, 0, 1)

    # convert to RGB array
    x *= 255
    #x = x.transpose((1, 2, 0))
    x = np.clip(x, 0, 255).astype('uint8')
    return x

def vis_img_in_filter(img = np.array(X_train[2]).reshape((1, 28, 28, 1)).astype(np.float64), 
                      layer_name = 'conv2d_14'):
    layer_output = layer_dict[layer_name].output
    img_ascs = list()
    for filter_index in range(layer_output.shape[3]):
        # build a loss function that maximizes the activation
        # of the nth filter of the layer considered
        loss = K.mean(layer_output[:, :, :, filter_index])

        # compute the gradient of the input picture wrt this loss
        grads = K.gradients(loss, model.input)[0]

        # normalization trick: we normalize the gradient
        grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)

        # this function returns the loss and grads given the input picture
        iterate = K.function([model.input], [loss, grads])

        # step size for gradient ascent
        step = 5.

        img_asc = np.array(img)
        # run gradient ascent for 20 steps
        for i in range(20):
            loss_value, grads_value = iterate([img_asc])
            img_asc += grads_value * step

        img_asc = img_asc[0]
        img_ascs.append(deprocess_image(img_asc).reshape((28, 28)))
        
    if layer_output.shape[3] >= 35:
        plot_x, plot_y = 6, 6
    elif layer_output.shape[3] >= 23:
        plot_x, plot_y = 4, 6
    elif layer_output.shape[3] >= 11:
        plot_x, plot_y = 2, 6
    else:
        plot_x, plot_y = 1, 2
    fig, ax = plt.subplots(plot_x, plot_y, figsize = (12, 12))
    ax[0, 0].imshow(img.reshape((28, 28)), cmap = 'gray')
    ax[0, 0].set_title('Input image')
    fig.suptitle('Input image and %s filters' % (layer_name,))
    fig.tight_layout(pad = 0.3, rect = [0, 0, 0.9, 0.9])
    for (x, y) in [(i, j) for i in range(plot_x) for j in range(plot_y)]:
        if x == 0 and y == 0:
            continue
        ax[x, y].imshow(img_ascs[x * plot_y + y - 1], cmap = 'gray')
        ax[x, y].set_title('filter %d' % (x * plot_y + y - 1))
vis_img_in_filter(layer_name='conv2d_1')
# vis_img_in_filter(layer_name='conv2d_2')<jupyter_output><empty_output><jupyter_text>Trails Observations- As per the Keras source code, Conv2D is an alias for the Convolution2D. But for the same Network architecute where Conv2D is used instead of Convolution2D; **Evaluation accuracy is more**. Unable to understand,why?- Can observe model_1 and model_3 evaluation results  Trail - 1<jupyter_code>from keras.layers import Activation
model_1 = Sequential()
 
model_1.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(28,28,1))) #26
model_1.add(Convolution2D(64, 3, 3, activation='relu')) #24
model_1.add(MaxPooling2D(pool_size=(2,2), strides=None, padding='valid')) #12
model_1.add(Convolution2D(128, 3, 3)) # 10
model_1.add(Convolution2D(256, 3, 3)) # 8
model_1.add(Convolution2D(10, 1, 1, activation='relu'))
model_1.add(Convolution2D(10, 8))
model_1.add(Flatten())
model_1.add(Activation('softmax'))
model_1.summary()
model_1.compile(loss='categorical_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])


model_1.fit(X_train, Y_train, batch_size=32, epochs=10, verbose=1)
score_1 = model_1.evaluate(X_test, Y_test, verbose=0)
print(score_1)<jupyter_output>[0.044826604385943304, 0.9885]<jupyter_text>Trail - 2- With Conv2D<jupyter_code>from keras.layers import Activation
model_3 = Sequential()

model_3.add(Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1))) #26
model_3.add(Conv2D(64, (3,3),activation='relu')) # 24
model_3.add(MaxPooling2D()) #12
model_3.add(Conv2D(128, (3,3), activation='relu')) #10
model_3.add(Conv2D(256, (3,3), activation='relu', padding='valid')) # 8
model_3.add(Conv2D(10,1, activation='relu'))
model_3.add(Conv2D(10,8))
model_3.add(Flatten())
model_3.add(Activation('softmax'))
model_3.summary()
model_3.compile(loss='categorical_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])
model_3.fit(X_train, Y_train, batch_size=32, epochs=10, verbose=1)
score_3 = model_3.evaluate(X_test, Y_test, verbose=0)
print(score_3)<jupyter_output>[0.028622790005646312, 0.9918]<jupyter_text>Trail - 3<jupyter_code>from keras.layers import Activation
model_4 = Sequential()

model_4.add(Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)))
model_4.add(Conv2D(64, (3,3),activation='relu'))
model_4.add(MaxPooling2D())
model_4.add(Conv2D(128, (3,3), activation='relu'))
model_4.add(Conv2D(10,1, activation='relu'))
model_4.add(Conv2D(10,10))
model_4.add(Flatten())
model_4.add(Activation('softmax'))
model_4.summary()
model_4.compile(loss='categorical_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])
model_4.fit(X_train, Y_train, batch_size=32, epochs=10, verbose=1)
score_4 = model_4.evaluate(X_test, Y_test, verbose=0)
print(score_4)<jupyter_output>[0.02959476177810229, 0.9917]<jupyter_text>Trail - 4<jupyter_code>from keras.layers import Activation
model_5 = Sequential()
 
model_5.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(28,28,1))) #26
model_5.add(Convolution2D(64, 3, 3, activation='relu')) #24
model_5.add(MaxPooling2D(pool_size=(2,2), strides=None, padding='valid')) #12
model_5.add(Convolution2D(64, 3, 3)) # 10
model_5.add(Convolution2D(128, 3, 3)) # 8
model_5.add(MaxPooling2D(pool_size=(2,2), strides=None, padding='valid')) #4
# model_5.add(Convolution2D(10, 1, 1, activation='relu'))
model_5.add(Convolution2D(10, 4))
model_5.add(Flatten())
model_5.add(Activation('softmax'))
model_5.summary()
model_5.compile(loss='categorical_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])
model_5.fit(X_train, Y_train, batch_size=32, epochs=10, verbose=1)
score_5 = model_5.evaluate(X_test, Y_test, verbose=0)
print(score_5)<jupyter_output>[0.04269996428527675, 0.9907]
{'fraction_non_alphanumeric': 0.1477425122932499, 'fraction_numerical': 0.0640366562360304, 'mean_word_length': 4.868196721311476, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '81193', 'license': 'MIT', 'n_tokens_mistral': 3717, 'n_tokens_neox': 3372, 'n_words': 731, 'path': 'wk-1/1st_DNN_trails.ipynb', 'repo_name': 'vamsigp/EIP-4', 'size': '8948'}