{"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"}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\nimport os\nimport platform\nimport sys\nimport random\n\n# linear algebra\nimport numpy as np\n# data processing, CSV file I/O (e.g. pd.read_csv)\nimport pandas as pd\n# plot\nimport matplotlib\nimport matplotlib.pyplot as plt\n# machine learning\nimport sklearn\n# deep learning\nimport torch\nimport torchvision","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-02-26T21:44:54.689600Z","iopub.execute_input":"2024-02-26T21:44:54.690571Z","iopub.status.idle":"2024-02-26T21:45:00.159551Z","shell.execute_reply.started":"2024-02-26T21:44:54.690535Z","shell.execute_reply":"2024-02-26T21:45:00.158170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Operating system: \", platform.platform(), platform.version())\nprint(\"Python version\", sys.version)\nprint(\"Pandas version: \", pd.__version__)\nprint(\"Matplotlib version: \", matplotlib.__version__)\nprint(\"Sklearn version: \", sklearn.__version__)\nprint(\"Pytorch version: \", torch.__version__)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-02-26T21:45:00.161480Z","iopub.execute_input":"2024-02-26T21:45:00.162426Z","iopub.status.idle":"2024-02-26T21:45:00.172260Z","shell.execute_reply.started":"2024-02-26T21:45:00.162335Z","shell.execute_reply":"2024-02-26T21:45:00.170863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reproducibility\nrandom.seed(0)\nnp.random.seed(0)\ntorch.manual_seed(0)\ntorch.use_deterministic_algorithms(True)","metadata":{"execution":{"iopub.status.busy":"2024-02-26T21:45:00.174412Z","iopub.execute_input":"2024-02-26T21:45:00.174842Z","iopub.status.idle":"2024-02-26T21:45:00.187466Z","shell.execute_reply.started":"2024-02-26T21:45:00.174809Z","shell.execute_reply":"2024-02-26T21:45:00.186075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if torch.cuda.is_available():\n    device = torch.device(\"cuda\")\n    print(torch.cuda.get_device_name(0))\n    print('Memory Usage:')\n    print('Allocated:', round(torch.cuda.memory_allocated(0)/1024**3,1), 'GB')\n    print('Cached:   ', round(torch.cuda.memory_reserved(0)/1024**3,1), 'GB')\nelif torch.backends.mps.is_available() and torch.backends.mps.is_built():\n    device = torch.device(\"mps\")\nelse:\n    device = torch.device(\"cpu\")\n\nprint('Using device:', device)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-02-26T21:45:00.191228Z","iopub.execute_input":"2024-02-26T21:45:00.191680Z","iopub.status.idle":"2024-02-26T21:45:00.200980Z","shell.execute_reply.started":"2024-02-26T21:45:00.191632Z","shell.execute_reply":"2024-02-26T21:45:00.199297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGET = \"label\"\nRANDOM_STATE = 0\nTRAIN_VS_VALIDATION_SET_PERCENTANGE = 0.8\nBATCH_SIZE = 16\nNUMBER_OF_EPOCHS = 14","metadata":{"execution":{"iopub.status.busy":"2024-02-26T21:45:00.202531Z","iopub.execute_input":"2024-02-26T21:45:00.202906Z","iopub.status.idle":"2024-02-26T21:45:00.216540Z","shell.execute_reply.started":"2024-02-26T21:45:00.202875Z","shell.execute_reply":"2024-02-26T21:45:00.215036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Input data files are available in the read-only \"../input/\" directory\ndataset_train_full_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\",dtype = np.float32)\nX_test_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\",dtype = np.float32)\n# Separate target from features\nX_train_full_df = dataset_train_full_df.drop([TARGET], axis=1)\ny_train_full_s = dataset_train_full_df[TARGET]","metadata":{"execution":{"iopub.status.busy":"2024-02-26T21:45:00.218120Z","iopub.execute_input":"2024-02-26T21:45:00.218512Z","iopub.status.idle":"2024-02-26T21:45:06.657007Z","shell.execute_reply.started":"2024-02-26T21:45:00.218465Z","shell.execute_reply":"2024-02-26T21:45:06.655939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean = 0.1307\nstd = 0.3081\nX_train_full_np = (X_train_full_df.to_numpy().reshape(-1, 1, 28, 28)/255-mean)/std\nX_train_full_tf = torch.from_numpy(X_train_full_np)\nX_test_np = (X_test_df.to_numpy().reshape(-1, 1, 28, 28)/255-mean)/std\nX_test_tf = torch.from_numpy(X_test_np)\ny_train_full_tf = torch.from_numpy(y_train_full_s.to_numpy(dtype=\"int\"))\ny_train_full_tf_onehot = torch.nn.functional.one_hot(y_train_full_tf).float()","metadata":{"execution":{"iopub.status.busy":"2024-02-26T21:45:06.658472Z","iopub.execute_input":"2024-02-26T21:45:06.658933Z","iopub.status.idle":"2024-02-26T21:45:06.829669Z","shell.execute_reply.started":"2024-02-26T21:45:06.658890Z","shell.execute_reply":"2024-02-26T21:45:06.828165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# Break off validation set from training data\nX_train_tf, X_valid_tf, y_train_tf_onehot, y_valid_tf_onehot = train_test_split(\n    X_train_full_tf, y_train_full_tf_onehot,\n    train_size=TRAIN_VS_VALIDATION_SET_PERCENTANGE,\n    # shuffle the dataset to ensure the traning set is representive of the overall distribution of the data,\n    # this would be useful e.g. the input is sorted by target label or specific feature.\n    shuffle=True,\n    random_state=RANDOM_STATE\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-26T21:45:06.831761Z","iopub.execute_input":"2024-02-26T21:45:06.832245Z","iopub.status.idle":"2024-02-26T21:45:07.214751Z","shell.execute_reply.started":"2024-02-26T21:45:06.832199Z","shell.execute_reply":"2024-02-26T21:45:07.213585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_img(img):\n    PIL_RGB_image = img.permute(1, 2, 0)\n    img = (PIL_RGB_image * std + mean)\n    plt.imshow(img, cmap=\"gray\", vmin=0, vmax=1)","metadata":{"execution":{"iopub.status.busy":"2024-02-26T21:45:07.216546Z","iopub.execute_input":"2024-02-26T21:45:07.217044Z","iopub.status.idle":"2024-02-26T21:45:07.223598Z","shell.execute_reply.started":"2024-02-26T21:45:07.216998Z","shell.execute_reply":"2024-02-26T21:45:07.222342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_img(X_train_tf[0])\nprint(y_train_tf_onehot[0].argmax())","metadata":{"execution":{"iopub.status.busy":"2024-02-26T21:45:07.225233Z","iopub.execute_input":"2024-02-26T21:45:07.226305Z","iopub.status.idle":"2024-02-26T21:45:07.461050Z","shell.execute_reply.started":"2024-02-26T21:45:07.226261Z","shell.execute_reply":"2024-02-26T21:45:07.459871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import TensorDataset, DataLoader\n\ntrain_dataset = TensorDataset(X_train_tf, y_train_tf_onehot)\nvalid_dataset = TensorDataset(X_valid_tf, y_valid_tf_onehot)\ng = torch.Generator()\ng.manual_seed(0)\ntrain_loader = DataLoader(train_dataset, batch_size = BATCH_SIZE, shuffle = False, generator=g)\nvalid_loader = DataLoader(valid_dataset, batch_size = BATCH_SIZE, shuffle = False, generator=g)","metadata":{"execution":{"iopub.status.busy":"2024-02-26T21:45:07.462437Z","iopub.execute_input":"2024-02-26T21:45:07.462849Z","iopub.status.idle":"2024-02-26T21:45:07.471419Z","shell.execute_reply.started":"2024-02-26T21:45:07.462816Z","shell.execute_reply":"2024-02-26T21:45:07.470317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://github.com/pytorch/examples/blob/main/mnist/main.py\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nclass Net(nn.Module):\n    def __init__(self):\n        super(Net, self).__init__()\n        self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=5, stride=1, padding=0)\n        self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=0)\n        self.dropout1 = nn.Dropout(0.25)\n        \n        self.fc1 = nn.Linear(7744, 128)\n        self.dropout2 = nn.Dropout(0.5)\n        self.fc2 = nn.Linear(128, 10)\n\n    def forward(self, x):\n        x = self.conv1(x)\n        x = F.relu(x)\n        x = self.conv2(x)\n        x = F.relu(x)\n        x = F.max_pool2d(x, 2)\n        x = self.dropout1(x)\n\n        x = torch.flatten(x, 1)\n        x = self.fc1(x)\n        x = F.relu(x)\n        x = self.dropout2(x)\n        x = self.fc2(x)\n        output = F.log_softmax(x, dim=1)\n        return output","metadata":{"execution":{"iopub.status.busy":"2024-02-26T21:45:07.473009Z","iopub.execute_input":"2024-02-26T21:45:07.473373Z","iopub.status.idle":"2024-02-26T21:45:07.488726Z","shell.execute_reply.started":"2024-02-26T21:45:07.473328Z","shell.execute_reply":"2024-02-26T21:45:07.487396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.optim as optim\n\nmodel = Net()\noptimizer = optim.SGD(model.parameters(), lr=0.1)\ncriterion = nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2024-02-26T21:45:07.493329Z","iopub.execute_input":"2024-02-26T21:45:07.494262Z","iopub.status.idle":"2024-02-26T21:45:07.521823Z","shell.execute_reply.started":"2024-02-26T21:45:07.494221Z","shell.execute_reply":"2024-02-26T21:45:07.520651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nloss_evolution = []\nfor epoch in range(NUMBER_OF_EPOCHS):\n    epoch_loss = 0\n    for batch_index, batch in enumerate(train_loader):\n        sample_images, sample_labels = batch\n        optimizer.zero_grad()\n        predicted_labels = model(sample_images)\n        loss = criterion(predicted_labels, sample_labels)\n        loss.backward()\n        optimizer.step()\n        epoch_loss += loss\n    print(epoch_loss)","metadata":{"execution":{"iopub.status.busy":"2024-02-26T21:45:07.523268Z","iopub.execute_input":"2024-02-26T21:45:07.523771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_pred_tf_onehot = model(X_test_tf)\ny_test_pred_tf = y_test_pred_tf_onehot.argmax(axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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\ndf = pd.DataFrame(y_test_pred_tf, columns = ['Label'])\ndf.index.name='ImageId'\ndf.index += 1\ndf.to_csv('/kaggle/working/results.csv', header=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}