{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-23T05:31:42.456813Z","iopub.execute_input":"2024-07-23T05:31:42.457564Z","iopub.status.idle":"2024-07-23T05:31:43.447195Z","shell.execute_reply.started":"2024-07-23T05:31:42.457527Z","shell.execute_reply":"2024-07-23T05:31:43.446115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission=pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\n\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:12:32.644779Z","iopub.execute_input":"2024-07-23T06:12:32.645151Z","iopub.status.idle":"2024-07-23T06:12:32.668896Z","shell.execute_reply.started":"2024-07-23T06:12:32.645120Z","shell.execute_reply":"2024-07-23T06:12:32.667917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\n\ntrain_csv","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:12:37.543662Z","iopub.execute_input":"2024-07-23T06:12:37.544338Z","iopub.status.idle":"2024-07-23T06:12:40.154291Z","shell.execute_reply.started":"2024-07-23T06:12:37.544285Z","shell.execute_reply":"2024-07-23T06:12:40.153305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#now lets convert them into train and the test set\ndef convert_to_list(train_csv):\n    z=list(train_csv.columns)\n    final_list=[]\n    for i in range(len(train_csv)):\n        final1=[]\n        for j in train_csv.columns:\n            final1.append(train_csv[j][i])\n        final_list.append(final1)\n        \n    return final_list\n        ","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:12:43.708817Z","iopub.execute_input":"2024-07-23T06:12:43.709625Z","iopub.status.idle":"2024-07-23T06:12:43.714855Z","shell.execute_reply.started":"2024-07-23T06:12:43.709592Z","shell.execute_reply":"2024-07-23T06:12:43.713917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#convert_to_list(train_csv)\nz=convert_to_list(train_csv)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:12:47.147590Z","iopub.execute_input":"2024-07-23T06:12:47.148462Z","iopub.status.idle":"2024-07-23T06:18:15.343099Z","shell.execute_reply.started":"2024-07-23T06:12:47.148413Z","shell.execute_reply":"2024-07-23T06:18:15.342036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(z[1]))","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:18:55.481057Z","iopub.execute_input":"2024-07-23T06:18:55.481672Z","iopub.status.idle":"2024-07-23T06:18:55.486434Z","shell.execute_reply.started":"2024-07-23T06:18:55.481640Z","shell.execute_reply":"2024-07-23T06:18:55.485514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_train_list=z\n\nprint(final_train_list[2][0])","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:18:57.970899Z","iopub.execute_input":"2024-07-23T06:18:57.971836Z","iopub.status.idle":"2024-07-23T06:18:57.977165Z","shell.execute_reply.started":"2024-07-23T06:18:57.971794Z","shell.execute_reply":"2024-07-23T06:18:57.976021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(z[1][0])","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:19:00.569582Z","iopub.execute_input":"2024-07-23T06:19:00.570173Z","iopub.status.idle":"2024-07-23T06:19:00.574568Z","shell.execute_reply.started":"2024-07-23T06:19:00.570144Z","shell.execute_reply":"2024-07-23T06:19:00.573519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_list=[]\n\nfor i in range(42000):\n    m=final_train_list[i][0]\n    y_list.append(m)\n    \n    final_train_list[i]=final_train_list[i][1:]","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:19:03.092971Z","iopub.execute_input":"2024-07-23T06:19:03.093329Z","iopub.status.idle":"2024-07-23T06:19:03.380135Z","shell.execute_reply.started":"2024-07-23T06:19:03.093300Z","shell.execute_reply":"2024-07-23T06:19:03.379030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(y_list))\n\nprint(len(final_train_list[0]))","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:19:06.247246Z","iopub.execute_input":"2024-07-23T06:19:06.247611Z","iopub.status.idle":"2024-07-23T06:19:06.252663Z","shell.execute_reply.started":"2024-07-23T06:19:06.247582Z","shell.execute_reply":"2024-07-23T06:19:06.251709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\ntest_csv","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:19:08.745541Z","iopub.execute_input":"2024-07-23T06:19:08.745903Z","iopub.status.idle":"2024-07-23T06:19:10.845644Z","shell.execute_reply.started":"2024-07-23T06:19:08.745874Z","shell.execute_reply":"2024-07-23T06:19:10.844644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for z in train_csv.columns:\n    if z not in test_csv.columns:\n        print(z)","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:19:36.695058Z","iopub.execute_input":"2024-07-23T06:19:36.695403Z","iopub.status.idle":"2024-07-23T06:19:36.702090Z","shell.execute_reply.started":"2024-07-23T06:19:36.695375Z","shell.execute_reply":"2024-07-23T06:19:36.701057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#now lets do the same for the test set\nimport math\nfinal_test_list=[]\nfor i in range(len(test_csv)):\n    final_test1=[]\n    for j in test_csv.columns:\n        final_test1.append(test_csv[j][i])\n    final_test_list.append(final_test1)\n\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:19:39.301669Z","iopub.execute_input":"2024-07-23T06:19:39.302019Z","iopub.status.idle":"2024-07-23T06:23:21.639137Z","shell.execute_reply.started":"2024-07-23T06:19:39.301991Z","shell.execute_reply":"2024-07-23T06:23:21.638118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(final_test_list[1][2])","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:23:55.905506Z","iopub.execute_input":"2024-07-23T06:23:55.906359Z","iopub.status.idle":"2024-07-23T06:23:55.911028Z","shell.execute_reply.started":"2024-07-23T06:23:55.906327Z","shell.execute_reply":"2024-07-23T06:23:55.910020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(final_test_list))","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:23:58.197735Z","iopub.execute_input":"2024-07-23T06:23:58.198505Z","iopub.status.idle":"2024-07-23T06:23:58.203058Z","shell.execute_reply.started":"2024-07-23T06:23:58.198428Z","shell.execute_reply":"2024-07-23T06:23:58.202130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#now lets import the dependencies to train the model \nimport tensorflow as tf\nfrom tensorflow import keras \nfrom tensorflow.keras import layers \nfrom tensorflow.keras.layers import *\nfrom keras.utils import plot_model\nfrom tensorflow.keras.losses import MeanSquaredError\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.models import Model\n","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:24:00.871201Z","iopub.execute_input":"2024-07-23T06:24:00.871593Z","iopub.status.idle":"2024-07-23T06:24:00.877259Z","shell.execute_reply.started":"2024-07-23T06:24:00.871563Z","shell.execute_reply":"2024-07-23T06:24:00.876249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#now we create the model \ninput1=Input((784,))\n\ndense_layer1=Dense(724,activation='leaky_relu')(input1)\ndense_layer2=Dense(524,activation='leaky_relu')(dense_layer1)\ndropout=Dropout(0.2)(dense_layer2)\ndense_layer4=Dense(256,activation='leaky_relu')(dropout)\ndense_layer8=Dense(64,activation='leaky_relu')(dense_layer4)\ndense_layer9=Dense(32,activation='leaky_relu')(dense_layer8)\ndense_layer10=Dense(16,activation='leaky_relu')(dense_layer9)\noutput=Dense(9,activation='softmax')(dense_layer10)\n\noutput1=Dense(1,activation='linear')(output)\n\n#now lets wrap the model \ndense_layer_model=Model(inputs=input1,outputs=output1)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:24:03.926903Z","iopub.execute_input":"2024-07-23T06:24:03.927719Z","iopub.status.idle":"2024-07-23T06:24:03.997896Z","shell.execute_reply.started":"2024-07-23T06:24:03.927684Z","shell.execute_reply":"2024-07-23T06:24:03.996941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"dense_layer_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:24:07.217923Z","iopub.execute_input":"2024-07-23T06:24:07.218739Z","iopub.status.idle":"2024-07-23T06:24:07.242248Z","shell.execute_reply.started":"2024-07-23T06:24:07.218709Z","shell.execute_reply":"2024-07-23T06:24:07.241427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(dense_layer_model,show_layer_names=True,show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:24:11.020339Z","iopub.execute_input":"2024-07-23T06:24:11.020703Z","iopub.status.idle":"2024-07-23T06:24:11.394101Z","shell.execute_reply.started":"2024-07-23T06:24:11.020675Z","shell.execute_reply":"2024-07-23T06:24:11.393114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nfrom sklearn.preprocessing import StandardScaler\nfrom tensorflow.keras.losses import SparseCategoricalCrossentropy\nscaler=StandardScaler()\n\nfinal_train_list=scaler.fit_transform(final_train_list)\n'''","metadata":{"execution":{"iopub.status.busy":"2024-07-23T04:45:51.512433Z","iopub.execute_input":"2024-07-23T04:45:51.512800Z","iopub.status.idle":"2024-07-23T04:45:51.519713Z","shell.execute_reply.started":"2024-07-23T04:45:51.512772Z","shell.execute_reply":"2024-07-23T04:45:51.518804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-07-23T04:45:56.969030Z","iopub.execute_input":"2024-07-23T04:45:56.969420Z","iopub.status.idle":"2024-07-23T04:45:56.979931Z","shell.execute_reply.started":"2024-07-23T04:45:56.969389Z","shell.execute_reply":"2024-07-23T04:45:56.979162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_train_list=np.asarray(final_train_list)\ny_list=np.asarray(y_list)\n\nfinal_test_list=np.asarray(final_test_list)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:24:17.087878Z","iopub.execute_input":"2024-07-23T06:24:17.088587Z","iopub.status.idle":"2024-07-23T06:24:22.472306Z","shell.execute_reply.started":"2024-07-23T06:24:17.088555Z","shell.execute_reply":"2024-07-23T06:24:22.471210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(final_train_list.shape)\nprint(y_list.shape)\nprint(final_test_list.shape)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:24:26.646155Z","iopub.execute_input":"2024-07-23T06:24:26.646826Z","iopub.status.idle":"2024-07-23T06:24:26.651760Z","shell.execute_reply.started":"2024-07-23T06:24:26.646794Z","shell.execute_reply":"2024-07-23T06:24:26.650781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(final_test_list[0]))","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:24:30.824832Z","iopub.execute_input":"2024-07-23T06:24:30.825481Z","iopub.status.idle":"2024-07-23T06:24:30.830341Z","shell.execute_reply.started":"2024-07-23T06:24:30.825428Z","shell.execute_reply":"2024-07-23T06:24:30.829295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Softmax, LeakyReLU\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import SparseCategoricalCrossentropy\nimport tensorflow as tf\n\n# Define model\nnum_classes = 10  # Adjust as per your dataset\ndense_layer_model = Sequential([\n    Dense(128, input_shape=(final_train_list.shape[1],)),\n    LeakyReLU(alpha=0.01),\n    Dense(100),\n    LeakyReLU(alpha=0.01),  # LeakyReLU activation\n    Dropout(0.5),\n    Dense(64),\n    LeakyReLU(alpha=0.01),\n    Dense(32),\n    LeakyReLU(alpha=0.01),  # LeakyReLU activation\n    Dense(16),\n    LeakyReLU(alpha=0.01),\n    Dropout(0.5),\n    Dense(num_classes),\n    Softmax()\n])\n\n# Ensure numerical stability\nepsilon = 1e-7\n\ndef stable_sparse_categorical_crossentropy(y_true, y_pred):\n    y_pred = tf.clip_by_value(y_pred, epsilon, 1 - epsilon)\n    return SparseCategoricalCrossentropy()(y_true, y_pred)\n\n# Compile model with reduced learning rate and gradient clipping\noptimizer = Adam(learning_rate=0.00001, clipnorm=1.0)\ndense_layer_model.compile(optimizer=optimizer, loss=stable_sparse_categorical_crossentropy, metrics=['accuracy'], run_eagerly=True)\n\n# Train model\nhistory = dense_layer_model.fit(final_train_list, y_list, epochs=5, verbose=1, batch_size=100)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:24:35.035660Z","iopub.execute_input":"2024-07-23T06:24:35.036004Z","iopub.status.idle":"2024-07-23T06:27:56.070758Z","shell.execute_reply.started":"2024-07-23T06:24:35.035977Z","shell.execute_reply":"2024-07-23T06:27:56.069924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prediction = dense_layer_model.predict(final_test_list)\nprint(len(y_prediction))\n\n    ","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:28:02.654340Z","iopub.execute_input":"2024-07-23T06:28:02.655093Z","iopub.status.idle":"2024-07-23T06:28:11.179166Z","shell.execute_reply.started":"2024-07-23T06:28:02.655062Z","shell.execute_reply":"2024-07-23T06:28:11.178332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_prediction[1])","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:28:27.101377Z","iopub.execute_input":"2024-07-23T06:28:27.102232Z","iopub.status.idle":"2024-07-23T06:28:27.107390Z","shell.execute_reply.started":"2024-07-23T06:28:27.102201Z","shell.execute_reply":"2024-07-23T06:28:27.106366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prediction_single=[]\nfor i in range(len(y_prediction)):\n    y_prediction_single.append(np.argmax(y_prediction[i]))","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:28:30.040126Z","iopub.execute_input":"2024-07-23T06:28:30.040788Z","iopub.status.idle":"2024-07-23T06:28:30.141582Z","shell.execute_reply.started":"2024-07-23T06:28:30.040755Z","shell.execute_reply":"2024-07-23T06:28:30.140742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_prediction_single[1])","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:28:32.900067Z","iopub.execute_input":"2024-07-23T06:28:32.900772Z","iopub.status.idle":"2024-07-23T06:28:32.905683Z","shell.execute_reply.started":"2024-07-23T06:28:32.900739Z","shell.execute_reply":"2024-07-23T06:28:32.904600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#now lets create the datafram e\nimport numpy as np\nimport pandas as pd\n\n# Example data\nz = np.arange(1,28001)  # Assuming 28000 samples\n\n# Create a DataFrame\nsampled_output = pd.DataFrame({\n    \"ImageId\": z,\n    \"Label\": y_prediction_single\n})\n\nprint(sampled_output)\n\nsampled_output.to_csv(\"/kaggle/working/submission2.csv\", index=False)\n\n\nprint(\"Submission file saved to /kaggle/working/submission2.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-07-23T06:44:57.185290Z","iopub.execute_input":"2024-07-23T06:44:57.186084Z","iopub.status.idle":"2024-07-23T06:44:57.320466Z","shell.execute_reply.started":"2024-07-23T06:44:57.186050Z","shell.execute_reply":"2024-07-23T06:44:57.319526Z"},"trusted":true},"execution_count":null,"outputs":[]}]}