|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# High-level CNTK Example" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 1, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "import numpy as np\n", |
| 17 | + "import os\n", |
| 18 | + "import sys\n", |
| 19 | + "import cntk\n", |
| 20 | + "from cntk.layers import Convolution2D, MaxPooling, Dense, Dropout\n", |
| 21 | + "from common.params import *\n", |
| 22 | + "from common.utils import *" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 2, |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "# Force one-gpu\n", |
| 32 | + "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": 3, |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [ |
| 40 | + { |
| 41 | + "name": "stdout", |
| 42 | + "output_type": "stream", |
| 43 | + "text": [ |
| 44 | + "OS: linux\n", |
| 45 | + "Python: 3.5.2 |Anaconda custom (64-bit)| (default, Jul 2 2016, 17:53:06) \n", |
| 46 | + "[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]\n", |
| 47 | + "Numpy: 1.14.1\n", |
| 48 | + "CNTK: 2.4\n", |
| 49 | + "GPU: ['Tesla P100-PCIE-16GB', 'Tesla P100-PCIE-16GB']\n", |
| 50 | + "CUDA Version 8.0.61\n", |
| 51 | + "CuDNN Version 6.0.21\n" |
| 52 | + ] |
| 53 | + } |
| 54 | + ], |
| 55 | + "source": [ |
| 56 | + "print(\"OS: \", sys.platform)\n", |
| 57 | + "print(\"Python: \", sys.version)\n", |
| 58 | + "print(\"Numpy: \", np.__version__)\n", |
| 59 | + "print(\"CNTK: \", cntk.__version__)\n", |
| 60 | + "print(\"GPU: \", get_gpu_name())\n", |
| 61 | + "print(get_cuda_version())\n", |
| 62 | + "print(\"CuDNN Version \", get_cudnn_version())" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": 4, |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [], |
| 70 | + "source": [ |
| 71 | + "def create_symbol(n_classes=N_CLASSES):\n", |
| 72 | + " # Weight initialiser from uniform distribution\n", |
| 73 | + " # Activation (unless states) is None\n", |
| 74 | + " with cntk.layers.default_options(init = cntk.glorot_uniform(), activation = cntk.relu):\n", |
| 75 | + " x = Convolution2D(filter_shape=(3, 3), num_filters=50, pad=True)(features)\n", |
| 76 | + " x = Convolution2D(filter_shape=(3, 3), num_filters=50, pad=True)(x)\n", |
| 77 | + " x = MaxPooling((2, 2), strides=(2, 2), pad=False)(x)\n", |
| 78 | + " x = Dropout(0.25)(x)\n", |
| 79 | + "\n", |
| 80 | + " x = Convolution2D(filter_shape=(3, 3), num_filters=100, pad=True)(x)\n", |
| 81 | + " x = Convolution2D(filter_shape=(3, 3), num_filters=100, pad=True)(x)\n", |
| 82 | + " x = MaxPooling((2, 2), strides=(2, 2), pad=False)(x)\n", |
| 83 | + " x = Dropout(0.25)(x) \n", |
| 84 | + " \n", |
| 85 | + " x = Dense(512)(x)\n", |
| 86 | + " x = Dropout(0.5)(x)\n", |
| 87 | + " x = Dense(n_classes, activation=None)(x)\n", |
| 88 | + " return x" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": 5, |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [], |
| 96 | + "source": [ |
| 97 | + "def init_model(m, labels, lr=LR, momentum=MOMENTUM):\n", |
| 98 | + " # Loss (dense labels); check if support for sparse labels\n", |
| 99 | + " loss = cntk.cross_entropy_with_softmax(m, labels)\n", |
| 100 | + " # Momentum SGD\n", |
| 101 | + " # https://github.com/Microsoft/CNTK/blob/master/Manual/Manual_How_to_use_learners.ipynb\n", |
| 102 | + " # unit_gain=False: momentum_direction = momentum*old_momentum_direction + gradient\n", |
| 103 | + " # if unit_gain=True then ...(1-momentum)*gradient\n", |
| 104 | + " learner = cntk.momentum_sgd(m.parameters, \n", |
| 105 | + " lr=cntk.learning_rate_schedule(lr, cntk.UnitType.minibatch) , \n", |
| 106 | + " momentum=cntk.momentum_schedule(momentum),\n", |
| 107 | + " unit_gain=False)\n", |
| 108 | + " return loss, learner" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "code", |
| 113 | + "execution_count": 6, |
| 114 | + "metadata": {}, |
| 115 | + "outputs": [ |
| 116 | + { |
| 117 | + "name": "stdout", |
| 118 | + "output_type": "stream", |
| 119 | + "text": [ |
| 120 | + "Preparing train set...\n", |
| 121 | + "Preparing test set...\n", |
| 122 | + "(50000, 3, 32, 32) (10000, 3, 32, 32) (50000, 10) (10000, 10)\n", |
| 123 | + "float32 float32 float32 float32\n", |
| 124 | + "CPU times: user 738 ms, sys: 575 ms, total: 1.31 s\n", |
| 125 | + "Wall time: 1.31 s\n" |
| 126 | + ] |
| 127 | + } |
| 128 | + ], |
| 129 | + "source": [ |
| 130 | + "%%time\n", |
| 131 | + "# Data into format for library\n", |
| 132 | + "x_train, x_test, y_train, y_test = cifar_for_library(channel_first=True, one_hot=True)\n", |
| 133 | + "# CNTK format\n", |
| 134 | + "y_train = y_train.astype(np.float32)\n", |
| 135 | + "y_test = y_test.astype(np.float32)\n", |
| 136 | + "print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)\n", |
| 137 | + "print(x_train.dtype, x_test.dtype, y_train.dtype, y_test.dtype)" |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "code", |
| 142 | + "execution_count": 7, |
| 143 | + "metadata": {}, |
| 144 | + "outputs": [ |
| 145 | + { |
| 146 | + "name": "stdout", |
| 147 | + "output_type": "stream", |
| 148 | + "text": [ |
| 149 | + "CPU times: user 16.7 ms, sys: 40.4 ms, total: 57.1 ms\n", |
| 150 | + "Wall time: 67.4 ms\n" |
| 151 | + ] |
| 152 | + } |
| 153 | + ], |
| 154 | + "source": [ |
| 155 | + "%%time\n", |
| 156 | + "# Placeholders\n", |
| 157 | + "features = cntk.input_variable((3, 32, 32), np.float32)\n", |
| 158 | + "labels = cntk.input_variable(N_CLASSES, np.float32)\n", |
| 159 | + "# Load symbol\n", |
| 160 | + "sym = create_symbol()" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": 8, |
| 166 | + "metadata": {}, |
| 167 | + "outputs": [ |
| 168 | + { |
| 169 | + "name": "stdout", |
| 170 | + "output_type": "stream", |
| 171 | + "text": [ |
| 172 | + "CPU times: user 122 ms, sys: 116 ms, total: 238 ms\n", |
| 173 | + "Wall time: 239 ms\n" |
| 174 | + ] |
| 175 | + } |
| 176 | + ], |
| 177 | + "source": [ |
| 178 | + "%%time\n", |
| 179 | + "loss, learner = init_model(sym, labels)" |
| 180 | + ] |
| 181 | + }, |
| 182 | + { |
| 183 | + "cell_type": "code", |
| 184 | + "execution_count": 9, |
| 185 | + "metadata": {}, |
| 186 | + "outputs": [ |
| 187 | + { |
| 188 | + "name": "stdout", |
| 189 | + "output_type": "stream", |
| 190 | + "text": [ |
| 191 | + "CPU times: user 37.9 s, sys: 10.8 s, total: 48.7 s\n", |
| 192 | + "Wall time: 48.8 s\n" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "data": { |
| 197 | + "text/plain": [ |
| 198 | + "{'epoch_summaries': [{'loss': 1.8144259375, 'metric': 0.0, 'samples': 50000},\n", |
| 199 | + " {'loss': 1.36322234375, 'metric': 0.0, 'samples': 50000},\n", |
| 200 | + " {'loss': 1.122504140625, 'metric': 0.0, 'samples': 50000},\n", |
| 201 | + " {'loss': 0.974794296875, 'metric': 0.0, 'samples': 50000},\n", |
| 202 | + " {'loss': 0.8672890625, 'metric': 0.0, 'samples': 50000},\n", |
| 203 | + " {'loss': 0.7853078125, 'metric': 0.0, 'samples': 50000},\n", |
| 204 | + " {'loss': 0.716815546875, 'metric': 0.0, 'samples': 50000},\n", |
| 205 | + " {'loss': 0.65541078125, 'metric': 0.0, 'samples': 50000},\n", |
| 206 | + " {'loss': 0.606273671875, 'metric': 0.0, 'samples': 50000},\n", |
| 207 | + " {'loss': 0.560514921875, 'metric': 0.0, 'samples': 50000}],\n", |
| 208 | + " 'updates': [{'loss': 1.8144589081005922, 'metric': 0.0, 'samples': 49984},\n", |
| 209 | + " {'loss': 1.363123699583867, 'metric': 0.0, 'samples': 49984},\n", |
| 210 | + " {'loss': 1.1224501996889005, 'metric': 0.0, 'samples': 49984},\n", |
| 211 | + " {'loss': 0.9746546238546335, 'metric': 0.0, 'samples': 49984},\n", |
| 212 | + " {'loss': 0.8671638205475752, 'metric': 0.0, 'samples': 49984},\n", |
| 213 | + " {'loss': 0.7853081736155569, 'metric': 0.0, 'samples': 49984},\n", |
| 214 | + " {'loss': 0.7168769787582027, 'metric': 0.0, 'samples': 49984},\n", |
| 215 | + " {'loss': 0.6554993300981314, 'metric': 0.0, 'samples': 49984},\n", |
| 216 | + " {'loss': 0.6063771656930218, 'metric': 0.0, 'samples': 49984},\n", |
| 217 | + " {'loss': 0.5606013064805738, 'metric': 0.0, 'samples': 49984}]}" |
| 218 | + ] |
| 219 | + }, |
| 220 | + "execution_count": 9, |
| 221 | + "metadata": {}, |
| 222 | + "output_type": "execute_result" |
| 223 | + } |
| 224 | + ], |
| 225 | + "source": [ |
| 226 | + "%%time\n", |
| 227 | + "# Main training loop: 49s\n", |
| 228 | + "loss.train((x_train, y_train), \n", |
| 229 | + " minibatch_size=BATCHSIZE, \n", |
| 230 | + " max_epochs=EPOCHS,\n", |
| 231 | + " parameter_learners=[learner])" |
| 232 | + ] |
| 233 | + }, |
| 234 | + { |
| 235 | + "cell_type": "code", |
| 236 | + "execution_count": 10, |
| 237 | + "metadata": {}, |
| 238 | + "outputs": [ |
| 239 | + { |
| 240 | + "name": "stdout", |
| 241 | + "output_type": "stream", |
| 242 | + "text": [ |
| 243 | + "CPU times: user 284 ms, sys: 95.9 ms, total: 380 ms\n", |
| 244 | + "Wall time: 409 ms\n" |
| 245 | + ] |
| 246 | + } |
| 247 | + ], |
| 248 | + "source": [ |
| 249 | + "%%time\n", |
| 250 | + "# Main evaluation loop: 409ms\n", |
| 251 | + "n_samples = (y_test.shape[0]//BATCHSIZE)*BATCHSIZE\n", |
| 252 | + "y_guess = np.zeros(n_samples, dtype=np.int)\n", |
| 253 | + "y_truth = np.argmax(y_test[:n_samples], axis=-1)\n", |
| 254 | + "c = 0\n", |
| 255 | + "for data, label in yield_mb(x_test, y_test, BATCHSIZE):\n", |
| 256 | + " predicted_label_probs = sym.eval({features : data})\n", |
| 257 | + " y_guess[c*BATCHSIZE:(c+1)*BATCHSIZE] = np.argmax(predicted_label_probs, axis=-1)\n", |
| 258 | + " c += 1" |
| 259 | + ] |
| 260 | + }, |
| 261 | + { |
| 262 | + "cell_type": "code", |
| 263 | + "execution_count": 11, |
| 264 | + "metadata": {}, |
| 265 | + "outputs": [ |
| 266 | + { |
| 267 | + "name": "stdout", |
| 268 | + "output_type": "stream", |
| 269 | + "text": [ |
| 270 | + "Accuracy: 0.7591145833333334\n" |
| 271 | + ] |
| 272 | + } |
| 273 | + ], |
| 274 | + "source": [ |
| 275 | + "print(\"Accuracy: \", 1.*sum(y_guess == y_truth)/len(y_guess))" |
| 276 | + ] |
| 277 | + } |
| 278 | + ], |
| 279 | + "metadata": { |
| 280 | + "anaconda-cloud": {}, |
| 281 | + "kernelspec": { |
| 282 | + "display_name": "Python 3", |
| 283 | + "language": "python", |
| 284 | + "name": "python3" |
| 285 | + }, |
| 286 | + "language_info": { |
| 287 | + "codemirror_mode": { |
| 288 | + "name": "ipython", |
| 289 | + "version": 3 |
| 290 | + }, |
| 291 | + "file_extension": ".py", |
| 292 | + "mimetype": "text/x-python", |
| 293 | + "name": "python", |
| 294 | + "nbconvert_exporter": "python", |
| 295 | + "pygments_lexer": "ipython3", |
| 296 | + "version": "3.5.2" |
| 297 | + } |
| 298 | + }, |
| 299 | + "nbformat": 4, |
| 300 | + "nbformat_minor": 2 |
| 301 | +} |
0 commit comments