image_output_1.png
import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O

import random

import seaborn as sns

import tensorflow as tf

train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')

y_train = train['label']
X_train = train.drop('label', axis=1)

X_train = X_train / 255

y_train.nunique()

fig, ax = plt.subplots(5, 5, figsize=(8, 8))
fig.suptitle('Digits images and labels', fontsize=16)
ax = ax.ravel()
for i in range(25):
    sample_n = random.randint(0, X_train.shape[0])
    ax[i].imshow(np.array(X_train.iloc[sample_n]).reshape(28, 28), cmap='inferno')
    ax[i].get_xaxis().set_visible(False)
    ax[i].get_yaxis().set_visible(False)
    ax[i].set_title(y_train[sample_n], fontsize = 12)

plt.subplots_adjust(hspace=0.3)

plt.colorbar()

tf.random.set_seed(42)

model = tf.keras.Sequential([tf.keras.layers.Dense(units=784, activation='relu', input_shape=(784,)), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(units=392, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(units=151, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(units=50, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(units=10, activation='softmax')])

model.compile(optimizer='Adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

es = tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=0, mode='auto', baseline=None, restore_best_weights=False)

history = model.fit(X_train, y_train, epochs=100, validation_split=0.3, batch_size=50, callbacks=[es])