Created
September 9, 2021 19:42
-
-
Save chck/c2e11abf4d20067c804f097ba3498928 to your computer and use it in GitHub Desktop.
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## PyTorch 1.9 Training Sample w/ TensorBoard" | |
], | |
"metadata": { | |
"collapsed": false | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "'1.9.0'" | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import torch\n", | |
"torch.__version__" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from sklearn.datasets import load_iris\n", | |
"from sklearn.model_selection import train_test_split\n", | |
"from sklearn.preprocessing import StandardScaler\n", | |
"\n", | |
"iris = load_iris()\n", | |
"x = iris['data']\n", | |
"y = iris['target']\n", | |
"names = iris['target_names']\n", | |
"feature_names = iris['feature_names']\n", | |
"\n", | |
"scaler = StandardScaler()\n", | |
"x_scaled = scaler.fit_transform(x)\n", | |
"\n", | |
"x_train, x_test, y_train, y_test = train_test_split(x_scaled, y, test_size=0.2, random_state=2021)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "Model(\n (layer1): Linear(in_features=4, out_features=50, bias=True)\n (layer2): Linear(in_features=50, out_features=50, bias=True)\n (layer3): Linear(in_features=50, out_features=3, bias=True)\n)" | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from torch.nn import functional as F\n", | |
"from torch import nn\n", | |
"from torch.autograd import Variable\n", | |
"\n", | |
"class Model(nn.Module):\n", | |
" def __init__(self, input_dim):\n", | |
" super().__init__()\n", | |
" self.layer1 = nn.Linear(input_dim, 50)\n", | |
" self.layer2 = nn.Linear(50, 50)\n", | |
" self.layer3 = nn.Linear(50, 3)\n", | |
"\n", | |
" def forward(self, x):\n", | |
" x = F.relu(self.layer1(x))\n", | |
" x = F.relu(self.layer2(x))\n", | |
" x = F.softmax(self.layer3(x), dim=1)\n", | |
" return x\n", | |
"\n", | |
"model = Model(x_train.shape[1])\n", | |
"optimizer = torch.optim.Adam(model.parameters(), lr=0.001)\n", | |
"loss_fn = nn.CrossEntropyLoss()\n", | |
"model" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100%|██████████| 100/100 [00:00<00:00, 896.43it/s]\n" | |
] | |
} | |
], | |
"source": [ | |
"import tqdm\n", | |
"import numpy as np\n", | |
"from torch.utils.tensorboard import SummaryWriter\n", | |
"\n", | |
"writer = SummaryWriter(log_dir=\"/tmp/iris/exp1\")\n", | |
"\n", | |
"EPOCHS = 100\n", | |
"x_train = Variable(torch.from_numpy(x_train)).float()\n", | |
"y_train = Variable(torch.from_numpy(y_train)).long()\n", | |
"x_test = Variable(torch.from_numpy(x_test)).float()\n", | |
"y_test = Variable(torch.from_numpy(y_test)).long()\n", | |
"\n", | |
"loss_list = np.zeros((EPOCHS,))\n", | |
"accuracy_list = np.zeros((EPOCHS,))\n", | |
"\n", | |
"for epoch in tqdm.trange(EPOCHS):\n", | |
" # zero the parameter gradients\n", | |
" optimizer.zero_grad()\n", | |
"\n", | |
" # forward + backward + optimize\n", | |
" y_pred = model(x_train)\n", | |
" loss = loss_fn(y_pred, y_train)\n", | |
" loss.backward()\n", | |
" optimizer.step()\n", | |
" loss_list[epoch] = loss.item()\n", | |
" writer.add_scalar('Loss/train', loss.item(), epoch)\n", | |
"\n", | |
" with torch.no_grad():\n", | |
" y_pred = model(x_test)\n", | |
" correct = (torch.argmax(y_pred, dim=1) == y_test).type(torch.FloatTensor)\n", | |
" accuracy_list[epoch] = correct.mean()\n", | |
" writer.add_scalar('Accuracy/train', correct.mean(), epoch)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "Launching TensorBoard..." | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"%load_ext tensorboard\n", | |
"%tensorboard --logdir /tmp/iris/exp1" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Known TensorBoard instances:\n", | |
" - port 6006: logdir /tmp/iris/exp1 (started 0:00:01 ago; pid 55477)\n" | |
] | |
} | |
], | |
"source": [ | |
"from tensorboard import notebook\n", | |
"notebook.list()" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Selecting TensorBoard with logdir /tmp/iris/exp1 (started 0:00:02 ago; port 6006, pid 55477).\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": "<IPython.core.display.HTML object>", | |
"text/html": "\n <iframe id=\"tensorboard-frame-771db739d6bd12\" width=\"100%\" height=\"1000\" frameborder=\"0\">\n </iframe>\n <script>\n (function() {\n const frame = document.getElementById(\"tensorboard-frame-771db739d6bd12\");\n const url = new URL(\"/\", window.location);\n const port = 6006;\n if (port) {\n url.port = port;\n }\n frame.src = url;\n })();\n </script>\n " | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"notebook.display(port=6006, height=1000)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment