Created
September 24, 2021 17:34
-
-
Save sagorbrur/95a2c2370a18a1b3fae337dd06f65b9b to your computer and use it in GitHub Desktop.
simple_pytorch.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "simple_pytorch.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyNU1iA/twF8+dcyHE5G2TsM", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
}, | |
"accelerator": "GPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/sagorbrur/95a2c2370a18a1b3fae337dd06f65b9b/simple_pytorch.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Morez129upER" | |
}, | |
"source": [ | |
"## Import Necessaries" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "kfuKotgBsvnC" | |
}, | |
"source": [ | |
"import torch\n", | |
"import torch.nn as nn\n", | |
"import torch.optim as optim\n", | |
"from torch.utils.data import DataLoader" | |
], | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "NeHXixMHusPf" | |
}, | |
"source": [ | |
"## Set Device" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "JIBpIOtxs1en", | |
"outputId": "19c2ffa7-1e3d-4b5f-b4b0-c2343dd25aab" | |
}, | |
"source": [ | |
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", | |
"print(device)" | |
], | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"cuda\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "_HLHIGHzuuBo" | |
}, | |
"source": [ | |
"## Defince and Prepare Data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "Zkbe-vYCs5Xs", | |
"outputId": "abb4bdd1-841a-4008-943e-5e7510da76c6" | |
}, | |
"source": [ | |
"# device a sample trainset\n", | |
"traindata = torch.tensor([[1, 2, 3], [2, 3, 4], [5, 7, 4], [9, 1, 4]], dtype=torch.float32)\n", | |
"# map traindata in cuda device or cpu\n", | |
"traindata = traindata.to(device)\n", | |
"# device train label\n", | |
"trainlabel = torch.LongTensor([0, 1, 0, 1])\n", | |
"# map label to cuda or cpu\n", | |
"trainlabel = trainlabel.to(device)\n", | |
"print(traindata)\n", | |
"print(trainlabel)\n", | |
"\n", | |
"# define sample test set\n", | |
"testdata = torch.tensor([[1, 2, 3], [2, 3, 4], [5, 7, 4], [9, 1, 4]], dtype=torch.float32)\n", | |
"# map testdata in cuda device or cpu\n", | |
"testdata = testdata.to(device)\n", | |
"# device test label\n", | |
"testlabel = torch.LongTensor([0, 1, 0, 1])\n", | |
"# map label to cuda or cpu\n", | |
"testlabel = testlabel.to(device)\n", | |
"\n", | |
"# convert traindata and label to example\n", | |
"def generate_data_example(data, label):\n", | |
" examples = []\n", | |
" for d, l in zip(data, label):\n", | |
" data_set = (d, l)\n", | |
" examples.append(data_set)\n", | |
" return examples\n", | |
" \n", | |
"train_examples = generate_data_example(traindata, trainlabel)\n", | |
"test_examples = generate_data_example(testdata, testlabel)\n", | |
"\n", | |
"# fit in dataloader\n", | |
"batch_size = 1\n", | |
"train_dataloader = DataLoader(train_examples, batch_size=batch_size)\n", | |
"test_dataloader = DataLoader(test_examples, batch_size=batch_size)\n", | |
"print(f\"train data: {len(train_dataloader)} and test data: {len(test_dataloader)}\")" | |
], | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"tensor([[1., 2., 3.],\n", | |
" [2., 3., 4.],\n", | |
" [5., 7., 4.],\n", | |
" [9., 1., 4.]], device='cuda:0')\n", | |
"tensor([0, 1, 0, 1], device='cuda:0')\n", | |
"train data: 4 and test data: 4\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "LZL-CVMAuwqX" | |
}, | |
"source": [ | |
"## Define Model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "aTzsotOBtFj4", | |
"outputId": "2efba92c-8fbc-46f2-fa26-54a35bff6dde" | |
}, | |
"source": [ | |
"class Model(nn.Module):\n", | |
" def __init__(self, input_size, output_size):\n", | |
" super(Model, self).__init__()\n", | |
" self.input_size = input_size\n", | |
" self.output_size = output_size\n", | |
" self.linear = nn.Linear(input_size, output_size)\n", | |
"\n", | |
" def forward(self, x):\n", | |
" out = self.linear(x)\n", | |
" return out\n", | |
" \n", | |
"input_size = traindata.shape\n", | |
"print(input_size[1])\n", | |
"model = Model(input_size[1], 2)\n", | |
"model.to(device)\n", | |
"print(model)" | |
], | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"3\n", | |
"Model(\n", | |
" (linear): Linear(in_features=3, out_features=2, bias=True)\n", | |
")\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "ZNz9LHYGu0Ha" | |
}, | |
"source": [ | |
"## Define Loss" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "sd4CUF28tmIg" | |
}, | |
"source": [ | |
"criterion = nn.CrossEntropyLoss()" | |
], | |
"execution_count": 5, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "2aId5ff7u1u_" | |
}, | |
"source": [ | |
"## Define Optimizer" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "d4L9wP1Xto_B" | |
}, | |
"source": [ | |
"optimizer = optim.Adam(model.parameters(), lr=0.001)" | |
], | |
"execution_count": 6, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "o-KMlwNNu3iS" | |
}, | |
"source": [ | |
"## Define Train Function" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Qrpw2kUotsM4" | |
}, | |
"source": [ | |
"def train(dataloader, model, loss_fn, optimizer):\n", | |
" size = len(dataloader.dataset)\n", | |
" model.train()\n", | |
" for batch, (X, y) in enumerate(dataloader):\n", | |
" X, y = X.to(device), y.to(device)\n", | |
"\n", | |
" # Compute prediction error\n", | |
" pred = model(X)\n", | |
" loss = loss_fn(pred, y)\n", | |
"\n", | |
" # Backpropagation\n", | |
" optimizer.zero_grad()\n", | |
" loss.backward()\n", | |
" optimizer.step()\n", | |
"\n", | |
" if batch % 1 == 0:\n", | |
" loss, current = loss.item(), batch * len(X)\n", | |
" print(f\"loss: {loss:>7f} [{current:>5d}/{size:>5d}]\")" | |
], | |
"execution_count": 7, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "MdZDBavCu6f7" | |
}, | |
"source": [ | |
"## Define Test Function" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "scrbT9eUtzCf" | |
}, | |
"source": [ | |
"def test(dataloader, model, loss_fn):\n", | |
" size = len(dataloader.dataset)\n", | |
" num_batches = len(dataloader)\n", | |
" model.eval()\n", | |
" test_loss, correct = 0, 0\n", | |
" with torch.no_grad():\n", | |
" for X, y in dataloader:\n", | |
" X, y = X.to(device), y.to(device)\n", | |
" pred = model(X)\n", | |
" test_loss += loss_fn(pred, y).item()\n", | |
" correct += (pred.argmax(1) == y).type(torch.float).sum().item()\n", | |
" test_loss /= num_batches\n", | |
" correct /= size\n", | |
" print(f\"Test Error: \\n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \\n\")" | |
], | |
"execution_count": 8, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "SDKdp3Ymu8xQ" | |
}, | |
"source": [ | |
"## Train and Test" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "eiEF-pIRt1zq", | |
"outputId": "52df69df-051b-4926-bf61-4cd6757828d4" | |
}, | |
"source": [ | |
"epochs = 5\n", | |
"for t in range(epochs):\n", | |
" print(f\"Epoch {t+1}\\n-------------------------------\")\n", | |
" train(train_dataloader, model, criterion, optimizer)\n", | |
" test(test_dataloader, model, criterion)\n", | |
"print(\"Done!\")" | |
], | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Epoch 1\n", | |
"-------------------------------\n", | |
"loss: 0.468978 [ 0/ 4]\n", | |
"loss: 1.550761 [ 1/ 4]\n", | |
"loss: 0.021035 [ 2/ 4]\n", | |
"loss: 6.216814 [ 3/ 4]\n", | |
"Test Error: \n", | |
" Accuracy: 50.0%, Avg loss: 2.054861 \n", | |
"\n", | |
"Epoch 2\n", | |
"-------------------------------\n", | |
"loss: 0.471171 [ 0/ 4]\n", | |
"loss: 1.522439 [ 1/ 4]\n", | |
"loss: 0.022363 [ 2/ 4]\n", | |
"loss: 6.155248 [ 3/ 4]\n", | |
"Test Error: \n", | |
" Accuracy: 50.0%, Avg loss: 2.034846 \n", | |
"\n", | |
"Epoch 3\n", | |
"-------------------------------\n", | |
"loss: 0.481420 [ 0/ 4]\n", | |
"loss: 1.491253 [ 1/ 4]\n", | |
"loss: 0.023894 [ 2/ 4]\n", | |
"loss: 6.090882 [ 3/ 4]\n", | |
"Test Error: \n", | |
" Accuracy: 50.0%, Avg loss: 2.014044 \n", | |
"\n", | |
"Epoch 4\n", | |
"-------------------------------\n", | |
"loss: 0.492393 [ 0/ 4]\n", | |
"loss: 1.459543 [ 1/ 4]\n", | |
"loss: 0.025564 [ 2/ 4]\n", | |
"loss: 6.025702 [ 3/ 4]\n", | |
"Test Error: \n", | |
" Accuracy: 50.0%, Avg loss: 1.993133 \n", | |
"\n", | |
"Epoch 5\n", | |
"-------------------------------\n", | |
"loss: 0.503710 [ 0/ 4]\n", | |
"loss: 1.427929 [ 1/ 4]\n", | |
"loss: 0.027358 [ 2/ 4]\n", | |
"loss: 5.960329 [ 3/ 4]\n", | |
"Test Error: \n", | |
" Accuracy: 50.0%, Avg loss: 1.972324 \n", | |
"\n", | |
"Done!\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "rrJ2Izm3vAFv" | |
}, | |
"source": [ | |
"## Save Model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "7S3deFI5t4yR" | |
}, | |
"source": [ | |
"torch.save(model.state_dict(), 'model.pth')" | |
], | |
"execution_count": 10, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "8OGgoCxbvCQ2" | |
}, | |
"source": [ | |
"## Load and Inference" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "FnxQin0FuIV6", | |
"outputId": "8a6a6881-fa37-4c6a-8b6d-772be24e2ede" | |
}, | |
"source": [ | |
"load_model = model.load_state_dict(torch.load('model.pth'))\n", | |
"test_input = torch.tensor([[1, 2, 3]], dtype=torch.float32).to(device)\n", | |
"predict = model(test_input).data.max(1, keepdim=True)[1]\n", | |
"print(predict)" | |
], | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"tensor([[0]], device='cuda:0')\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "uALWJA6svEcG" | |
}, | |
"source": [ | |
"## Inspect Model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "inm_nhNZuLlI", | |
"outputId": "88b55d17-9935-4cd3-d4ec-294cfec832a7" | |
}, | |
"source": [ | |
"# inspect parameters\n", | |
"params = model.parameters()\n", | |
"for param in params:\n", | |
" print(param)\n", | |
"modules = model.named_modules()\n", | |
"for idx, m in enumerate(modules):\n", | |
" print(idx, m)" | |
], | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Parameter containing:\n", | |
"tensor([[ 0.1549, 0.1563, -0.2369],\n", | |
" [-0.5547, 0.0815, -0.1797]], device='cuda:0', requires_grad=True)\n", | |
"Parameter containing:\n", | |
"tensor([0.2673, 0.5606], device='cuda:0', requires_grad=True)\n", | |
"0 ('', Model(\n", | |
" (linear): Linear(in_features=3, out_features=2, bias=True)\n", | |
"))\n", | |
"1 ('linear', Linear(in_features=3, out_features=2, bias=True))\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "CFyZhzW9uekJ", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "d937a950-a270-47c9-a192-06e129305dc7" | |
}, | |
"source": [ | |
"# Print model's state_dict\n", | |
"print(\"Model's state_dict:\")\n", | |
"for param_tensor in model.state_dict():\n", | |
" print(param_tensor, \"\\t\", model.state_dict()[param_tensor].size())\n", | |
"\n", | |
"# Print optimizer's state_dict\n", | |
"print(\"Optimizer's state_dict:\")\n", | |
"for var_name in optimizer.state_dict():\n", | |
" print(var_name, \"\\t\", optimizer.state_dict()[var_name])" | |
], | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Model's state_dict:\n", | |
"linear.weight \t torch.Size([2, 3])\n", | |
"linear.bias \t torch.Size([2])\n", | |
"Optimizer's state_dict:\n", | |
"state \t {0: {'step': 20, 'exp_avg': tensor([[ 2.5106, 0.5475, 1.4160],\n", | |
" [-2.5106, -0.5475, -1.4160]], device='cuda:0'), 'exp_avg_sq': tensor([[0.4126, 0.0347, 0.1331],\n", | |
" [0.4126, 0.0347, 0.1331]], device='cuda:0')}, 1: {'step': 20, 'exp_avg': tensor([ 0.3360, -0.3360], device='cuda:0'), 'exp_avg_sq': tensor([0.0086, 0.0086], device='cuda:0')}}\n", | |
"param_groups \t [{'lr': 0.001, 'betas': (0.9, 0.999), 'eps': 1e-08, 'weight_decay': 0, 'amsgrad': False, 'params': [0, 1]}]\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "fsNDlygO3yV3" | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment