{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2023-10-02T03:17:32.942535Z","iopub.execute_input":"2023-10-02T03:17:32.942956Z","iopub.status.idle":"2023-10-02T03:17:32.951515Z","shell.execute_reply.started":"2023-10-02T03:17:32.942917Z","shell.execute_reply":"2023-10-02T03:17:32.950474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import DataLoader, Dataset\nimport torchvision\nimport torchvision.transforms as transforms\nfrom torch.utils.data.sampler import SubsetRandomSampler\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:17:32.953353Z","iopub.execute_input":"2023-10-02T03:17:32.954376Z","iopub.status.idle":"2023-10-02T03:17:32.962106Z","shell.execute_reply.started":"2023-10-02T03:17:32.954344Z","shell.execute_reply":"2023-10-02T03:17:32.961176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TrainMNIST(Dataset):\n    \n    def __init__(self, file_path, transform=None):\n        self.data = pd.read_csv(file_path)\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.data)\n    \n    def __getitem__(self, index):\n        image = self.data.iloc[index, 1:].values.astype(np.uint8).reshape((28, 28, 1))\n        label = self.data.iloc[index, 0]\n        \n        if self.transform is not None:\n            image = self.transform(image)\n            \n        return image, label","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:17:32.963255Z","iopub.execute_input":"2023-10-02T03:17:32.964178Z","iopub.status.idle":"2023-10-02T03:17:32.971860Z","shell.execute_reply.started":"2023-10-02T03:17:32.964146Z","shell.execute_reply":"2023-10-02T03:17:32.970855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestMNIST(Dataset):    \n    def __init__(self, file_path, transform=None):\n        self.data = pd.read_csv(file_path)\n        self.transform = transform        \n    def __len__(self):\n        return len(self.data)  \n    def __getitem__(self, index):\n        image = self.data.iloc[index, 1:].values.astype(np.uint8).reshape((28, 28, 1))        \n        if self.transform is not None:\n            image = self.transform(image)\n            \n        return image","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:17:33.317299Z","iopub.execute_input":"2023-10-02T03:17:33.318104Z","iopub.status.idle":"2023-10-02T03:17:33.325032Z","shell.execute_reply.started":"2023-10-02T03:17:33.318067Z","shell.execute_reply":"2023-10-02T03:17:33.324200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = TrainMNIST('/kaggle/input/Kannada-MNIST/train.csv', transform=transforms.ToTensor())\ntest_data = TestMNIST('/kaggle/input/Kannada-MNIST/test.csv', transform=transforms.ToTensor())","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:17:33.326944Z","iopub.execute_input":"2023-10-02T03:17:33.327761Z","iopub.status.idle":"2023-10-02T03:17:36.010688Z","shell.execute_reply.started":"2023-10-02T03:17:33.327730Z","shell.execute_reply":"2023-10-02T03:17:36.009768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:17:36.011995Z","iopub.execute_input":"2023-10-02T03:17:36.012841Z","iopub.status.idle":"2023-10-02T03:17:38.305981Z","shell.execute_reply.started":"2023-10-02T03:17:36.012807Z","shell.execute_reply":"2023-10-02T03:17:38.305088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\ntest","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:17:38.307371Z","iopub.execute_input":"2023-10-02T03:17:38.307940Z","iopub.status.idle":"2023-10-02T03:17:38.526599Z","shell.execute_reply.started":"2023-10-02T03:17:38.307903Z","shell.execute_reply":"2023-10-02T03:17:38.525687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_workers = 0\nbatch_size = 32\ntrain_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size,\n     num_workers=num_workers,)\ntest_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, \n    num_workers=num_workers)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:17:38.529457Z","iopub.execute_input":"2023-10-02T03:17:38.529765Z","iopub.status.idle":"2023-10-02T03:17:38.534415Z","shell.execute_reply.started":"2023-10-02T03:17:38.529741Z","shell.execute_reply":"2023-10-02T03:17:38.533444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\ndataiter = iter(train_loader)\nimages, labels = next(dataiter)\nimages = images.numpy()\nfig = plt.figure(figsize=(25, 4))\nfor idx in np.arange(20):\n    ax = fig.add_subplot(2,10, idx+1, xticks=[], yticks=[])\n    ax.imshow(np.squeeze(images[idx]), cmap='gray')\n    ax.set_title(str(labels[idx].item()))","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:17:38.535729Z","iopub.execute_input":"2023-10-02T03:17:38.536298Z","iopub.status.idle":"2023-10-02T03:17:39.702987Z","shell.execute_reply.started":"2023-10-02T03:17:38.536267Z","shell.execute_reply":"2023-10-02T03:17:39.702082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import 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        hidden_1 = 512\n        hidden_2 = 512\n        self.fc1 = nn.Linear(28 * 28, hidden_1)\n        self.fc2 = nn.Linear(hidden_1, hidden_2)\n        self.fc3 = nn.Linear(hidden_2, 10)\n\n\n    def forward(self, x):\n        x = x.view(-1, 28 * 28)\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = self.fc3(x)\n        return x\n\nmodel = Net()\nprint(model)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:17:39.706623Z","iopub.execute_input":"2023-10-02T03:17:39.708636Z","iopub.status.idle":"2023-10-02T03:17:39.731259Z","shell.execute_reply.started":"2023-10-02T03:17:39.708603Z","shell.execute_reply":"2023-10-02T03:17:39.730553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_parameters = model.parameters()  # Get model parameters\n\n# Check the device of each model parameter\ni=0\nfor param in model_parameters:\n    print(param, param.device)\n    i+=1\nprint(\"numeber of param: \", i)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:17:39.734796Z","iopub.execute_input":"2023-10-02T03:17:39.736724Z","iopub.status.idle":"2023-10-02T03:17:39.765995Z","shell.execute_reply.started":"2023-10-02T03:17:39.736692Z","shell.execute_reply":"2023-10-02T03:17:39.765267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Access and print the weights and biases for each layer\nfor name, param in model.named_parameters():\n    if 'weight' in name:\n        print(f'Layer: {name}, Weights: {param}')\n    elif 'bias' in name:\n        print(f'Layer: {name}, Biases: {param}')","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:17:39.769789Z","iopub.execute_input":"2023-10-02T03:17:39.771724Z","iopub.status.idle":"2023-10-02T03:17:39.798237Z","shell.execute_reply.started":"2023-10-02T03:17:39.771693Z","shell.execute_reply":"2023-10-02T03:17:39.797535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# specify loss function (categorical cross-entropy)\ncriterion = nn.CrossEntropyLoss()\n\n# specify optimizer (stochastic gradient descent) and learning rate = 0.001\noptimizer = torch.optim.Adam(model.parameters(), lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:17:39.802274Z","iopub.execute_input":"2023-10-02T03:17:39.804205Z","iopub.status.idle":"2023-10-02T03:17:39.810157Z","shell.execute_reply.started":"2023-10-02T03:17:39.804175Z","shell.execute_reply":"2023-10-02T03:17:39.809339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_epochs = 20\ntrain_loss_min = np.Inf # set initial \"min\" to infinity\ntrain_losses = []\nepochs = []\nfor epoch in range(n_epochs):\n    train_loss = 0.0\n\n    model.train() # prep model for training\n    for data, target in train_loader:\n        # clear the gradients of all optimized variables\n        optimizer.zero_grad()\n        # forward pass: compute predicted outputs by passing inputs to the model\n        output = model(data)\n        # calculate the loss\n        loss = criterion(output, target)\n        # backward pass: compute gradient of the loss with respect to model parameters\n        loss.backward()\n        # perform a single optimization step (parameter update)\n        optimizer.step()\n        # update running training loss\n        train_loss += loss.item()*data.size(0)\n        \n   \n    train_loss = train_loss/len(train_loader.dataset)\n    \n    print('Epoch: {} \\tTraining Loss: {:.6f}'.format(\n        epoch+1, \n        train_loss,\n        ))\n    # Append training loss and epoch values for plotting\n    train_losses.append(train_loss)\n    epochs.append(epoch + 1)\n    # save model if validation loss has decreased\n    if train_loss <= train_loss_min:\n        print('Training loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(\n        train_loss_min,\n        train_loss))\n        torch.save(model.state_dict(), 'model.pt')\n        train_loss_min = train_loss\n# Plotting the smoothed training loss vs. epoch\nplt.figure(figsize=(10, 6))\nplt.plot(epochs, train_losses, label='Training Loss', marker='o', linestyle='-')\nplt.title('Smoothed Training Loss vs. Epoch')\nplt.xlabel('Epoch')\nplt.ylabel('Training Loss')\nplt.grid(True)\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:17:39.814023Z","iopub.execute_input":"2023-10-02T03:17:39.816040Z","iopub.status.idle":"2023-10-02T03:27:32.511957Z","shell.execute_reply.started":"2023-10-02T03:17:39.816010Z","shell.execute_reply":"2023-10-02T03:27:32.511015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_state_dict(torch.load('model.pt'))","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:27:32.513164Z","iopub.execute_input":"2023-10-02T03:27:32.514104Z","iopub.status.idle":"2023-10-02T03:27:32.522917Z","shell.execute_reply.started":"2023-10-02T03:27:32.514067Z","shell.execute_reply":"2023-10-02T03:27:32.522022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"iteration_count = 0\n\n# Iterate over the test data loader\nfor i, batch in enumerate(test_loader):\n    data = batch\n    outputs = model(data)\n    max_values, predicted = torch.max(outputs.data, 1) # hoặc outputs ko cần .data\n    print(\"outputs.data\", outputs.data) # batch của test_loader là 20, do đó có 20 dòng\n    print(\"max_values: \", max_values)\n    print(\"predicted: \" , predicted)\n    \n    # Increment the counter\n    iteration_count += 1\n    \n    # Check if the counter has reached 5, and if so, break out of the loop\n    if iteration_count >= 5:\n        break","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:27:32.524430Z","iopub.execute_input":"2023-10-02T03:27:32.525150Z","iopub.status.idle":"2023-10-02T03:27:32.576065Z","shell.execute_reply.started":"2023-10-02T03:27:32.525114Z","shell.execute_reply":"2023-10-02T03:27:32.575267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# obtain one batch of test images\ndataiter = iter(test_loader)\nimages = next(dataiter)\n\n# get sample outputs\noutput = model(images)\n# convert output probabilities to predicted class\n_, preds = torch.max(output, 1)\n# prep images for display\nimages = images.numpy()\n\n# plot the images in the batch, along with predicted and true labels\nfig = plt.figure(figsize=(25, 4))\nfor idx in np.arange(20):\n    ax = fig.add_subplot(2, 10, idx+1, xticks=[], yticks=[])\n    ax.imshow(np.squeeze(images[idx]), cmap='gray')\n    ax.set_title(\"{}\".format(str(preds[idx].item())))","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:27:32.579539Z","iopub.execute_input":"2023-10-02T03:27:32.579891Z","iopub.status.idle":"2023-10-02T03:27:33.442717Z","shell.execute_reply.started":"2023-10-02T03:27:32.579870Z","shell.execute_reply":"2023-10-02T03:27:33.441730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_loader.dataset)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:27:33.444053Z","iopub.execute_input":"2023-10-02T03:27:33.444900Z","iopub.status.idle":"2023-10-02T03:27:33.451452Z","shell.execute_reply.started":"2023-10-02T03:27:33.444864Z","shell.execute_reply":"2023-10-02T03:27:33.450417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loader = torch.utils.data.DataLoader(test_data, batch_size=1, \n    num_workers=num_workers)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:27:33.453093Z","iopub.execute_input":"2023-10-02T03:27:33.453781Z","iopub.status.idle":"2023-10-02T03:27:33.460883Z","shell.execute_reply.started":"2023-10-02T03:27:33.453752Z","shell.execute_reply":"2023-10-02T03:27:33.459957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"iteration_count = 0\n\n# Iterate over the test data loader\nfor i, batch in enumerate(test_loader):\n    data = batch\n    outputs = model(data)\n    max_values, predicted = torch.max(outputs.data, 1) # hoặc outputs ko cần .data\n    print(\"outputs.data\", outputs.data) # batch của test_loader là 20, do đó có 20 dòng\n    print(\"max_values: \", max_values)\n    print(\"predicted: \" , predicted)\n    \n    # Increment the counter\n    iteration_count += 1\n    \n    # Check if the counter has reached 5, and if so, break out of the loop\n    if iteration_count >= 5:\n        break","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:27:33.462273Z","iopub.execute_input":"2023-10-02T03:27:33.462886Z","iopub.status.idle":"2023-10-02T03:27:33.481405Z","shell.execute_reply.started":"2023-10-02T03:27:33.462856Z","shell.execute_reply":"2023-10-02T03:27:33.480480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(model, dataloader):\n    prediction_list = []\n    for i, batch in enumerate(dataloader):\n        outputs = model(batch)\n        _, predicted = torch.max(outputs.data, 1) \n        prediction_list.append(predicted.cpu())\n    return prediction_list","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:27:33.482451Z","iopub.execute_input":"2023-10-02T03:27:33.482958Z","iopub.status.idle":"2023-10-02T03:27:33.488071Z","shell.execute_reply.started":"2023-10-02T03:27:33.482918Z","shell.execute_reply":"2023-10-02T03:27:33.487199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = predict(model,test_loader)\npredictions = np.array(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:27:33.489466Z","iopub.execute_input":"2023-10-02T03:27:33.490074Z","iopub.status.idle":"2023-10-02T03:27:36.569680Z","shell.execute_reply.started":"2023-10-02T03:27:33.490044Z","shell.execute_reply":"2023-10-02T03:27:36.568729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_values = [tensor.item() for tensor in predictions]\n\n# Create a NumPy array from the list of integer values\npredicted_array = np.array(predicted_values)\n\n# Now, 'predicted_array' contains the integer values without truncation\npredicted_array","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:27:36.571350Z","iopub.execute_input":"2023-10-02T03:27:36.572218Z","iopub.status.idle":"2023-10-02T03:27:36.582256Z","shell.execute_reply.started":"2023-10-02T03:27:36.572181Z","shell.execute_reply":"2023-10-02T03:27:36.581139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\")\n\n\nsubmission = pd.DataFrame(data={\n    \"id\": test_data.index,\n    \"label\": predicted_array\n})\nsubmission.to_csv(\"submission.csv\", index=None)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:27:36.584061Z","iopub.execute_input":"2023-10-02T03:27:36.584716Z","iopub.status.idle":"2023-10-02T03:27:36.807280Z","shell.execute_reply.started":"2023-10-02T03:27:36.584681Z","shell.execute_reply":"2023-10-02T03:27:36.806359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.remove(\"/kaggle/working/submission.csv\")\n# os.remove(\"/kaggle/working/model.pt\")","metadata":{"execution":{"iopub.status.busy":"2023-10-02T03:27:36.808525Z","iopub.execute_input":"2023-10-02T03:27:36.809387Z","iopub.status.idle":"2023-10-02T03:27:36.813269Z","shell.execute_reply.started":"2023-10-02T03:27:36.809354Z","shell.execute_reply":"2023-10-02T03:27:36.812444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}