A series of experiments with Julia and Python
0

Configure Feed

Select the types of activity you want to include in your feed.

Try MXNet and MXBoard on Windows

+730
+730
dl/hello_mx_gpu.ipynb
··· 1 + { 2 + "cells": [ 3 + { 4 + "cell_type": "markdown", 5 + "metadata": {}, 6 + "source": [ 7 + "It works on a Windows 10 PC which has an old GPU: GeForce GTX 550 Ti.\n", 8 + "\n", 9 + "Simply run the following command (not using docker) in the root of this project:\n", 10 + "\n", 11 + "```\n", 12 + "jupyter notebook\n", 13 + "```\n", 14 + "\n", 15 + "Also run the following in a separate terminal:\n", 16 + "\n", 17 + "```\n", 18 + "tensorboard --logdir=./dl/logs --host=127.0.0.1 --port=8889\n", 19 + "```" 20 + ] 21 + }, 22 + { 23 + "cell_type": "code", 24 + "execution_count": 1, 25 + "metadata": {}, 26 + "outputs": [ 27 + { 28 + "name": "stdout", 29 + "output_type": "stream", 30 + "text": [ 31 + "Requirement already satisfied: tensorflow-gpu in c:\\coding\\anaconda3\\lib\\site-packages (1.8.0)\n", 32 + "Requirement already satisfied: termcolor>=1.1.0 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorflow-gpu) (1.1.0)\n", 33 + "Requirement already satisfied: protobuf>=3.4.0 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorflow-gpu) (3.6.0)\n", 34 + "Requirement already satisfied: six>=1.10.0 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorflow-gpu) (1.11.0)\n", 35 + "Requirement already satisfied: absl-py>=0.1.6 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorflow-gpu) (0.2.2)\n", 36 + "Requirement already satisfied: astor>=0.6.0 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorflow-gpu) (0.6.2)\n", 37 + "Requirement already satisfied: wheel>=0.26 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorflow-gpu) (0.31.1)\n", 38 + "Requirement already satisfied: tensorboard<1.9.0,>=1.8.0 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorflow-gpu) (1.8.0)\n", 39 + "Requirement already satisfied: grpcio>=1.8.6 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorflow-gpu) (1.12.1)\n", 40 + "Requirement already satisfied: numpy>=1.13.3 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorflow-gpu) (1.14.3)\n", 41 + "Requirement already satisfied: gast>=0.2.0 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorflow-gpu) (0.2.0)\n", 42 + "Requirement already satisfied: setuptools in c:\\coding\\anaconda3\\lib\\site-packages (from protobuf>=3.4.0->tensorflow-gpu) (39.1.0)\n", 43 + "Requirement already satisfied: markdown>=2.6.8 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorboard<1.9.0,>=1.8.0->tensorflow-gpu) (2.6.11)\n", 44 + "Requirement already satisfied: werkzeug>=0.11.10 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorboard<1.9.0,>=1.8.0->tensorflow-gpu) (0.14.1)\n", 45 + "Requirement already satisfied: bleach==1.5.0 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorboard<1.9.0,>=1.8.0->tensorflow-gpu) (1.5.0)\n", 46 + "Requirement already satisfied: html5lib==0.9999999 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorboard<1.9.0,>=1.8.0->tensorflow-gpu) (0.9999999)\n" 47 + ] 48 + }, 49 + { 50 + "name": "stderr", 51 + "output_type": "stream", 52 + "text": [ 53 + "distributed 1.21.8 requires msgpack, which is not installed.\n" 54 + ] 55 + } 56 + ], 57 + "source": [ 58 + "!pip install tensorflow-gpu" 59 + ] 60 + }, 61 + { 62 + "cell_type": "code", 63 + "execution_count": 2, 64 + "metadata": {}, 65 + "outputs": [ 66 + { 67 + "name": "stdout", 68 + "output_type": "stream", 69 + "text": [ 70 + "env: CUDA_DEVICE_ORDER=PCI_BUS_ID\n", 71 + "env: CUDA_VISIBLE_DEVICES=0,1\n" 72 + ] 73 + } 74 + ], 75 + "source": [ 76 + "%env CUDA_DEVICE_ORDER=PCI_BUS_ID\n", 77 + "%env CUDA_VISIBLE_DEVICES=0,1" 78 + ] 79 + }, 80 + { 81 + "cell_type": "code", 82 + "execution_count": 3, 83 + "metadata": {}, 84 + "outputs": [ 85 + { 86 + "name": "stdout", 87 + "output_type": "stream", 88 + "text": [ 89 + "PCI_BUS_ID\n", 90 + "0,1\n" 91 + ] 92 + } 93 + ], 94 + "source": [ 95 + "import os\n", 96 + "print(os.environ[\"CUDA_DEVICE_ORDER\"])\n", 97 + "print(os.environ[\"CUDA_VISIBLE_DEVICES\"])" 98 + ] 99 + }, 100 + { 101 + "cell_type": "code", 102 + "execution_count": 4, 103 + "metadata": {}, 104 + "outputs": [ 105 + { 106 + "name": "stderr", 107 + "output_type": "stream", 108 + "text": [ 109 + "C:\\coding\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n", 110 + " from ._conv import register_converters as _register_converters\n" 111 + ] 112 + } 113 + ], 114 + "source": [ 115 + "import tensorflow as tf" 116 + ] 117 + }, 118 + { 119 + "cell_type": "code", 120 + "execution_count": 5, 121 + "metadata": {}, 122 + "outputs": [ 123 + { 124 + "data": { 125 + "text/plain": [ 126 + "[name: \"/device:CPU:0\"\n", 127 + " device_type: \"CPU\"\n", 128 + " memory_limit: 268435456\n", 129 + " locality {\n", 130 + " }\n", 131 + " incarnation: 8034297251726998311]" 132 + ] 133 + }, 134 + "execution_count": 5, 135 + "metadata": {}, 136 + "output_type": "execute_result" 137 + } 138 + ], 139 + "source": [ 140 + "from tensorflow.python.client import device_lib\n", 141 + "\n", 142 + "device_lib.list_local_devices()" 143 + ] 144 + }, 145 + { 146 + "cell_type": "code", 147 + "execution_count": 6, 148 + "metadata": {}, 149 + "outputs": [ 150 + { 151 + "data": { 152 + "text/plain": [ 153 + "''" 154 + ] 155 + }, 156 + "execution_count": 6, 157 + "metadata": {}, 158 + "output_type": "execute_result" 159 + } 160 + ], 161 + "source": [ 162 + "tf.test.gpu_device_name()" 163 + ] 164 + }, 165 + { 166 + "cell_type": "markdown", 167 + "metadata": {}, 168 + "source": [ 169 + "From https://mxnet.incubator.apache.org/install/index.html :" 170 + ] 171 + }, 172 + { 173 + "cell_type": "code", 174 + "execution_count": 7, 175 + "metadata": {}, 176 + "outputs": [ 177 + { 178 + "name": "stdout", 179 + "output_type": "stream", 180 + "text": [ 181 + "nvcc: NVIDIA (R) Cuda compiler driver\n", 182 + "Copyright (c) 2005-2017 NVIDIA Corporation\n", 183 + "Built on Fri_Sep__1_21:08:32_Central_Daylight_Time_2017\n", 184 + "Cuda compilation tools, release 9.0, V9.0.176\n" 185 + ] 186 + } 187 + ], 188 + "source": [ 189 + "!nvcc --version" 190 + ] 191 + }, 192 + { 193 + "cell_type": "code", 194 + "execution_count": 8, 195 + "metadata": { 196 + "scrolled": true 197 + }, 198 + "outputs": [ 199 + { 200 + "name": "stdout", 201 + "output_type": "stream", 202 + "text": [ 203 + "Requirement already satisfied: mxnet-cu90 in c:\\coding\\anaconda3\\lib\\site-packages (1.2.0)\n", 204 + "Requirement already satisfied: requests in c:\\coding\\anaconda3\\lib\\site-packages (from mxnet-cu90) (2.18.4)\n", 205 + "Requirement already satisfied: numpy in c:\\coding\\anaconda3\\lib\\site-packages (from mxnet-cu90) (1.14.3)\n", 206 + "Requirement already satisfied: graphviz in c:\\coding\\anaconda3\\lib\\site-packages (from mxnet-cu90) (0.8.3)\n", 207 + "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\\coding\\anaconda3\\lib\\site-packages (from requests->mxnet-cu90) (3.0.4)\n", 208 + "Requirement already satisfied: idna<2.7,>=2.5 in c:\\coding\\anaconda3\\lib\\site-packages (from requests->mxnet-cu90) (2.6)\n", 209 + "Requirement already satisfied: urllib3<1.23,>=1.21.1 in c:\\coding\\anaconda3\\lib\\site-packages (from requests->mxnet-cu90) (1.22)\n", 210 + "Requirement already satisfied: certifi>=2017.4.17 in c:\\coding\\anaconda3\\lib\\site-packages (from requests->mxnet-cu90) (2018.4.16)\n" 211 + ] 212 + }, 213 + { 214 + "name": "stderr", 215 + "output_type": "stream", 216 + "text": [ 217 + "distributed 1.21.8 requires msgpack, which is not installed.\n" 218 + ] 219 + } 220 + ], 221 + "source": [ 222 + "!pip install mxnet-cu90" 223 + ] 224 + }, 225 + { 226 + "cell_type": "code", 227 + "execution_count": 9, 228 + "metadata": {}, 229 + "outputs": [ 230 + { 231 + "name": "stdout", 232 + "output_type": "stream", 233 + "text": [ 234 + "timestamp, name, pci.bus_id, driver_version, pstate, pcie.link.gen.max, pcie.link.gen.current, temperature.gpu, utilization.gpu [%], utilization.memory [%], memory.total [MiB], memory.free [MiB], memory.used [MiB]\n", 235 + "2018/06/29 09:51:58.754, GeForce GTX 550 Ti, 00000000:01:00.0, 385.54, P0, [Not Supported], [Not Supported], 42, [Not Supported], [Not Supported], 1024 MiB, 448 MiB, 576 MiB\n" 236 + ] 237 + } 238 + ], 239 + "source": [ 240 + "# From https://stackoverflow.com/questions/49076092/is-there-a-way-to-check-if-mxnet-uses-my-gpu/49079940#49079940\n", 241 + "# https://developer.download.nvidia.com/compute/DCGM/docs/nvidia-smi-367.38.pdf\n", 242 + "!\"C:\\Program Files\\NVIDIA Corporation\\NVSMI\\nvidia-smi\" --query-gpu=timestamp,name,pci.bus_id,driver_version,pstate,pcie.link.gen.max,pcie.link.gen.current,temperature.gpu,utilization.gpu,utilization.memory,memory.total,memory.free,memory.used --format=csv" 243 + ] 244 + }, 245 + { 246 + "cell_type": "code", 247 + "execution_count": 10, 248 + "metadata": {}, 249 + "outputs": [], 250 + "source": [ 251 + "import mxnet as mx \n", 252 + "def gpu_device(gpu_number=0):\n", 253 + " try:\n", 254 + " _ = mx.nd.array([1, 2, 3], ctx=mx.gpu(gpu_number))\n", 255 + " except mx.MXNetError:\n", 256 + " return None\n", 257 + " return mx.gpu(gpu_number)" 258 + ] 259 + }, 260 + { 261 + "cell_type": "code", 262 + "execution_count": 11, 263 + "metadata": {}, 264 + "outputs": [ 265 + { 266 + "data": { 267 + "text/plain": [ 268 + "gpu(0)" 269 + ] 270 + }, 271 + "execution_count": 11, 272 + "metadata": {}, 273 + "output_type": "execute_result" 274 + } 275 + ], 276 + "source": [ 277 + "gpu_device()" 278 + ] 279 + }, 280 + { 281 + "cell_type": "code", 282 + "execution_count": 12, 283 + "metadata": {}, 284 + "outputs": [ 285 + { 286 + "data": { 287 + "text/plain": [ 288 + "gpu(0)" 289 + ] 290 + }, 291 + "execution_count": 12, 292 + "metadata": {}, 293 + "output_type": "execute_result" 294 + } 295 + ], 296 + "source": [ 297 + "mx.gpu(0)" 298 + ] 299 + }, 300 + { 301 + "cell_type": "markdown", 302 + "metadata": {}, 303 + "source": [ 304 + "From https://gluon.mxnet.io/chapter03_deep-neural-networks/mlp-gluon.html :" 305 + ] 306 + }, 307 + { 308 + "cell_type": "code", 309 + "execution_count": 13, 310 + "metadata": {}, 311 + "outputs": [], 312 + "source": [ 313 + "from __future__ import print_function\n", 314 + "import numpy as np\n", 315 + "import mxnet as mx\n", 316 + "from mxnet import nd, autograd, gluon" 317 + ] 318 + }, 319 + { 320 + "cell_type": "code", 321 + "execution_count": 14, 322 + "metadata": {}, 323 + "outputs": [], 324 + "source": [ 325 + "data_ctx = mx.cpu()\n", 326 + "model_ctx = mx.cpu()\n", 327 + "# model_ctx = mx.gpu(0)" 328 + ] 329 + }, 330 + { 331 + "cell_type": "code", 332 + "execution_count": 15, 333 + "metadata": {}, 334 + "outputs": [], 335 + "source": [ 336 + "batch_size = 64\n", 337 + "num_inputs = 784\n", 338 + "num_outputs = 10\n", 339 + "num_examples = 60000\n", 340 + "def transform(data, label):\n", 341 + " return data.astype(np.float32)/255, label.astype(np.float32)" 342 + ] 343 + }, 344 + { 345 + "cell_type": "code", 346 + "execution_count": 16, 347 + "metadata": {}, 348 + "outputs": [], 349 + "source": [ 350 + "train_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=True, transform=transform),\n", 351 + " batch_size, shuffle=True)\n", 352 + "test_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=False, transform=transform),\n", 353 + " batch_size, shuffle=False)" 354 + ] 355 + }, 356 + { 357 + "cell_type": "code", 358 + "execution_count": 17, 359 + "metadata": {}, 360 + "outputs": [], 361 + "source": [ 362 + "class MLP(gluon.Block):\n", 363 + " def __init__(self, **kwargs):\n", 364 + " super(MLP, self).__init__(**kwargs)\n", 365 + " with self.name_scope():\n", 366 + " self.dense0 = gluon.nn.Dense(64)\n", 367 + " self.dense1 = gluon.nn.Dense(64)\n", 368 + " self.dense2 = gluon.nn.Dense(10)\n", 369 + "\n", 370 + " def forward(self, x):\n", 371 + " x = nd.relu(self.dense0(x))\n", 372 + " x = nd.relu(self.dense1(x))\n", 373 + " x = self.dense2(x)\n", 374 + " return x" 375 + ] 376 + }, 377 + { 378 + "cell_type": "code", 379 + "execution_count": 18, 380 + "metadata": {}, 381 + "outputs": [], 382 + "source": [ 383 + "net = MLP()\n", 384 + "net.collect_params().initialize(mx.init.Normal(sigma=.01), ctx=model_ctx)" 385 + ] 386 + }, 387 + { 388 + "cell_type": "code", 389 + "execution_count": 19, 390 + "metadata": {}, 391 + "outputs": [ 392 + { 393 + "data": { 394 + "text/plain": [ 395 + "\n", 396 + "[[-5.2642502e-04 -4.8494569e-04 -9.1017238e-05 -1.0700601e-03\n", 397 + " 9.5340359e-04 1.2931204e-03 -3.8861975e-04 -6.4619188e-04\n", 398 + " 1.3646495e-04 -1.7153830e-03]]\n", 399 + "<NDArray 1x10 @cpu(0)>" 400 + ] 401 + }, 402 + "execution_count": 19, 403 + "metadata": {}, 404 + "output_type": "execute_result" 405 + } 406 + ], 407 + "source": [ 408 + "data = nd.ones((1,784))\n", 409 + "net(data.as_in_context(model_ctx))" 410 + ] 411 + }, 412 + { 413 + "cell_type": "code", 414 + "execution_count": 20, 415 + "metadata": {}, 416 + "outputs": [ 417 + { 418 + "name": "stdout", 419 + "output_type": "stream", 420 + "text": [ 421 + "Hidden Representation 1: \n", 422 + "[[0. 0. 0.0257028 0.41763663 0. 0.\n", 423 + " 0. 0. 0. 0.03712562 0.16054314 0.35507876\n", 424 + " 0. 0.12578698 0. 0. 0. 0.30374664\n", 425 + " 0.292567 0.35357708 0. 0.07809136 0.21969806 0.2177984\n", 426 + " 0. 0.3457912 0.13206203 0.01624641 0.27534354 0.22952288\n", 427 + " 0.2202207 0. 0.00258669 0.06395139 0.68015635 0.\n", 428 + " 0. 0. 0.1652459 0.18695295 0.25243065 0.01728743\n", 429 + " 0.06471729 0. 0. 0.2552151 0. 0.\n", 430 + " 0.03300378 0.33107045 0.6453747 0.04547642 0. 0.\n", 431 + " 0. 0.19542485 0.02424754 0. 0. 0.04300808\n", 432 + " 0.16542053 0.13203493 0. 0. ]]\n", 433 + "<NDArray 1x64 @cpu(0)>\n", 434 + "Hidden Representation 2: \n", 435 + "[[0.0000000e+00 0.0000000e+00 4.8457514e-03 0.0000000e+00 2.4975553e-02\n", 436 + " 0.0000000e+00 9.2384806e-03 1.1846514e-02 0.0000000e+00 1.5087268e-02\n", 437 + " 0.0000000e+00 1.3427198e-02 1.6015759e-02 0.0000000e+00 0.0000000e+00\n", 438 + " 0.0000000e+00 0.0000000e+00 0.0000000e+00 2.7162414e-02 4.1979598e-05\n", 439 + " 0.0000000e+00 1.8946800e-02 3.0578913e-03 0.0000000e+00 0.0000000e+00\n", 440 + " 2.7754948e-02 7.5642066e-04 0.0000000e+00 0.0000000e+00 1.9757828e-02\n", 441 + " 1.7670706e-02 0.0000000e+00 4.0669916e-03 1.0265570e-02 7.5005908e-03\n", 442 + " 1.5555882e-02 0.0000000e+00 0.0000000e+00 0.0000000e+00 2.8156085e-02\n", 443 + " 0.0000000e+00 0.0000000e+00 2.0807199e-02 0.0000000e+00 0.0000000e+00\n", 444 + " 0.0000000e+00 5.2651879e-04 0.0000000e+00 0.0000000e+00 3.6671013e-02\n", 445 + " 1.6886523e-02 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00\n", 446 + " 1.5089142e-02 1.0638590e-02 9.0155248e-03 1.8627236e-02 1.4041221e-02\n", 447 + " 0.0000000e+00 0.0000000e+00 0.0000000e+00 1.2555162e-02]]\n", 448 + "<NDArray 1x64 @cpu(0)>\n", 449 + "Network output: \n", 450 + "[[-1.1785791e-03 1.9014490e-04 8.1118196e-04 -3.8255830e-04\n", 451 + " 4.7956721e-04 -1.2719276e-04 3.3852040e-05 -2.3284566e-04\n", 452 + " 7.1805023e-04 1.1753932e-03]]\n", 453 + "<NDArray 1x10 @cpu(0)>\n" 454 + ] 455 + }, 456 + { 457 + "data": { 458 + "text/plain": [ 459 + "\n", 460 + "[[-1.1785791e-03 1.9014490e-04 8.1118196e-04 -3.8255830e-04\n", 461 + " 4.7956721e-04 -1.2719276e-04 3.3852040e-05 -2.3284566e-04\n", 462 + " 7.1805023e-04 1.1753932e-03]]\n", 463 + "<NDArray 1x10 @cpu(0)>" 464 + ] 465 + }, 466 + "execution_count": 20, 467 + "metadata": {}, 468 + "output_type": "execute_result" 469 + } 470 + ], 471 + "source": [ 472 + "class MLP(gluon.Block):\n", 473 + " def __init__(self, **kwargs):\n", 474 + " super(MLP, self).__init__(**kwargs)\n", 475 + " with self.name_scope():\n", 476 + " self.dense0 = gluon.nn.Dense(64, activation=\"relu\")\n", 477 + " self.dense1 = gluon.nn.Dense(64, activation=\"relu\")\n", 478 + " self.dense2 = gluon.nn.Dense(10)\n", 479 + "\n", 480 + " def forward(self, x):\n", 481 + " x = self.dense0(x)\n", 482 + " print(\"Hidden Representation 1: %s\" % x)\n", 483 + " x = self.dense1(x)\n", 484 + " print(\"Hidden Representation 2: %s\" % x)\n", 485 + " x = self.dense2(x)\n", 486 + " print(\"Network output: %s\" % x)\n", 487 + " return x\n", 488 + "\n", 489 + "net = MLP()\n", 490 + "net.collect_params().initialize(mx.init.Normal(sigma=.01), ctx=model_ctx)\n", 491 + "net(data.as_in_context(model_ctx))" 492 + ] 493 + }, 494 + { 495 + "cell_type": "code", 496 + "execution_count": 33, 497 + "metadata": {}, 498 + "outputs": [], 499 + "source": [ 500 + "num_hidden = 64\n", 501 + "net = gluon.nn.HybridSequential()\n", 502 + "with net.name_scope():\n", 503 + " net.add(gluon.nn.Dense(num_hidden, activation=\"relu\"))\n", 504 + " net.add(gluon.nn.Dense(num_hidden, activation=\"relu\"))\n", 505 + " net.add(gluon.nn.Dense(num_outputs))\n", 506 + "net.hybridize()" 507 + ] 508 + }, 509 + { 510 + "cell_type": "code", 511 + "execution_count": 34, 512 + "metadata": {}, 513 + "outputs": [], 514 + "source": [ 515 + "net.collect_params().initialize(mx.init.Normal(sigma=.1), ctx=model_ctx)" 516 + ] 517 + }, 518 + { 519 + "cell_type": "code", 520 + "execution_count": 35, 521 + "metadata": {}, 522 + "outputs": [], 523 + "source": [ 524 + "softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()" 525 + ] 526 + }, 527 + { 528 + "cell_type": "code", 529 + "execution_count": 36, 530 + "metadata": {}, 531 + "outputs": [], 532 + "source": [ 533 + "trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': .01})" 534 + ] 535 + }, 536 + { 537 + "cell_type": "code", 538 + "execution_count": 37, 539 + "metadata": {}, 540 + "outputs": [], 541 + "source": [ 542 + "def evaluate_accuracy(data_iterator, net):\n", 543 + " acc = mx.metric.Accuracy()\n", 544 + " for i, (data, label) in enumerate(data_iterator):\n", 545 + " data = data.as_in_context(model_ctx).reshape((-1, 784))\n", 546 + " label = label.as_in_context(model_ctx)\n", 547 + " output = net(data)\n", 548 + " predictions = nd.argmax(output, axis=1)\n", 549 + " acc.update(preds=predictions, labels=label)\n", 550 + " return acc.get()[1]" 551 + ] 552 + }, 553 + { 554 + "cell_type": "code", 555 + "execution_count": 38, 556 + "metadata": {}, 557 + "outputs": [ 558 + { 559 + "name": "stdout", 560 + "output_type": "stream", 561 + "text": [ 562 + "Requirement already satisfied: mxboard in c:\\coding\\anaconda3\\lib\\site-packages (0.1.0)\n", 563 + "Requirement already satisfied: protobuf>=3.0.0 in c:\\coding\\anaconda3\\lib\\site-packages (from mxboard) (3.6.0)\n", 564 + "Requirement already satisfied: Pillow in c:\\coding\\anaconda3\\lib\\site-packages (from mxboard) (5.1.0)\n", 565 + "Requirement already satisfied: six in c:\\coding\\anaconda3\\lib\\site-packages (from mxboard) (1.11.0)\n", 566 + "Requirement already satisfied: numpy in c:\\coding\\anaconda3\\lib\\site-packages (from mxboard) (1.14.3)\n", 567 + "Requirement already satisfied: setuptools in c:\\coding\\anaconda3\\lib\\site-packages (from protobuf>=3.0.0->mxboard) (39.1.0)\n" 568 + ] 569 + }, 570 + { 571 + "name": "stderr", 572 + "output_type": "stream", 573 + "text": [ 574 + "distributed 1.21.8 requires msgpack, which is not installed.\n" 575 + ] 576 + } 577 + ], 578 + "source": [ 579 + "!pip install mxboard" 580 + ] 581 + }, 582 + { 583 + "cell_type": "code", 584 + "execution_count": 39, 585 + "metadata": {}, 586 + "outputs": [ 587 + { 588 + "name": "stdout", 589 + "output_type": "stream", 590 + "text": [ 591 + "Requirement already satisfied: tensorboard in c:\\coding\\anaconda3\\lib\\site-packages (1.8.0)\n", 592 + "Requirement already satisfied: html5lib==0.9999999 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorboard) (0.9999999)\n", 593 + "Requirement already satisfied: bleach==1.5.0 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorboard) (1.5.0)\n", 594 + "Requirement already satisfied: protobuf>=3.4.0 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorboard) (3.6.0)\n", 595 + "Requirement already satisfied: werkzeug>=0.11.10 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorboard) (0.14.1)\n", 596 + "Requirement already satisfied: six>=1.10.0 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorboard) (1.11.0)\n", 597 + "Requirement already satisfied: wheel>=0.26; python_version >= \"3\" in c:\\coding\\anaconda3\\lib\\site-packages (from tensorboard) (0.31.1)\n", 598 + "Requirement already satisfied: markdown>=2.6.8 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorboard) (2.6.11)\n", 599 + "Requirement already satisfied: numpy>=1.12.0 in c:\\coding\\anaconda3\\lib\\site-packages (from tensorboard) (1.14.3)\n", 600 + "Requirement already satisfied: setuptools in c:\\coding\\anaconda3\\lib\\site-packages (from protobuf>=3.4.0->tensorboard) (39.1.0)\n" 601 + ] 602 + }, 603 + { 604 + "name": "stderr", 605 + "output_type": "stream", 606 + "text": [ 607 + "distributed 1.21.8 requires msgpack, which is not installed.\n" 608 + ] 609 + } 610 + ], 611 + "source": [ 612 + "!pip install tensorboard" 613 + ] 614 + }, 615 + { 616 + "cell_type": "code", 617 + "execution_count": 40, 618 + "metadata": {}, 619 + "outputs": [], 620 + "source": [ 621 + "from mxboard import SummaryWriter\n", 622 + "sw = SummaryWriter(logdir='logs', flush_secs=5)" 623 + ] 624 + }, 625 + { 626 + "cell_type": "code", 627 + "execution_count": 42, 628 + "metadata": {}, 629 + "outputs": [ 630 + { 631 + "name": "stdout", 632 + "output_type": "stream", 633 + "text": [ 634 + "Epoch 0. Loss: 1.2356886094411215, Train_acc 0.8395166666666667, Test_acc 0.8474\n", 635 + "Epoch 1. Loss: 0.46565542084376016, Train_acc 0.88465, Test_acc 0.8912\n", 636 + "Epoch 2. Loss: 0.3715192502895991, Train_acc 0.901, Test_acc 0.9029\n", 637 + "Epoch 3. Loss: 0.32939287207126616, Train_acc 0.91, Test_acc 0.9112\n", 638 + "Epoch 4. Loss: 0.30172612600326537, Train_acc 0.91625, Test_acc 0.9184\n", 639 + "Epoch 5. Loss: 0.2804558823386828, Train_acc 0.9217333333333333, Test_acc 0.921\n", 640 + "Epoch 6. Loss: 0.2626380964756012, Train_acc 0.9262666666666667, Test_acc 0.9252\n", 641 + "Epoch 7. Loss: 0.24708882774909338, Train_acc 0.9300333333333334, Test_acc 0.9296\n", 642 + "Epoch 8. Loss: 0.23393845278819403, Train_acc 0.9341833333333334, Test_acc 0.9332\n", 643 + "Epoch 9. Loss: 0.22262413431803385, Train_acc 0.93545, Test_acc 0.9352\n" 644 + ] 645 + } 646 + ], 647 + "source": [ 648 + "epochs = 10\n", 649 + "smoothing_constant = .01\n", 650 + "\n", 651 + "# collect parameter names for logging the gradients of parameters in each epoch\n", 652 + "params = net.collect_params()\n", 653 + "param_names = params.keys()\n", 654 + "global_step = 0\n", 655 + "\n", 656 + "for e in range(epochs):\n", 657 + " cumulative_loss = 0\n", 658 + " for i, (data, label) in enumerate(train_data):\n", 659 + " data = data.as_in_context(model_ctx).reshape((-1, 784))\n", 660 + " label = label.as_in_context(model_ctx)\n", 661 + " with autograd.record():\n", 662 + " output = net(data)\n", 663 + " loss = softmax_cross_entropy(output, label)\n", 664 + " \n", 665 + " sw.add_scalar(tag='cross_entropy', value=loss.mean().asscalar(), global_step=global_step)\n", 666 + " if i == 0:\n", 667 + " sw.add_image('minist_first_minibatch', data.reshape((batch_size, 1, 28, 28)), e)\n", 668 + " if e == 0:\n", 669 + " sw.add_graph(net)\n", 670 + " grads = [i.grad() for i in net.collect_params().values()]\n", 671 + " for i, name in enumerate(param_names):\n", 672 + " sw.add_histogram(tag=name, values=grads[i], global_step=e, bins=1000)\n", 673 + "\n", 674 + " global_step += 1\n", 675 + " loss.backward()\n", 676 + " trainer.step(data.shape[0])\n", 677 + " cumulative_loss += nd.sum(loss).asscalar()\n", 678 + "\n", 679 + " \n", 680 + " \n", 681 + " test_accuracy = evaluate_accuracy(test_data, net)\n", 682 + " train_accuracy = evaluate_accuracy(train_data, net)\n", 683 + " sw.add_scalar(tag='accuracy_curves', value=('train_acc', train_accuracy), global_step=e)\n", 684 + " sw.add_scalar(tag='accuracy_curves', value=('valid_acc', test_accuracy), global_step=e)\n", 685 + " print(\"Epoch %s. Loss: %s, Train_acc %s, Test_acc %s\" %\n", 686 + " (e, cumulative_loss/num_examples, train_accuracy, test_accuracy))" 687 + ] 688 + }, 689 + { 690 + "cell_type": "markdown", 691 + "metadata": {}, 692 + "source": [ 693 + "![](images/mx_01_acc.png)\n", 694 + "![](images/mx_02_loss.png)\n", 695 + "![](images/mx_03_img.png)\n", 696 + "![](images/mx_04_net.png)\n", 697 + "![](images/mx_05_dist.png)\n", 698 + "![](images/mx_06_hist.png)" 699 + ] 700 + }, 701 + { 702 + "cell_type": "code", 703 + "execution_count": null, 704 + "metadata": {}, 705 + "outputs": [], 706 + "source": [] 707 + } 708 + ], 709 + "metadata": { 710 + "kernelspec": { 711 + "display_name": "Python 3", 712 + "language": "python", 713 + "name": "python3" 714 + }, 715 + "language_info": { 716 + "codemirror_mode": { 717 + "name": "ipython", 718 + "version": 3 719 + }, 720 + "file_extension": ".py", 721 + "mimetype": "text/x-python", 722 + "name": "python", 723 + "nbconvert_exporter": "python", 724 + "pygments_lexer": "ipython3", 725 + "version": "3.6.5" 726 + } 727 + }, 728 + "nbformat": 4, 729 + "nbformat_minor": 2 730 + }
dl/images/mx_01_acc.png

This is a binary file and will not be displayed.

dl/images/mx_02_loss.png

This is a binary file and will not be displayed.

dl/images/mx_03_img.png

This is a binary file and will not be displayed.

dl/images/mx_04_net.png

This is a binary file and will not be displayed.

dl/images/mx_05_dist.png

This is a binary file and will not be displayed.

dl/images/mx_06_hist.png

This is a binary file and will not be displayed.