{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf \nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:18:40.793542Z","iopub.execute_input":"2024-10-27T13:18:40.794135Z","iopub.status.idle":"2024-10-27T13:18:55.182353Z","shell.execute_reply.started":"2024-10-27T13:18:40.794089Z","shell.execute_reply":"2024-10-27T13:18:55.181560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Tensorflow version \" + tf.__version__)\n\n# Detect and init the TPU\ntry:\n    # Detect TPUs and initialize TPUClusterResolver\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    # Initialize the TPU system\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.TPUStrategy(tpu)\nexcept (ValueError, RuntimeError):  # Detect GPUs\n    strategy = tf.distribute.MirroredStrategy()\n\nprint(\"Number of accelerators: \", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:18:55.184258Z","iopub.execute_input":"2024-10-27T13:18:55.185136Z","iopub.status.idle":"2024-10-27T13:18:56.219872Z","shell.execute_reply.started":"2024-10-27T13:18:55.185088Z","shell.execute_reply":"2024-10-27T13:18:56.218969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:18:56.221138Z","iopub.execute_input":"2024-10-27T13:18:56.221863Z","iopub.status.idle":"2024-10-27T13:19:01.536020Z","shell.execute_reply.started":"2024-10-27T13:18:56.221811Z","shell.execute_reply":"2024-10-27T13:19:01.534953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:01.538233Z","iopub.execute_input":"2024-10-27T13:19:01.538577Z","iopub.status.idle":"2024-10-27T13:19:01.567824Z","shell.execute_reply.started":"2024-10-27T13:19:01.538541Z","shell.execute_reply":"2024-10-27T13:19:01.566887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop('label', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:01.569007Z","iopub.execute_input":"2024-10-27T13:19:01.569293Z","iopub.status.idle":"2024-10-27T13:19:01.652655Z","shell.execute_reply.started":"2024-10-27T13:19:01.569262Z","shell.execute_reply":"2024-10-27T13:19:01.651635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['label']","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:01.653937Z","iopub.execute_input":"2024-10-27T13:19:01.654254Z","iopub.status.idle":"2024-10-27T13:19:01.659215Z","shell.execute_reply.started":"2024-10-27T13:19:01.654213Z","shell.execute_reply":"2024-10-27T13:19:01.658064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ANN = keras.Sequential([\n    layers.Input(shape=(784, )),\n    layers.Dense(1568, activation='relu'),\n    layers.Dense(1960, activation='relu'),\n    layers.Dense(1568, activation='relu'),\n    layers.Dense(784, activation='relu'),\n    layers.Dense(196, activation='relu'),\n    layers.Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:01.660569Z","iopub.execute_input":"2024-10-27T13:19:01.660977Z","iopub.status.idle":"2024-10-27T13:19:01.749215Z","shell.execute_reply.started":"2024-10-27T13:19:01.660928Z","shell.execute_reply":"2024-10-27T13:19:01.748519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ANN.summary()","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:01.750377Z","iopub.execute_input":"2024-10-27T13:19:01.751043Z","iopub.status.idle":"2024-10-27T13:19:01.771304Z","shell.execute_reply.started":"2024-10-27T13:19:01.750999Z","shell.execute_reply":"2024-10-27T13:19:01.770468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ANN.compile(\n    optimizer='adam',\n    metrics=['accuracy'],\n    loss='sparse_categorical_crossentropy'\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:01.772520Z","iopub.execute_input":"2024-10-27T13:19:01.773173Z","iopub.status.idle":"2024-10-27T13:19:01.785444Z","shell.execute_reply.started":"2024-10-27T13:19:01.773130Z","shell.execute_reply":"2024-10-27T13:19:01.784777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(\n    patience=10,\n    min_delta = 0.001,\n    restore_best_weights = True\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:01.788850Z","iopub.execute_input":"2024-10-27T13:19:01.789183Z","iopub.status.idle":"2024-10-27T13:19:01.793164Z","shell.execute_reply.started":"2024-10-27T13:19:01.789153Z","shell.execute_reply":"2024-10-27T13:19:01.792277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model_ANN.fit(X, y,\n    validation_split=0.25,\n    batch_size=train.shape[0]//40,\n    epochs=40,\n    callbacks=early_stopping,\n    verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:01.794371Z","iopub.execute_input":"2024-10-27T13:19:01.794745Z","iopub.status.idle":"2024-10-27T13:19:16.323231Z","shell.execute_reply.started":"2024-10-27T13:19:01.794701Z","shell.execute_reply":"2024-10-27T13:19:16.322422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:16.324554Z","iopub.execute_input":"2024-10-27T13:19:16.324902Z","iopub.status.idle":"2024-10-27T13:19:16.614653Z","shell.execute_reply.started":"2024-10-27T13:19:16.324868Z","shell.execute_reply":"2024-10-27T13:19:16.613726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_annd = model_ANN.predict(test)","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:16.615954Z","iopub.execute_input":"2024-10-27T13:19:16.616257Z","iopub.status.idle":"2024-10-27T13:19:18.933276Z","shell.execute_reply.started":"2024-10-27T13:19:16.616224Z","shell.execute_reply":"2024-10-27T13:19:18.932436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_annd[0]","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:18.934491Z","iopub.execute_input":"2024-10-27T13:19:18.934914Z","iopub.status.idle":"2024-10-27T13:19:18.941064Z","shell.execute_reply.started":"2024-10-27T13:19:18.934878Z","shell.execute_reply":"2024-10-27T13:19:18.940198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_annd = np.argmax(preds_annd, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:18.943051Z","iopub.execute_input":"2024-10-27T13:19:18.943381Z","iopub.status.idle":"2024-10-27T13:19:18.952316Z","shell.execute_reply.started":"2024-10-27T13:19:18.943323Z","shell.execute_reply":"2024-10-27T13:19:18.951348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_annd[0]","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:18.953422Z","iopub.execute_input":"2024-10-27T13:19:18.954059Z","iopub.status.idle":"2024-10-27T13:19:18.960982Z","shell.execute_reply.started":"2024-10-27T13:19:18.954026Z","shell.execute_reply":"2024-10-27T13:19:18.960057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ImageId': [i for i in range(1, len(labels_annd) + 1)],\n    'label': labels_annd\n})","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:19:58.160714Z","iopub.execute_input":"2024-10-27T13:19:58.161067Z","iopub.status.idle":"2024-10-27T13:19:58.180580Z","shell.execute_reply.started":"2024-10-27T13:19:58.161034Z","shell.execute_reply":"2024-10-27T13:19:58.179324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-27T13:20:00.237027Z","iopub.execute_input":"2024-10-27T13:20:00.237514Z","iopub.status.idle":"2024-10-27T13:20:00.276937Z","shell.execute_reply.started":"2024-10-27T13:20:00.237462Z","shell.execute_reply":"2024-10-27T13:20:00.276123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}