{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-05T11:44:29.861688Z","iopub.execute_input":"2024-07-05T11:44:29.862162Z","iopub.status.idle":"2024-07-05T11:44:29.868465Z","shell.execute_reply.started":"2024-07-05T11:44:29.862129Z","shell.execute_reply":"2024-07-05T11:44:29.867225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path(\"/kaggle/input/digit-recognizer\")\npath.ls()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:44:29.965943Z","iopub.execute_input":"2024-07-05T11:44:29.966358Z","iopub.status.idle":"2024-07-05T11:44:29.986167Z","shell.execute_reply.started":"2024-07-05T11:44:29.966328Z","shell.execute_reply":"2024-07-05T11:44:29.984938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:44:30.123658Z","iopub.execute_input":"2024-07-05T11:44:30.124063Z","iopub.status.idle":"2024-07-05T11:44:32.847030Z","shell.execute_reply.started":"2024-07-05T11:44:30.124031Z","shell.execute_reply":"2024-07-05T11:44:32.845790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tst_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntst_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:44:32.849291Z","iopub.execute_input":"2024-07-05T11:44:32.849756Z","iopub.status.idle":"2024-07-05T11:44:35.354938Z","shell.execute_reply.started":"2024-07-05T11:44:32.849717Z","shell.execute_reply":"2024-07-05T11:44:35.353651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"unique number of dependent variables is {df['label'].nunique()}\")\nprint(f\"unique dependent variables are {sorted(df['label'].unique())}\")","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:44:35.356330Z","iopub.execute_input":"2024-07-05T11:44:35.356682Z","iopub.status.idle":"2024-07-05T11:44:35.363790Z","shell.execute_reply.started":"2024-07-05T11:44:35.356653Z","shell.execute_reply":"2024-07-05T11:44:35.362718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(df.iloc[:10, 1:].values.reshape(-1, 28, 28), titles=df.iloc[:10, 0].values);","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:44:35.366606Z","iopub.execute_input":"2024-07-05T11:44:35.367084Z","iopub.status.idle":"2024-07-05T11:44:36.233750Z","shell.execute_reply.started":"2024-07-05T11:44:35.367045Z","shell.execute_reply":"2024-07-05T11:44:36.232671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ndef split_df(df, test_size=0.2):\n    return train_test_split(df, test_size=test_size)\n\ntrn_df, val_df = split_df(df)\nprint(f\"training set shape  : {trn_df.shape}\")\nprint(f\"validation set shape: {val_df.shape}\")\nprint(f\"test set shape      : {tst_df.shape}\")","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:44:36.235476Z","iopub.execute_input":"2024-07-05T11:44:36.235865Z","iopub.status.idle":"2024-07-05T11:44:36.480399Z","shell.execute_reply.started":"2024-07-05T11:44:36.235817Z","shell.execute_reply":"2024-07-05T11:44:36.479130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_x = tensor(trn_df.iloc[:, 1:].values).float() / 255.\ntrn_y = tensor(trn_df[\"label\"].values).unsqueeze(-1)\nval_x = tensor(val_df.iloc[:, 1:].values).float() / 255.\nval_y = tensor(val_df[\"label\"].values).unsqueeze(-1)\ntst_x = tensor(tst_df.values).float() / 255.\n\ntrn_x.shape, trn_y.shape, val_x.shape, val_y.shape, tst_x.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:44:36.482060Z","iopub.execute_input":"2024-07-05T11:44:36.482424Z","iopub.status.idle":"2024-07-05T11:44:36.696987Z","shell.execute_reply.started":"2024-07-05T11:44:36.482393Z","shell.execute_reply":"2024-07-05T11:44:36.695715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_dset = list(zip(trn_x, trn_y))\nval_dset = list(zip(val_x, val_y))\ntrn_x, trn_y = trn_dset[0]\ntrn_x.shape, trn_y.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:44:45.230534Z","iopub.execute_input":"2024-07-05T11:44:45.230998Z","iopub.status.idle":"2024-07-05T11:44:45.371747Z","shell.execute_reply.started":"2024-07-05T11:44:45.230961Z","shell.execute_reply":"2024-07-05T11:44:45.370427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_image(trn_x.view(28, 28), title=trn_y);","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:44:45.373736Z","iopub.execute_input":"2024-07-05T11:44:45.374122Z","iopub.status.idle":"2024-07-05T11:44:45.485096Z","shell.execute_reply.started":"2024-07-05T11:44:45.374091Z","shell.execute_reply":"2024-07-05T11:44:45.483566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bs = 256\ntrn_dl = DataLoader(trn_dset, batch_size=bs, shuffle=True)\nval_dl = DataLoader(val_dset, batch_size=bs)\ndls = DataLoaders(trn_dl, val_dl)\n\ntrn_xb, trn_yb = dls.train.one_batch()\nval_xb, val_yb = dls.valid.one_batch()\ntrn_xb.shape, trn_yb.shape, val_xb.shape, val_yb.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:44:48.910670Z","iopub.execute_input":"2024-07-05T11:44:48.911774Z","iopub.status.idle":"2024-07-05T11:44:48.958066Z","shell.execute_reply.started":"2024-07-05T11:44:48.911733Z","shell.execute_reply":"2024-07-05T11:44:48.956887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(trn_xb[:10].view(-1, 28, 28), titles=trn_yb[:10])","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:44:49.016170Z","iopub.execute_input":"2024-07-05T11:44:49.016570Z","iopub.status.idle":"2024-07-05T11:44:49.860515Z","shell.execute_reply.started":"2024-07-05T11:44:49.016536Z","shell.execute_reply":"2024-07-05T11:44:49.859308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_learner(opt_func, prt_sum=False, prt_lrf=False):    \n    learn = Learner(dls, nn.Linear(28*28, 10), opt_func=opt_func,\n                   loss_func=CrossEntropyLossFlat(), metrics=accuracy)\n    if prt_sum: print(learn.summary())\n    if prt_lrf: learn.lr_find()\n    return learn\n\nlearn = get_learner(opt_func=SGD, prt_sum=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:45:00.615056Z","iopub.execute_input":"2024-07-05T11:45:00.615526Z","iopub.status.idle":"2024-07-05T11:45:00.674711Z","shell.execute_reply.started":"2024-07-05T11:45:00.615491Z","shell.execute_reply":"2024-07-05T11:45:00.673395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(20, 3e-2)\nval_loss, acc = learn.validate()\nprint(f\"val loss: {val_loss}\\naccuracy: {acc}\")","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:45:08.317792Z","iopub.execute_input":"2024-07-05T11:45:08.318192Z","iopub.status.idle":"2024-07-05T11:45:36.352897Z","shell.execute_reply.started":"2024-07-05T11:45:08.318164Z","shell.execute_reply":"2024-07-05T11:45:36.351576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_weights(learner): \n    w, _ = learner.model.parameters()\n    # print(w.shape)\n    show_images(w.reshape(-1, 28, 28), suptitle=f\"{learner.opt_func.__name__} optimizer\");\n    \nshow_weights(learn)","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:45:40.354965Z","iopub.execute_input":"2024-07-05T11:45:40.355360Z","iopub.status.idle":"2024-07-05T11:45:40.806066Z","shell.execute_reply.started":"2024-07-05T11:45:40.355333Z","shell.execute_reply":"2024-07-05T11:45:40.804200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = get_learner(opt_func=RMSProp)\nlearn.fit_one_cycle(20, 1e-3)\nval_loss, acc = learn.validate()\nprint(f\"val loss: {val_loss}\\naccuracy: {acc}\")\nshow_weights(learn)","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:45:52.736249Z","iopub.execute_input":"2024-07-05T11:45:52.736641Z","iopub.status.idle":"2024-07-05T11:46:21.458757Z","shell.execute_reply.started":"2024-07-05T11:45:52.736611Z","shell.execute_reply":"2024-07-05T11:46:21.457466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = get_learner(opt_func=Adam)\nlearn.fit_one_cycle(20, 1e-2)\nval_loss, acc = learn.validate()\nprint(f\"val loss: {val_loss}\\naccuracy: {acc}\")\nshow_weights(learn)","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:46:30.949288Z","iopub.execute_input":"2024-07-05T11:46:30.949816Z","iopub.status.idle":"2024-07-05T11:46:59.808513Z","shell.execute_reply.started":"2024-07-05T11:46:30.949767Z","shell.execute_reply":"2024-07-05T11:46:59.807295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = learn.model(val_xb[:10]).max(dim=1)[1]\nshow_images(val_xb[:10].view(-1, 28, 28), titles=preds)","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:47:27.983350Z","iopub.execute_input":"2024-07-05T11:47:27.983821Z","iopub.status.idle":"2024-07-05T11:47:28.829570Z","shell.execute_reply.started":"2024-07-05T11:47:27.983781Z","shell.execute_reply":"2024-07-05T11:47:28.828332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsub_df.tail()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:47:28.831717Z","iopub.execute_input":"2024-07-05T11:47:28.832179Z","iopub.status.idle":"2024-07-05T11:47:28.859020Z","shell.execute_reply.started":"2024-07-05T11:47:28.832139Z","shell.execute_reply":"2024-07-05T11:47:28.857831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = learn.model(tst_x).max(dim=1)[1]\nsub_df[\"Label\"] = preds.numpy()\nsub_df.tail()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:47:29.276025Z","iopub.execute_input":"2024-07-05T11:47:29.276436Z","iopub.status.idle":"2024-07-05T11:47:29.313685Z","shell.execute_reply.started":"2024-07-05T11:47:29.276402Z","shell.execute_reply":"2024-07-05T11:47:29.312484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:47:30.097955Z","iopub.execute_input":"2024-07-05T11:47:30.098360Z","iopub.status.idle":"2024-07-05T11:47:30.129918Z","shell.execute_reply.started":"2024-07-05T11:47:30.098330Z","shell.execute_reply":"2024-07-05T11:47:30.128564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}