<jupyter_start><jupyter_text>CIFAR-10 using Simple Convolution Neural Networks  Importing Libraries and Packages<jupyter_code>import numpy as np 
import pandas as pd 
from tensorflow import keras
from keras import Model, Input
from keras.datasets import cifar10
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, BatchNormalization, Dropout
from keras.utils.np_utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator 
from keras.models import Sequential
from keras.optimizers import Adam<jupyter_output><empty_output><jupyter_text>Loading and Processing data<jupyter_code>(x_train, y_train), (x_test, y_test) = cifar10.load_data()<jupyter_output><empty_output><jupyter_text>Before moving on let's normalize the imgae data so that it is between 0 and 1.<jupyter_code>x_train = x_train / 255.0
x_test = x_test / 255.0
x_train.shape, y_train.shape<jupyter_output><empty_output><jupyter_text>As the labels are intergers let's convert them into array for simpler processing.<jupyter_code>y_train = to_categorical(y_train, num_classes = 10)
y_test = to_categorical(y_test, num_classes = 10)<jupyter_output><empty_output><jupyter_text>Building and training model<jupyter_code>def cnn(input_shape = (32,32,3), classes = 10) :
    X_input = Input(input_shape)
    
    #Convolution Block 1
    X = Conv2D(filters = 16, kernel_size=(3,3), activation = "relu", padding = "Same")(X_input)
    X = MaxPooling2D(pool_size = (2,2), strides = 2)(X)
    X = BatchNormalization()(X)
    
    #Convolution Block 2
    X = Conv2D(filters = 32, kernel_size=(3,3), activation = "relu", padding = "Same")(X)
    X = MaxPooling2D(pool_size = (2,2), strides = 2)(X)
    X = BatchNormalization()(X)
    
    #Convolution Block 3
    X = Conv2D(filters = 64, kernel_size=(3,3), activation = "relu", padding = "Same")(X)
    X = MaxPooling2D(pool_size = (2,2), strides = 2)(X)
    X = BatchNormalization()(X)

    
    X = Flatten()(X)
    X = Dense(100, activation = 'relu')(X)
    X = BatchNormalization(axis = 1)(X)
    X_output = Dense(10, activation = 'softmax')(X)
    
    model = Model(X_input, X_output)
    
    return model<jupyter_output><empty_output><jupyter_text>Let's name the model and see it's structure and other important architectural details.<jupyter_code>model = cnn(input_shape = (32,32,3), classes = 10)
model.summary()<jupyter_output>Model: "functional_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 32, 32, 3)]       0         
_________________________________________________________________
conv2d (Conv2D)              (None, 32, 32, 16)        448       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 16, 16, 16)        0         
_________________________________________________________________
batch_normalization (BatchNo (None, 16, 16, 16)        64        
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 16, 16, 32)        4640      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 8, 8, 32)          0         
______________________________________________________[...]<jupyter_text>This code below defines the important hyperparameters for training of model.We're using learning rate decay for smoother and accurate training of model.<jupyter_code>batch_size = 128
epochs = 20
lr_schedule = keras.optimizers.schedules.ExponentialDecay(
    initial_learning_rate=0.01,
    decay_steps=5000,
    decay_rate=0.9)
optimizer = keras.optimizers.Adam(learning_rate=lr_schedule)<jupyter_output><empty_output><jupyter_text>Compiling and fitting the model.<jupyter_code>model.compile(loss="categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"])
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.0)<jupyter_output>Epoch 1/20
391/391 [==============================] - 42s 107ms/step - loss: 1.2835 - accuracy: 0.5425
Epoch 2/20
391/391 [==============================] - 46s 116ms/step - loss: 0.8995 - accuracy: 0.6806
Epoch 3/20
391/391 [==============================] - 41s 106ms/step - loss: 0.7579 - accuracy: 0.7323
Epoch 4/20
391/391 [==============================] - 41s 105ms/step - loss: 0.6594 - accuracy: 0.7700
Epoch 5/20
391/391 [==============================] - 41s 106ms/step - loss: 0.5843 - accuracy: 0.7925
Epoch 6/20
391/391 [==============================] - 43s 109ms/step - loss: 0.5221 - accuracy: 0.8149
Epoch 7/20
391/391 [==============================] - 46s 119ms/step - loss: 0.4540 - accuracy: 0.8395
Epoch 8/20
391/391 [==============================] - 48s 122ms/step - loss: 0.3985 - accuracy: 0.8574
Epoch 9/20
391/391 [==============================] - 46s 117ms/step - loss: 0.3530 - accuracy: 0.8753
Epoch 10/20
391/391 [==============================] - 46s 118ms/step - l[...]<jupyter_text>Checking the accuracy of the model.<jupyter_code>score = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])<jupyter_output>Test loss: 1.5554429292678833
Test accuracy: 0.7289999723434448
{'fraction_non_alphanumeric': 0.16964117978583507, 'fraction_numerical': 0.0775878264136765, 'mean_word_length': 4.948603351955307, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '594015', 'license': 'MIT', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1669, 'n_words': 397, 'path': 'cnn.ipynb', 'repo_name': '007mathur/CIFAR-10', 'size': '5323'}