<jupyter_start><jupyter_text>**Not an ideal network**<jupyter_code># https://keras.io/
# Installing Keras in Colab environment(Colab is Docker container with its own setup) and then import Keras library
!pip install -q keras
import keras
# Import numpy, Keras models, layers, utils and datasets modules

import numpy as np

from keras.models import Sequential
from keras.layers import Flatten
from keras.layers import Convolution2D
from keras.utils import np_utils

from keras.datasets import mnist
# Load MNIST dataset from Keras datasets module seperately as train and test datasets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# Display the shape of train dataset. Also show the first image in the train dataset
print (X_train.shape)
from matplotlib import pyplot as plt
%matplotlib inline
plt.imshow(X_train[0])
# Reshape train and test dataset so that it also contains the channel information. MNIST contains grey scale images, hence this has just 1 channel
X_train = X_train.reshape(X_train.shape[0], 28, 28,1)
X_test = X_test.reshape(X_test.shape[0], 28, 28,1)
# Convert the Tensor i.e. train and test dataset to float tensors. Also perform a min-max normalization where min = 0 and max = 255
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
# Show 10 items of labels from train dataset
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)
# Show 10 labels from train dataset
Y_train[:10]

# Create a CNN model using Keras functions. Use relu for all layers, except at the end. The logits are passed into softmax layer to represent everything between 0 and 1.
# Display the model summary

from keras.layers import Activation, MaxPooling2D

# Input is an image of size 28x28 with 1 channel
model = Sequential() 

model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(28,28,1))) #Input channel 1, number of filters 32, filter size is 3x3 with 1 channel. Receptive field is 3x3
model.add(Convolution2D(64, 3, 3, activation='relu'))#Input channels 32, number of filters 64, filter size is 3x3 with 32 channels. Receptive field is 5x5
model.add(Convolution2D(128, 3, 3, activation='relu'))#Input channels 64, number of filters 128, filter size is 3x3 with 64 channels. Receptive field is 7x7

model.add(MaxPooling2D(pool_size=(2, 2)))#Input channels 128, number of filters n/a, filter size is n/a with n/a channels. Receptive field is 14x14

model.add(Convolution2D(256, 3, 3, activation='relu'))#Input channels 128, number of filters 256, filter size is 3x3 with 128 channels. Receptive field is 16x16
model.add(Convolution2D(512, 3, 3, activation='relu'))#Input channels 256, number of filters 512, filter size is 3x3 with 256 channels. Receptive field is 18x18
model.add(Convolution2D(1024, 3, 3, activation='relu'))#Input channels 512, number of filters 1024, filter size is 3x3 with 512 channels. Receptive field is 20x20
model.add(Convolution2D(2048, 3, 3, activation='relu'))#Input channels 1024, number of filters 2048, filter size is 3x3 with 1024 channels. Receptive field is 22x22
model.add(Convolution2D(10, 3, 3, activation='relu'))#Input channels 2048, number of filters 10, filter size is 3x3 with 2048 channels. Receptive field is 24x24

# Flatten the output from the last layer and pass these to softmax activation function
model.add(Flatten())
model.add(Activation('softmax'))

# Display model summary i.e. different layers and their dimentions
model.summary()
# Compile the model to use "Categorical Crossentropy loss", "Adam" optimser and use accuracy metric
model.compile(loss='categorical_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])
# Train the model with batch size of 32 for 10 epochs
model.fit(X_train, Y_train, batch_size=32, nb_epoch=10, verbose=1)
# Evaluate the model using test dataset.
score = model.evaluate(X_test, Y_test, verbose=0)
# Print the evaluation metric/score
print(score)
# Perform predictions on test dataset
y_pred = model.predict(X_test)
# Print 9 predictions from test dataset. Also print the actual labels from test dataset
print(y_pred[:9])
print(y_test[:9])<jupyter_output>[[1.1116206e-09 1.1116206e-09 1.1116206e-09 1.1116206e-09 1.1116206e-09
  1.1116206e-09 1.1116206e-09 1.0000000e+00 1.1116206e-09 1.1116206e-09]
 [4.5880510e-25 4.5880510e-25 1.0000000e+00 4.5880510e-25 4.5880510e-25
  4.5880510e-25 4.5880510e-25 4.5880510e-25 4.5880510e-25 4.5880510e-25]
 [1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01
  1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01]
 [1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01
  1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01]
 [1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01
  1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01]
 [1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01
  1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01]
 [1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01
  1.0000000e-01 1.0000000e-01 1.0000000e-01 1.0000000e-01 [...]
{'fraction_non_alphanumeric': 0.09783434650455927, 'fraction_numerical': 0.17591185410334345, 'mean_word_length': 4.837028824833703, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 51, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '574387', 'license': 'Apache-2.0', 'n_tokens_mistral': 2484, 'n_tokens_neox': 1784, 'n_words': 602, 'path': 'Phase1/Project2/Session2.ipynb', 'repo_name': 'ksasi/Deep_Vision', 'size': '5264'}