import tensorflow as tf
						from tensorflow.keras.callbacks import EarlyStopping
						from tensorflow.keras.models import Sequential
						from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout
						import matplotlib.pyplot as plt
						import tensorflowjs as tfjs
					  

						# loading the data
						mnist=tf.keras.datasets.mnist
						(x_train, y_train),(x_valid, y_valid) = mnist.load_data()
					  

							# preprocessing
							x_train = x_train.reshape((x_train.shape[0], 28, 28, 1))
							x_valid = x_valid.reshape((x_valid.shape[0], 28, 28, 1))
							x_train = tf.keras.utils.normalize(x_train, axis=1)
							x_valid = tf.keras.utils.normalize(x_valid, axis=1)
							y_train = tf.keras.utils.to_categorical(y_train)
							y_valid = tf.keras.utils.to_categorical(y_valid)
						  

						# Instantiate model
						model = Sequential()
					  

						model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', 
						activation ='relu', input_shape = (28,28,1)))
						model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', 
											activation ='relu'))
						model.add(MaxPool2D(pool_size=(2,2)))
						model.add(Dropout(0.25))
						
						model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', 
											activation ='relu'))
						model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', 
											activation ='relu'))
						model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
						model.add(Dropout(0.25))
						
						model.add(Flatten())
						model.add(Dense(256, activation = "relu"))
						model.add(Dropout(0.5))
						model.add(Dense(10, activation = "softmax"))
						  

							# compile the model
							model.compile(optimizer='adam',
										loss='categorical_crossentropy',
										metrics=['accuracy'])
							early_stopping = EarlyStopping(min_delta=.001,
										restore_best_weights=True,
										patience=20,
										verbose=1)
						 

							# fit the model
							history = model.fit(x_train, y_train,
								epochs=1000,
								validation_data=(x_valid, y_valid),
								callbacks=[early_stopping])
						

						plt.plot(history.history['accuracy'])
						plt.plot(history.history['val_accuracy'])
						plt.title('model accuracy')
						plt.ylabel('accuracy')
						plt.xlabel('epoch')
						plt.legend(['train', 'validation'], loc='upper left')
						plt.show()
					  

						plt.plot(history.history['loss'])
						plt.plot(history.history['val_loss'])
						plt.title('model loss')
						plt.ylabel('loss')
						plt.xlabel('epoch')
						plt.legend(['train', 'validation'], loc='upper left')
						plt.show()
						  

Visualize

Go to visualization

Play

Go to playground