<jupyter_start><jupyter_text>ASSIGNMENT 1Using Keras to build a MLP for classifying the CIFAR-10 dataset. Each record is of size 1*3072. Building a MLP to classify the data into the 10 classes.  Dataset CIFAR-10 datasetThe CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images per class. There are 50000 training images and 10000 test images. The dataset is divided into five training batches and one test batch, each with 10000 images. The test batch contains exactly 1000 randomly-selected images from each class. The training batches contain the remaining images in random order, but some training batches may contain more images from one class than another. Between them, the training batches contain exactly 5000 images from each class. http://www.cs.utoronto.ca/~kriz/cifar.html  Importing libraries<jupyter_code>import keras
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense, Dropout,Activation,Flatten
from keras.optimizers import RMSprop
from keras.utils import plot_model
import matplotlib.pyplot as plt
from keras.utils import plot_model 
batch_size = 64
num_classes = 10
epochs = 25<jupyter_output><empty_output><jupyter_text>Loading the CIFAR 10 dataset<jupyter_code>(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')<jupyter_output>50000 train samples
10000 test samples<jupyter_text>The data, split between train and test sets:<jupyter_code>y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
x_train.shape[1:]<jupyter_output><empty_output><jupyter_text>Defining the model<jupyter_code>model = Sequential()

model.add(Dense(64, input_shape=x_train.shape[1:]))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(num_classes))
model.add(Activation('softmax'))<jupyter_output><empty_output><jupyter_text>Model Summary<jupyter_code>model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_2 (Dense)              (None, 32, 32, 64)        256       
_________________________________________________________________
activation_2 (Activation)    (None, 32, 32, 64)        0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 32, 32, 64)        0         
_________________________________________________________________
dense_3 (Dense)              (None, 32, 32, 128)       8320      
_________________________________________________________________
activation_3 (Activation)    (None, 32, 32, 128)       0         
_________________________________________________________________
dropout_3 (Dropout)          (None, 32, 32, 128)       0         
_________________________________________________________________
dense_4 (D[...]<jupyter_text>Compiling the model<jupyter_code>model.compile(loss='categorical_crossentropy',

              optimizer='adam',

              metrics=['accuracy'])
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255<jupyter_output><empty_output><jupyter_text>Fitting the model<jupyter_code>mlp = model.fit(x_train, y_train,

                    batch_size=batch_size,

                    epochs=epochs,

                    verbose=1,

                    validation_data=(x_test, y_test))<jupyter_output>Train on 50000 samples, validate on 10000 samples
Epoch 1/25
50000/50000 [==============================] - 61s 1ms/step - loss: 1.6500 - acc: 0.4183 - val_loss: 1.4974 - val_acc: 0.4681
Epoch 2/25
50000/50000 [==============================] - 59s 1ms/step - loss: 1.4527 - acc: 0.4914 - val_loss: 1.4474 - val_acc: 0.4863
Epoch 3/25
50000/50000 [==============================] - 59s 1ms/step - loss: 1.3871 - acc: 0.5176 - val_loss: 1.4123 - val_acc: 0.5061
Epoch 4/25
50000/50000 [==============================] - 59s 1ms/step - loss: 1.3378 - acc: 0.5328 - val_loss: 1.4283 - val_acc: 0.5048
Epoch 5/25
50000/50000 [==============================] - 59s 1ms/step - loss: 1.2861 - acc: 0.5542 - val_loss: 1.4465 - val_acc: 0.5016
Epoch 6/25
50000/50000 [==============================] - 58s 1ms/step - loss: 1.2357 - acc: 0.5761 - val_loss: 1.4431 - val_acc: 0.5069
Epoch 7/25
50000/50000 [==============================] - 58s 1ms/step - loss: 1.1787 - acc: 0.5956 - val_loss: 1.4965 - val_acc[...]<jupyter_text>Scoring and Evaluating trained model.<jupyter_code>scores = model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])<jupyter_output>10000/10000 [==============================] - 4s 358us/step
Test loss: 2.526078281021118
Test accuracy: 0.4402<jupyter_text>Printing the accuracy<jupyter_code>scores = model.evaluate(x_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))<jupyter_output>Accuracy: 44.02%<jupyter_text>Plot training & validation accuracy values<jupyter_code>plt.figure()
plt.plot(mlp.history['acc'])
plt.plot(mlp.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# "Loss"
plt.plot(mlp.history['loss'])
plt.plot(mlp.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()

# plot graph
plot_model(model, to_file='MLP.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16245052486663225, 'fraction_numerical': 0.08002065049044915, 'mean_word_length': 4.979423868312757, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '548112', 'license': 'MIT', 'n_tokens_mistral': 2225, 'n_tokens_neox': 1841, 'n_words': 435, 'path': 'Assignment 1 Keras/CIFAR10_using_MLP1.ipynb', 'repo_name': 'RajeshreeKale/INFO-7374', 'size': '5811'}