<jupyter_start><jupyter_text>Model without regularization<jupyter_code>model = Sequential()
    
model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(32,32,3)))    
model.add(Conv2D(32, (3, 3), activation='relu'))    
model.add(MaxPool2D(pool_size=(2, 2)))

model.add(Flatten())

model.add(Dense(512, activation='relu'))

model.add(Dense(10, activation='softmax'))

model.summary()

model.compile(loss='categorical_crossentropy', 
              optimizer='adam',
              metrics = ['accuracy'])
model_details = model.fit(x_train, y_train,
                    batch_size = 512, 
                    epochs = 10, 
                    validation_data= (x_test, y_test),
                    verbose=1)
score = model.evaluate(x_test, y_test)
print("Accuracy: {0:.2f}%".format(score[1]*100))<jupyter_output>10000/10000 [==============================] - 21s 2ms/step
Accuracy: 10.00%<jupyter_text>Model with regularization<jupyter_code>model = Sequential()
    
model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(32,32,3)))    
model.add(Conv2D(32, (3, 3), activation='relu'))    
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.10))

model.add(Flatten())

model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(10, activation='softmax'))

model.summary()
model.compile(loss='categorical_crossentropy', 
              optimizer='adam',
              metrics = ['accuracy'])
model_details = model.fit(x_train, y_train,
                    batch_size = 512, 
                    epochs = 10, 
                    validation_data= (x_test, y_test),
                    verbose=1)
score = model.evaluate(x_test, y_test)
print("Accuracy: {0:.2f}%".format(score[1]*100))
y_pred = model.predict(x_test)
correct_indices = np.nonzero(np.argmax(y_pred,axis=1) == np.argmax(y_test,axis=1))[0]
incorrect_indices = np.nonzero(np.argmax(y_pred,axis=1) != np.argmax(y_test,axis=1))[0]
labels = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
image = 3
plt.imshow(x_test[incorrect_indices[image]].reshape(32,32,3))
plt.show()
print("Prediction: {0}".format(labels[np.argmax(y_pred[incorrect_indices[image]])]))
image = 2
plt.imshow(x_test[correct_indices[image]].reshape(32,32,3))
plt.show()
print("Prediction: {0}".format(labels[np.argmax(y_pred[correct_indices[image]])]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1905344757241942, 'fraction_numerical': 0.04569563443492452, 'mean_word_length': 4.448888888888889, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '75949', 'license': 'MIT', 'n_tokens_mistral': 977, 'n_tokens_neox': 889, 'n_words': 121, 'path': 'Chapter06/Exercise 52.ipynb', 'repo_name': 'TrainingByPackt/Master-Data-Science-with-Python-', 'size': '2451'}