Last active
June 29, 2018 10:14
-
-
Save ytakashina/1aee084766a98b14260cf7f69bd2dd26 to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Results\n", | |
"1. MLP w/o ideep\n", | |
" - 18.46 (10 epoch)\n", | |
"2. MLP w/ ideep\n", | |
" - 25.65 (10 epoch)\n", | |
"3. CNN w/o ideep\n", | |
" - 72.0849 (1 epoch)\n", | |
"4. CNN w/ ideep\n", | |
" - 23.4223 (1 epoch)\n", | |
"5. CNN w/ GPU\n", | |
" - 14.2982 (1 epoch)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import chainer\n", | |
"import chainer.links as L\n", | |
"import chainer.functions as F\n", | |
"from chainer.cuda import to_gpu, to_cpu\n", | |
"from chainer.datasets import mnist, split_dataset_random\n", | |
"from chainer.optimizers import Adam\n", | |
"from chainer.iterators import SerialIterator\n", | |
"from chainer.training import StandardUpdater, Trainer, extensions\n", | |
"from chainer.training.extensions import LogReport, PrintReport\n", | |
"from chainer.backends.intel64 import is_ideep_available" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"GPU_ID = -1\n", | |
"BATCH_SIZE = 16\n", | |
"EPOCH = 10" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"if GPU_ID >= 0:\n", | |
" pass\n", | |
"elif GPU_ID == -2 and is_ideep_available():\n", | |
" chainer.global_config.use_ideep = 'always'\n", | |
"else:\n", | |
" chainer.global_config.use_ideep = 'never'\n", | |
"\n", | |
"chainer.config.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"class MLP(chainer.Chain):\n", | |
" def __init__(self, n_mid_units=16, n_out=10):\n", | |
" super(MLP, self).__init__()\n", | |
" with self.init_scope():\n", | |
" self.l1 = L.Linear(None, n_mid_units)\n", | |
" self.l2 = L.Linear(None, n_mid_units)\n", | |
" self.l3 = L.Linear(None, n_out)\n", | |
"\n", | |
" def __call__(self, x, t):\n", | |
" h1 = F.relu(self.l1(x))\n", | |
" h2 = F.relu(self.l2(h1))\n", | |
" y = self.l3(h2)\n", | |
" loss = F.softmax_cross_entropy(y, t)\n", | |
" return loss\n", | |
"\n", | |
"\n", | |
"class CNN(chainer.Chain):\n", | |
" def __init__(self, n_mid_channels=16, n_out=10):\n", | |
" super(CNN, self).__init__()\n", | |
" with self.init_scope():\n", | |
" self.conv1 = L.Convolution2D(None, n_mid_channels, 3)\n", | |
" self.conv2 = L.Convolution2D(None, n_mid_channels, 3)\n", | |
" self.l1 = L.Linear(None, n_out)\n", | |
"\n", | |
"\n", | |
" def __call__(self, x, t):\n", | |
" h = F.relu(self.conv1(x))\n", | |
" h = F.relu(self.conv2(h))\n", | |
" y = self.l1(h)\n", | |
" loss = F.softmax_cross_entropy(y, t)\n", | |
" return loss" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"train, _ = mnist.get_mnist(ndim=3)\n", | |
"train_iter = SerialIterator(train, BATCH_SIZE)\n", | |
"\n", | |
"# model = MLP()\n", | |
"model = CNN()\n", | |
"if GPU_ID >= 0:\n", | |
" model.to_gpu(GPU_ID)\n", | |
"elif GPU_ID == -2:\n", | |
" model.to_intel64()\n", | |
"optimizer = Adam().setup(model)\n", | |
"\n", | |
"updater = StandardUpdater(train_iter, optimizer, device=GPU_ID)\n", | |
"\n", | |
"trainer = Trainer(updater, (EPOCH, 'epoch'))\n", | |
"trainer.extend(LogReport(trigger=(1, 'epoch')))\n", | |
"trainer.extend(PrintReport(entries=['epoch', 'elapsed_time']))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": false | |
}, | |
"outputs": [], | |
"source": [ | |
"trainer.run()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [conda env:py36-chainer4]", | |
"language": "python", | |
"name": "conda-env-py36-chainer4-py" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment