├── .gitignore ├── .travis.yml ├── LICENSE.txt ├── README.md ├── deeplearning1 ├── excel │ ├── collab_filter.xlsx │ ├── conv-example.xlsx │ ├── entropy_example.xlsx │ ├── graddesc.xlsm │ └── layers_example.xlsx └── nbs │ ├── char-rnn.ipynb │ ├── convolution-intro.ipynb │ ├── dogs_cats_redux.ipynb │ ├── dogscats-ensemble.ipynb │ ├── imagenet_batchnorm.ipynb │ ├── init.sh │ ├── lesson1.ipynb │ ├── lesson2.ipynb │ ├── lesson3.ipynb │ ├── lesson4.ipynb │ ├── lesson5.ipynb │ ├── lesson6.ipynb │ ├── lesson7.ipynb │ ├── mnist.ipynb │ ├── resnet50.py │ ├── run.sh │ ├── sgd-intro.ipynb │ ├── statefarm-sample.ipynb │ ├── statefarm.ipynb │ ├── utils.py │ ├── vgg16.py │ ├── vgg16bn.py │ └── wordvectors.ipynb ├── deeplearning2 ├── DCGAN.ipynb ├── Keras-Tensorflow-Tutorial.ipynb ├── LICENSE ├── README.md ├── attention_wrapper.py ├── babi-memnn.ipynb ├── batcher.py ├── bcolz_array_iterator.py ├── bcolz_iter_test.ipynb ├── dcgan.py ├── densenet-keras.ipynb ├── imagenet_process.ipynb ├── kmeans.py ├── kmeans_test.ipynb ├── meanshift.ipynb ├── neural-sr.ipynb ├── neural-style-pytorch.ipynb ├── neural-style.ipynb ├── pytorch-tut.ipynb ├── rossman.ipynb ├── rossman_exp.py ├── seq2seq-translation.ipynb ├── spelling_bee_RNN.ipynb ├── taxi.ipynb ├── taxi_data_prep_and_mlp.ipynb ├── tf-basics.ipynb ├── tiramisu-keras.ipynb ├── tiramisu-pytorch.ipynb ├── torch_utils.py ├── translate-pytorch.ipynb ├── translate.ipynb ├── utils2.py ├── vgg16.py ├── vgg16_avg.py └── wgan-pytorch.ipynb ├── requirements.txt └── setup ├── README.md ├── aws-alias.sh ├── install-gpu-azure.sh ├── install-gpu.sh ├── setup_instance.sh ├── setup_p2.sh └── setup_t2.sh /.gitignore: -------------------------------------------------------------------------------- 1 | _site/ 2 | .sass-cache/ 3 | .jekyll-metadata 4 | .ipynb_checkpoints/ 5 | data/ 6 | *.pyc 7 | *.h5 8 | ai_aliases.sh 9 | fast-ai-commands.txt 10 | fast-ai-remove.sh 11 | .DS_Store -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 2.7 3 | dist: trusty 4 | sudo: false 5 | 6 | env: 7 | global: 8 | - NBCONVERT_ARGS="--to html --execute --ExecutePreprocessor.kernel_name=python --ExecutePreprocessor.timeout=-1" 9 | 10 | install: 11 | # xvfb 12 | # https://docs.travis-ci.com/user/gui-and-headless-browsers/ 13 | - "export DISPLAY=:99.0" 14 | - "sh -e /etc/init.d/xvfb start" 15 | # install conda 16 | # https://conda.io/docs/travis.html 17 | - if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then 18 | wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh -O miniconda.sh; 19 | else 20 | wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh; 21 | fi 22 | - bash miniconda.sh -b -f -p $HOME/miniconda 23 | - export PATH="$HOME/miniconda/bin:$PATH" 24 | - hash -r 25 | - conda config --set always_yes yes --set changeps1 no 26 | - conda update -q conda 27 | - conda config --add channels conda-forge 28 | - conda info -a 29 | - conda create -q --name fastai --file requirements.txt python=$TRAVIS_PYTHON_VERSION 30 | - source activate fastai 31 | - conda list 32 | 33 | script: 34 | - mkdir -p deeplearning1/nbs/data 35 | - if [ ! -d deeplearning1/nbs/data/dogscats ]; then 36 | curl -sSLO "http://files.fast.ai/data/dogscats.zip" && 37 | unzip -qq -d deeplearning1/nbs/data dogscats.zip; 38 | fi 39 | - travis_wait jupyter nbconvert deeplearning1/nbs/lesson1.ipynb $NBCONVERT_ARGS 40 | - travis_wait jupyter nbconvert deeplearning1/nbs/lesson2.ipynb $NBCONVERT_ARGS 41 | 42 | before_cache: 43 | # We only cache the archives but not the extracted packages, 44 | # such that the miniconda installer script can extract correctly. 45 | - rm -rf $HOME/miniconda/pkgs/*/ 46 | cache: 47 | timeout: 1000 48 | directories: 49 | - $HOME/miniconda/pkgs 50 | - $HOME/.keras/models 51 | - deeplearning1/nbs/data -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Practical Deep Learning for Coders (fast.ai courses) 2 | 3 | These are the lecture materials from [Practical Deep Learning for Coders](http://course17.fast.ai/) (2017). Two important parts of the course are [our online forums](http://forums.fast.ai/) and [our wiki](http://wiki.fast.ai/index.php/Main_Page). If you are encountering an error, we recommend that you first search the forums and wiki for a solution. If you can't find the answer there, the next step is to ask your question on the forums. See this advice on [how to ask for help](http://wiki.fast.ai/index.php/How_to_ask_for_Help) in a way that will allow others to most quickly and effectively be able to help you. Please don't use Github Issues to ask for help debugging (many questions have already been answered in the forums). 4 | -------------------------------------------------------------------------------- /deeplearning1/excel/collab_filter.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fastai/courses/31760fc1909c7ef61d81be5a147b19237fd45eaf/deeplearning1/excel/collab_filter.xlsx -------------------------------------------------------------------------------- /deeplearning1/excel/conv-example.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fastai/courses/31760fc1909c7ef61d81be5a147b19237fd45eaf/deeplearning1/excel/conv-example.xlsx -------------------------------------------------------------------------------- /deeplearning1/excel/entropy_example.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fastai/courses/31760fc1909c7ef61d81be5a147b19237fd45eaf/deeplearning1/excel/entropy_example.xlsx -------------------------------------------------------------------------------- /deeplearning1/excel/graddesc.xlsm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fastai/courses/31760fc1909c7ef61d81be5a147b19237fd45eaf/deeplearning1/excel/graddesc.xlsm -------------------------------------------------------------------------------- /deeplearning1/excel/layers_example.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fastai/courses/31760fc1909c7ef61d81be5a147b19237fd45eaf/deeplearning1/excel/layers_example.xlsx -------------------------------------------------------------------------------- /deeplearning1/nbs/char-rnn.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stderr", 12 | "output_type": "stream", 13 | "text": [ 14 | "Using gpu device 2: GeForce GTX TITAN X (CNMeM is enabled with initial size: 90.0% of memory, cuDNN 4007)\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "from theano.sandbox import cuda\n", 20 | "cuda.use('gpu2')" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 2, 26 | "metadata": { 27 | "collapsed": false 28 | }, 29 | "outputs": [ 30 | { 31 | "name": "stderr", 32 | "output_type": "stream", 33 | "text": [ 34 | "Using Theano backend.\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "%matplotlib inline\n", 40 | "import utils; reload(utils)\n", 41 | "from utils import *\n", 42 | "from __future__ import division, print_function" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 144, 48 | "metadata": { 49 | "collapsed": true 50 | }, 51 | "outputs": [], 52 | "source": [ 53 | "from keras.layers import TimeDistributed, Activation\n", 54 | "from numpy.random import choice" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "## Setup" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "We haven't really looked into the detail of how this works yet - so this is provided for self-study for those who are interested. We'll look at it closely next week." 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 107, 74 | "metadata": { 75 | "collapsed": false 76 | }, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "corpus length: 600901\n" 83 | ] 84 | } 85 | ], 86 | "source": [ 87 | "path = get_file('nietzsche.txt', origin=\"https://s3.amazonaws.com/text-datasets/nietzsche.txt\")\n", 88 | "text = open(path).read().lower()\n", 89 | "print('corpus length:', len(text))" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 272, 95 | "metadata": { 96 | "collapsed": false 97 | }, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "are thinkers who believe in the saints.\r\n", 104 | "\r\n", 105 | "\r\n", 106 | "144\r\n", 107 | "\r\n", 108 | "It stands to reason that this sketch of the saint, made upon the model\r\n", 109 | "of the whole species, can be confronted with many opposing sketches that\r\n", 110 | "would create a more agreeable impression. There are certain exceptions\r\n", 111 | "among the species who distinguish themselves either by especial\r\n", 112 | "gentleness or especial humanity, and perhaps by the strength of their\r\n", 113 | "own personality. Others are in the highest degree fascinating because\r\n", 114 | "certain of their delusions shed a particular glow over their whole\r\n", 115 | "being, as is the case with the founder of christianity who took himself\r\n", 116 | "for the only begotten son of God and hence felt himself sinless; so that\r\n", 117 | "through his imagination--that should not be too harshly judged since the\r\n", 118 | "whole of antiquity swarmed with sons of god--he attained the same goal,\r\n", 119 | "the sense of complete sinlessness, complete irresponsibility, that can\r\n", 120 | "now be attained by every individual through science.--In the same manner\r\n", 121 | "I have viewed the saints of India who occupy an intermediate station\r\n", 122 | "between the christian saints and the Greek philosophers and hence are\r\n", 123 | "not to be regarded as a pure type. Knowledge and science--as far as they\r\n", 124 | "existed--and superiority to the rest of mankind by logical discipline\r\n", 125 | "and training of the intellectual powers were insisted upon by the\r\n", 126 | "Buddhists as essential to sanctity, just as they were denounced by the\r\n", 127 | "christian world as the indications of sinfulness." 128 | ] 129 | } 130 | ], 131 | "source": [ 132 | "!tail {path} -n25" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 101, 138 | "metadata": { 139 | "collapsed": false 140 | }, 141 | "outputs": [ 142 | { 143 | "name": "stdout", 144 | "output_type": "stream", 145 | "text": [ 146 | "corpus length: 137587200\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "#path = 'data/wiki/'\n", 152 | "#text = open(path+'small.txt').read().lower()\n", 153 | "#print('corpus length:', len(text))\n", 154 | "\n", 155 | "#text = text[0:1000000]" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 124, 161 | "metadata": { 162 | "collapsed": false 163 | }, 164 | "outputs": [ 165 | { 166 | "name": "stdout", 167 | "output_type": "stream", 168 | "text": [ 169 | "total chars: 60\n" 170 | ] 171 | } 172 | ], 173 | "source": [ 174 | "chars = sorted(list(set(text)))\n", 175 | "vocab_size = len(chars)+1\n", 176 | "print('total chars:', vocab_size)" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 134, 182 | "metadata": { 183 | "collapsed": true 184 | }, 185 | "outputs": [], 186 | "source": [ 187 | "chars.insert(0, \"\\0\")" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 270, 193 | "metadata": { 194 | "collapsed": false 195 | }, 196 | "outputs": [ 197 | { 198 | "data": { 199 | "text/plain": [ 200 | "'\\n !\"\\'(),-.0123456789:;=?[]_abcdefghijklmnopqrstuvwxyz'" 201 | ] 202 | }, 203 | "execution_count": 270, 204 | "metadata": {}, 205 | "output_type": "execute_result" 206 | } 207 | ], 208 | "source": [ 209 | "''.join(chars[1:-6])" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 135, 215 | "metadata": { 216 | "collapsed": false 217 | }, 218 | "outputs": [], 219 | "source": [ 220 | "char_indices = dict((c, i) for i, c in enumerate(chars))\n", 221 | "indices_char = dict((i, c) for i, c in enumerate(chars))" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 136, 227 | "metadata": { 228 | "collapsed": true 229 | }, 230 | "outputs": [], 231 | "source": [ 232 | "idx = [char_indices[c] for c in text]" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 276, 238 | "metadata": { 239 | "collapsed": false 240 | }, 241 | "outputs": [ 242 | { 243 | "data": { 244 | "text/plain": [ 245 | "[43, 45, 32, 33, 28, 30, 32, 1, 1, 1]" 246 | ] 247 | }, 248 | "execution_count": 276, 249 | "metadata": {}, 250 | "output_type": "execute_result" 251 | } 252 | ], 253 | "source": [ 254 | "idx[:10]" 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": 274, 260 | "metadata": { 261 | "collapsed": false 262 | }, 263 | "outputs": [ 264 | { 265 | "data": { 266 | "text/plain": [ 267 | "'preface\\n\\n\\nsupposing that truth is a woman--what then? is there not gro'" 268 | ] 269 | }, 270 | "execution_count": 274, 271 | "metadata": {}, 272 | "output_type": "execute_result" 273 | } 274 | ], 275 | "source": [ 276 | "''.join(indices_char[i] for i in idx[:70])" 277 | ] 278 | }, 279 | { 280 | "cell_type": "markdown", 281 | "metadata": {}, 282 | "source": [ 283 | "## Preprocess and create model" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 139, 289 | "metadata": { 290 | "collapsed": false 291 | }, 292 | "outputs": [ 293 | { 294 | "name": "stdout", 295 | "output_type": "stream", 296 | "text": [ 297 | "0\n", 298 | "nb sequences: 600862\n" 299 | ] 300 | } 301 | ], 302 | "source": [ 303 | "maxlen = 40\n", 304 | "sentences = []\n", 305 | "next_chars = []\n", 306 | "for i in range(0, len(idx) - maxlen+1):\n", 307 | " sentences.append(idx[i: i + maxlen])\n", 308 | " next_chars.append(idx[i+1: i+maxlen+1])\n", 309 | "print('nb sequences:', len(sentences))" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": null, 315 | "metadata": { 316 | "collapsed": true 317 | }, 318 | "outputs": [], 319 | "source": [ 320 | "sentences = np.concatenate([[np.array(o)] for o in sentences[:-2]])\n", 321 | "next_chars = np.concatenate([[np.array(o)] for o in next_chars[:-2]])" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 277, 327 | "metadata": { 328 | "collapsed": false 329 | }, 330 | "outputs": [ 331 | { 332 | "data": { 333 | "text/plain": [ 334 | "((600860, 40), (600860, 40))" 335 | ] 336 | }, 337 | "execution_count": 277, 338 | "metadata": {}, 339 | "output_type": "execute_result" 340 | } 341 | ], 342 | "source": [ 343 | "sentences.shape, next_chars.shape" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 213, 349 | "metadata": { 350 | "collapsed": true 351 | }, 352 | "outputs": [], 353 | "source": [ 354 | "n_fac = 24" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 232, 360 | "metadata": { 361 | "collapsed": false 362 | }, 363 | "outputs": [], 364 | "source": [ 365 | "model=Sequential([\n", 366 | " Embedding(vocab_size, n_fac, input_length=maxlen),\n", 367 | " LSTM(512, input_dim=n_fac,return_sequences=True, dropout_U=0.2, dropout_W=0.2,\n", 368 | " consume_less='gpu'),\n", 369 | " Dropout(0.2),\n", 370 | " LSTM(512, return_sequences=True, dropout_U=0.2, dropout_W=0.2,\n", 371 | " consume_less='gpu'),\n", 372 | " Dropout(0.2),\n", 373 | " TimeDistributed(Dense(vocab_size)),\n", 374 | " Activation('softmax')\n", 375 | " ]) " 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "execution_count": 233, 381 | "metadata": { 382 | "collapsed": false 383 | }, 384 | "outputs": [], 385 | "source": [ 386 | "model.compile(loss='sparse_categorical_crossentropy', optimizer=Adam())" 387 | ] 388 | }, 389 | { 390 | "cell_type": "markdown", 391 | "metadata": {}, 392 | "source": [ 393 | "## Train" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 219, 399 | "metadata": { 400 | "collapsed": false 401 | }, 402 | "outputs": [], 403 | "source": [ 404 | "def print_example():\n", 405 | " seed_string=\"ethics is a basic foundation of all that\"\n", 406 | " for i in range(320):\n", 407 | " x=np.array([char_indices[c] for c in seed_string[-40:]])[np.newaxis,:]\n", 408 | " preds = model.predict(x, verbose=0)[0][-1]\n", 409 | " preds = preds/np.sum(preds)\n", 410 | " next_char = choice(chars, p=preds)\n", 411 | " seed_string = seed_string + next_char\n", 412 | " print(seed_string)" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": 236, 418 | "metadata": { 419 | "collapsed": true 420 | }, 421 | "outputs": [ 422 | { 423 | "name": "stdout", 424 | "output_type": "stream", 425 | "text": [ 426 | "Epoch 1/1\n", 427 | "600860/600860 [==============================] - 640s - loss: 1.5152 \n" 428 | ] 429 | }, 430 | { 431 | "data": { 432 | "text/plain": [ 433 | "" 434 | ] 435 | }, 436 | "execution_count": 236, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "model.fit(sentences, np.expand_dims(next_chars,-1), batch_size=64, nb_epoch=1)" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 220, 448 | "metadata": { 449 | "collapsed": false 450 | }, 451 | "outputs": [ 452 | { 453 | "name": "stdout", 454 | "output_type": "stream", 455 | "text": [ 456 | "ethics is a basic foundation of all thatscrriets sdi ,s lrrbmh\n", 457 | "fceelsora tec\n", 458 | " n yiefma\n", 459 | "cnostencnrut - o\n", 460 | "pen.htt\" oaiosovo stialpts es rb b\n", 461 | "ea ie\n", 462 | "ohatnmauyielueysiutlmo,es etfrne oh\n", 463 | "ohnio iis e.eosme o rdorfdbteirnse ohdnotafi enicron e eietnyn sytt e ptsrdrede httmi ah\n", 464 | "oo, tdye es r,igyct toehitu abrh ei isiem-r natra lnspamlltefae a\n", 465 | "cni vuui\n", 466 | "twgt fatieh\n" 467 | ] 468 | } 469 | ], 470 | "source": [ 471 | "print_example()" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": 236, 477 | "metadata": { 478 | "collapsed": true 479 | }, 480 | "outputs": [ 481 | { 482 | "name": "stdout", 483 | "output_type": "stream", 484 | "text": [ 485 | "Epoch 1/1\n", 486 | "600860/600860 [==============================] - 640s - loss: 1.5152 \n" 487 | ] 488 | }, 489 | { 490 | "data": { 491 | "text/plain": [ 492 | "" 493 | ] 494 | }, 495 | "execution_count": 236, 496 | "metadata": {}, 497 | "output_type": "execute_result" 498 | } 499 | ], 500 | "source": [ 501 | "model.fit(sentences, np.expand_dims(next_chars,-1), batch_size=64, nb_epoch=1)" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": 222, 507 | "metadata": { 508 | "collapsed": false, 509 | "scrolled": false 510 | }, 511 | "outputs": [ 512 | { 513 | "name": "stdout", 514 | "output_type": "stream", 515 | "text": [ 516 | "ethics is a basic foundation of all that he maluces indofely and is; pticrast', and re onerog je ivesantamale as whered\n", 517 | "and ror and kytinf? on chaninn nurdeln--ans prory. heke the pepadinar; anf bom,\n", 518 | "puntely\"\" ones to bucf, alcherstol the qisleves: the the wite dadong the gur is prang not galcaula rewinl\n", 519 | "more by than sic appads not pepow o mee, a more\n", 520 | "bins c\n" 521 | ] 522 | } 523 | ], 524 | "source": [ 525 | "print_example()" 526 | ] 527 | }, 528 | { 529 | "cell_type": "code", 530 | "execution_count": 235, 531 | "metadata": { 532 | "collapsed": false 533 | }, 534 | "outputs": [], 535 | "source": [ 536 | "model.optimizer.lr=0.001" 537 | ] 538 | }, 539 | { 540 | "cell_type": "code", 541 | "execution_count": 236, 542 | "metadata": { 543 | "collapsed": false 544 | }, 545 | "outputs": [ 546 | { 547 | "name": "stdout", 548 | "output_type": "stream", 549 | "text": [ 550 | "Epoch 1/1\n", 551 | "600860/600860 [==============================] - 640s - loss: 1.5152 \n" 552 | ] 553 | }, 554 | { 555 | "data": { 556 | "text/plain": [ 557 | "" 558 | ] 559 | }, 560 | "execution_count": 236, 561 | "metadata": {}, 562 | "output_type": "execute_result" 563 | } 564 | ], 565 | "source": [ 566 | "model.fit(sentences, np.expand_dims(next_chars,-1), batch_size=64, nb_epoch=1)" 567 | ] 568 | }, 569 | { 570 | "cell_type": "code", 571 | "execution_count": 237, 572 | "metadata": { 573 | "collapsed": false 574 | }, 575 | "outputs": [ 576 | { 577 | "name": "stdout", 578 | "output_type": "stream", 579 | "text": [ 580 | "ethics is a basic foundation of all that schools pedhaps a new prisons of the ashamed in which\n", 581 | "a coverbine estimates of the assumption that one avoid; he will curse about pain:\n", 582 | " people, he-equally present to\n", 583 | "the lalier,\n", 584 | "nature. that he has\n", 585 | "rendered and henceforth distrain and impulses to perceive that each other\n", 586 | "former and dangerous, and cannot at\n", 587 | "the pu\n" 588 | ] 589 | } 590 | ], 591 | "source": [ 592 | "print_example()" 593 | ] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "execution_count": 250, 598 | "metadata": { 599 | "collapsed": false 600 | }, 601 | "outputs": [], 602 | "source": [ 603 | "model.optimizer.lr=0.0001" 604 | ] 605 | }, 606 | { 607 | "cell_type": "code", 608 | "execution_count": 239, 609 | "metadata": { 610 | "collapsed": false, 611 | "scrolled": true 612 | }, 613 | "outputs": [ 614 | { 615 | "name": "stdout", 616 | "output_type": "stream", 617 | "text": [ 618 | "Epoch 1/1\n", 619 | "600860/600860 [==============================] - 639s - loss: 1.2892 \n" 620 | ] 621 | }, 622 | { 623 | "data": { 624 | "text/plain": [ 625 | "" 626 | ] 627 | }, 628 | "execution_count": 239, 629 | "metadata": {}, 630 | "output_type": "execute_result" 631 | } 632 | ], 633 | "source": [ 634 | "model.fit(sentences, np.expand_dims(next_chars,-1), batch_size=64, nb_epoch=1)" 635 | ] 636 | }, 637 | { 638 | "cell_type": "code", 639 | "execution_count": 240, 640 | "metadata": { 641 | "collapsed": false 642 | }, 643 | "outputs": [ 644 | { 645 | "name": "stdout", 646 | "output_type": "stream", 647 | "text": [ 648 | "ethics is a basic foundation of all that account has its granitify them.\n", 649 | "\n", 650 | "131. the new \"dilence,\" out of the\n", 651 | "same light,\n", 652 | "interpretation thereof: under the \"thinking\"\n", 653 | "there, to counter-arguments in the monality, so many language:\n", 654 | "though\n", 655 | "all nobilitys of higher impulses, man and hence to everything of seldom man.\n", 656 | "\n", 657 | "\n", 658 | "\n", 659 | "chapter i. woman decides according the injur\n" 660 | ] 661 | } 662 | ], 663 | "source": [ 664 | "print_example()" 665 | ] 666 | }, 667 | { 668 | "cell_type": "code", 669 | "execution_count": 242, 670 | "metadata": { 671 | "collapsed": false 672 | }, 673 | "outputs": [], 674 | "source": [ 675 | "model.save_weights('data/char_rnn.h5')" 676 | ] 677 | }, 678 | { 679 | "cell_type": "code", 680 | "execution_count": 257, 681 | "metadata": { 682 | "collapsed": false 683 | }, 684 | "outputs": [], 685 | "source": [ 686 | "model.optimizer.lr=0.00001" 687 | ] 688 | }, 689 | { 690 | "cell_type": "code", 691 | "execution_count": 243, 692 | "metadata": { 693 | "collapsed": false 694 | }, 695 | "outputs": [ 696 | { 697 | "name": "stdout", 698 | "output_type": "stream", 699 | "text": [ 700 | "Epoch 1/1\n", 701 | "600860/600860 [==============================] - 640s - loss: 1.2544 \n" 702 | ] 703 | }, 704 | { 705 | "data": { 706 | "text/plain": [ 707 | "" 708 | ] 709 | }, 710 | "execution_count": 243, 711 | "metadata": {}, 712 | "output_type": "execute_result" 713 | } 714 | ], 715 | "source": [ 716 | "model.fit(sentences, np.expand_dims(next_chars,-1), batch_size=64, nb_epoch=1)" 717 | ] 718 | }, 719 | { 720 | "cell_type": "code", 721 | "execution_count": 249, 722 | "metadata": { 723 | "collapsed": false 724 | }, 725 | "outputs": [ 726 | { 727 | "name": "stdout", 728 | "output_type": "stream", 729 | "text": [ 730 | "ethics is a basic foundation of all that is military\n", 731 | "contemplation of distance itself is in physician!\n", 732 | "\n", 733 | "249.. in every\n", 734 | "to strick in the man of disguise and in the\n", 735 | "will to wind at any progress, the\n", 736 | "religious estimates of vehapance has a powerful and religious nature of manner, who had the problem of\n", 737 | "decided expression of his equality, which, sometimes power? \n" 738 | ] 739 | } 740 | ], 741 | "source": [ 742 | "print_example()" 743 | ] 744 | }, 745 | { 746 | "cell_type": "code", 747 | "execution_count": 258, 748 | "metadata": { 749 | "collapsed": false 750 | }, 751 | "outputs": [ 752 | { 753 | "name": "stdout", 754 | "output_type": "stream", 755 | "text": [ 756 | "Epoch 1/1\n", 757 | "600860/600860 [==============================] - 640s - loss: 1.2218 \n" 758 | ] 759 | }, 760 | { 761 | "data": { 762 | "text/plain": [ 763 | "" 764 | ] 765 | }, 766 | "execution_count": 258, 767 | "metadata": {}, 768 | "output_type": "execute_result" 769 | } 770 | ], 771 | "source": [ 772 | "model.fit(sentences, np.expand_dims(next_chars,-1), batch_size=64, nb_epoch=1)" 773 | ] 774 | }, 775 | { 776 | "cell_type": "code", 777 | "execution_count": 264, 778 | "metadata": { 779 | "collapsed": false 780 | }, 781 | "outputs": [ 782 | { 783 | "name": "stdout", 784 | "output_type": "stream", 785 | "text": [ 786 | "ethics is a basic foundation of all that\n", 787 | "the belief in the importance. the employs concerning\n", 788 | "seriousness and\n", 789 | "materialism, it is circles which alone is already attained, that he sees\n", 790 | "also the day after thinking\n", 791 | "of mankind, brightness, resistance--and after the value of \"nature\" in order to nevertheless\n", 792 | "have taken a system of liberal fatalists are willing him\n" 793 | ] 794 | } 795 | ], 796 | "source": [ 797 | "print_example()" 798 | ] 799 | }, 800 | { 801 | "cell_type": "code", 802 | "execution_count": 283, 803 | "metadata": { 804 | "collapsed": false 805 | }, 806 | "outputs": [ 807 | { 808 | "name": "stdout", 809 | "output_type": "stream", 810 | "text": [ 811 | "ethics is a basic foundation of all that were beought by the temptation of truth--for the rest of a sublime medely and take part of life, which lacks himself the\n", 812 | "credibility about this, in short, and raise such a\n", 813 | "gods; and on the\n", 814 | "other hand, the explanation of\n", 815 | "the case, as the most ingredient, and insight, and approach as to the\n", 816 | "peculiarly prolonged \"distrus\n" 817 | ] 818 | } 819 | ], 820 | "source": [ 821 | "print_example()" 822 | ] 823 | }, 824 | { 825 | "cell_type": "code", 826 | "execution_count": 282, 827 | "metadata": { 828 | "collapsed": false 829 | }, 830 | "outputs": [], 831 | "source": [ 832 | "model.save_weights('data/char_rnn.h5')" 833 | ] 834 | }, 835 | { 836 | "cell_type": "code", 837 | "execution_count": null, 838 | "metadata": { 839 | "collapsed": true 840 | }, 841 | "outputs": [], 842 | "source": [] 843 | } 844 | ], 845 | "metadata": { 846 | "kernelspec": { 847 | "display_name": "Python 2", 848 | "language": "python", 849 | "name": "python2" 850 | }, 851 | "language_info": { 852 | "codemirror_mode": { 853 | "name": "ipython", 854 | "version": 2 855 | }, 856 | "file_extension": ".py", 857 | "mimetype": "text/x-python", 858 | "name": "python", 859 | "nbconvert_exporter": "python", 860 | "pygments_lexer": "ipython2", 861 | "version": "2.7.12" 862 | }, 863 | "nav_menu": {}, 864 | "toc": { 865 | "navigate_menu": true, 866 | "number_sections": true, 867 | "sideBar": true, 868 | "threshold": 6, 869 | "toc_cell": true, 870 | "toc_section_display": "block", 871 | "toc_window_display": false 872 | }, 873 | "widgets": { 874 | "state": {}, 875 | "version": "1.1.2" 876 | } 877 | }, 878 | "nbformat": 4, 879 | "nbformat_minor": 0 880 | } 881 | -------------------------------------------------------------------------------- /deeplearning1/nbs/init.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # 4 | # README: 5 | # 6 | # This script creates openssl certificates which are necessary for 7 | # publishing Jupyter notebooks through openssl when using EC2 instances. 8 | # 9 | # And it also configures Jupyter to use the created cerficates. 10 | # 11 | # NOTE: Your Jupyter notebook will be listening port number 8888. 12 | # This will also be configured by this script. 13 | # 14 | 15 | # Code adapted from https://gist.githubusercontent.com/rashmibanthia/5a1e4d7e313d6832f2ff/raw/1f32274758851a32444491500fef4852496596ce/jupyter_notebook_ec2.sh 16 | 17 | cd ~ 18 | 19 | jupyter notebook --generate-config 20 | 21 | key=$(python -c "from notebook.auth import passwd; print(passwd())") 22 | 23 | cd ~ 24 | mkdir certs 25 | cd certs 26 | certdir=$(pwd) 27 | openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.key -out mycert.pem 28 | 29 | cd ~ 30 | sed -i "1 a\ 31 | c = get_config()\\ 32 | c.NotebookApp.certfile = u'$certdir/mycert.pem'\\ 33 | c.NotebookApp.ip = '*'\\ 34 | c.NotebookApp.open_browser = False\\ 35 | c.NotebookApp.password = u'$key'\\ 36 | c.NotebookApp.port = 8888" .jupyter/jupyter_notebook_config.py 37 | -------------------------------------------------------------------------------- /deeplearning1/nbs/resnet50.py: -------------------------------------------------------------------------------- 1 | from __future__ import division, print_function 2 | 3 | import os, json 4 | from glob import glob 5 | import numpy as np 6 | from scipy import misc, ndimage 7 | from scipy.ndimage.interpolation import zoom 8 | 9 | import keras 10 | from keras import backend as K 11 | from keras.models import Sequential, Model 12 | from keras.layers.core import Flatten, Dense, Dropout, Lambda 13 | from keras.layers import Input, Activation, merge 14 | from keras.optimizers import RMSprop 15 | from keras.layers.normalization import BatchNormalization 16 | from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D, AveragePooling2D 17 | import keras.preprocessing.image as image 18 | from keras.utils.data_utils import get_file 19 | from keras.utils.layer_utils import convert_all_kernels_in_model 20 | from keras.applications.resnet50 import identity_block, conv_block 21 | 22 | 23 | class Resnet50(): 24 | """The Resnet 50 Imagenet model""" 25 | 26 | 27 | def __init__(self, size=(224,224), include_top=True): 28 | self.FILE_PATH = 'http://files.fast.ai/models/' 29 | self.vgg_mean = np.array([123.68, 116.779, 103.939]).reshape((3,1,1)) 30 | self.create(size, include_top) 31 | self.get_classes() 32 | 33 | 34 | def get_classes(self): 35 | fname = 'imagenet_class_index.json' 36 | fpath = get_file(fname, self.FILE_PATH+fname, cache_subdir='models') 37 | with open(fpath) as f: 38 | class_dict = json.load(f) 39 | self.classes = [class_dict[str(i)][1] for i in range(len(class_dict))] 40 | 41 | def predict(self, imgs, details=False): 42 | all_preds = self.model.predict(imgs) 43 | idxs = np.argmax(all_preds, axis=1) 44 | preds = [all_preds[i, idxs[i]] for i in range(len(idxs))] 45 | classes = [self.classes[idx] for idx in idxs] 46 | return np.array(preds), idxs, classes 47 | 48 | 49 | def vgg_preprocess(self, x): 50 | x = x - self.vgg_mean 51 | return x[:, ::-1] # reverse axis bgr->rgb 52 | 53 | 54 | def create(self, size, include_top): 55 | input_shape = (3,)+size 56 | img_input = Input(shape=input_shape) 57 | bn_axis = 1 58 | 59 | x = Lambda(self.vgg_preprocess)(img_input) 60 | x = ZeroPadding2D((3, 3))(x) 61 | x = Convolution2D(64, 7, 7, subsample=(2, 2), name='conv1')(x) 62 | x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) 63 | x = Activation('relu')(x) 64 | x = MaxPooling2D((3, 3), strides=(2, 2))(x) 65 | 66 | x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) 67 | x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') 68 | x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') 69 | 70 | x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') 71 | for n in ['b','c','d']: x = identity_block(x, 3, [128, 128, 512], stage=3, block=n) 72 | x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') 73 | for n in ['b','c','d', 'e', 'f']: x = identity_block(x, 3, [256, 256, 1024], stage=4, block=n) 74 | 75 | x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') 76 | x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') 77 | x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') 78 | 79 | if include_top: 80 | x = AveragePooling2D((7, 7), name='avg_pool')(x) 81 | x = Flatten()(x) 82 | x = Dense(1000, activation='softmax', name='fc1000')(x) 83 | fname = 'resnet50.h5' 84 | else: 85 | fname = 'resnet_nt.h5' 86 | 87 | self.img_input = img_input 88 | self.model = Model(self.img_input, x) 89 | convert_all_kernels_in_model(self.model) 90 | self.model.load_weights(get_file(fname, self.FILE_PATH+fname, cache_subdir='models')) 91 | 92 | 93 | def get_batches(self, path, gen=image.ImageDataGenerator(),class_mode='categorical', shuffle=True, batch_size=8): 94 | return gen.flow_from_directory(path, target_size=(224,224), 95 | class_mode=class_mode, shuffle=shuffle, batch_size=batch_size) 96 | 97 | 98 | def finetune(self, batches): 99 | model = self.model 100 | model.layers.pop() 101 | for layer in model.layers: layer.trainable=False 102 | m = Dense(batches.nb_class, activation='softmax')(model.layers[-1].output) 103 | self.model = Model(model.input, m) 104 | self.model.compile(optimizer=RMSprop(lr=0.1), loss='categorical_crossentropy', metrics=['accuracy']) 105 | 106 | 107 | def fit(self, batches, val_batches, nb_epoch=1): 108 | self.model.fit_generator(batches, samples_per_epoch=batches.nb_sample, nb_epoch=nb_epoch, 109 | validation_data=val_batches, nb_val_samples=val_batches.nb_sample) 110 | 111 | def test(self, path, batch_size=8): 112 | test_batches = self.get_batches(path, shuffle=False, batch_size=batch_size, class_mode=None) 113 | return test_batches, self.model.predict_generator(test_batches, test_batches.nb_sample) 114 | -------------------------------------------------------------------------------- /deeplearning1/nbs/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 4 | # README: 5 | # 6 | # This file will launch Jupyter notebook server with openssl certificates 7 | # which might have been created through helper script init.sh. 8 | # 9 | # Your Jupyter notebook server will be accessible at: 10 | # https://YourEC2IP:8888 11 | # 12 | 13 | jupyter notebook --certfile=~/certs/mycert.pem --keyfile=~/certs/mycert.key 14 | -------------------------------------------------------------------------------- /deeplearning1/nbs/sgd-intro.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "toc": "true" 7 | }, 8 | "source": [ 9 | "# Table of Contents\n", 10 | "

" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": { 17 | "collapsed": true 18 | }, 19 | "outputs": [], 20 | "source": [ 21 | "%matplotlib inline\n", 22 | "import math,sys,os,numpy as np\n", 23 | "from numpy.random import random\n", 24 | "from matplotlib import pyplot as plt, rcParams, animation, rc\n", 25 | "from __future__ import print_function, division\n", 26 | "from ipywidgets import interact, interactive, fixed\n", 27 | "from ipywidgets.widgets import *\n", 28 | "rc('animation', html='html5')\n", 29 | "rcParams['figure.figsize'] = 3, 3\n", 30 | "%precision 4\n", 31 | "np.set_printoptions(precision=4, linewidth=100)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 2, 37 | "metadata": { 38 | "collapsed": true 39 | }, 40 | "outputs": [], 41 | "source": [ 42 | "def lin(a,b,x): return a*x+b" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 3, 48 | "metadata": { 49 | "collapsed": true 50 | }, 51 | "outputs": [], 52 | "source": [ 53 | "a=3.\n", 54 | "b=8." 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 4, 60 | "metadata": { 61 | "collapsed": true 62 | }, 63 | "outputs": [], 64 | "source": [ 65 | "n=30\n", 66 | "x = random(n)\n", 67 | "y = lin(a,b,x)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 5, 73 | "metadata": { 74 | "collapsed": false 75 | }, 76 | "outputs": [ 77 | { 78 | "data": { 79 | "text/plain": [ 80 | "array([ 0.2764, 0.3057, 0.4897, 0.787 , 0.6627, 0.6034, 0.3722, 0.1886, 0.4145, 0.6801,\n", 81 | " 0.5178, 0.4675, 0.3708, 0.9557, 0.1604, 0.4065, 0.8752, 0.6775, 0.1818, 0.2346,\n", 82 | " 0.591 , 0.9049, 0.9551, 0.3547, 0.7319, 0.5636, 0.7542, 0.7464, 0.2111, 0.1858])" 83 | ] 84 | }, 85 | "execution_count": 5, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "x" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 6, 97 | "metadata": { 98 | "collapsed": false 99 | }, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "array([ 8.8293, 8.9171, 9.4692, 10.3611, 9.9881, 9.8102, 9.1167, 8.5659, 9.2436,\n", 105 | " 10.0403, 9.5534, 9.4025, 9.1125, 10.8671, 8.4813, 9.2196, 10.6257, 10.0326,\n", 106 | " 8.5453, 8.7038, 9.7731, 10.7148, 10.8654, 9.064 , 10.1956, 9.6907, 10.2627,\n", 107 | " 10.2393, 8.6334, 8.5573])" 108 | ] 109 | }, 110 | "execution_count": 6, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "source": [ 116 | "y" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 7, 122 | "metadata": { 123 | "collapsed": false 124 | }, 125 | "outputs": [ 126 | { 127 | "data": { 128 | "text/plain": [ 129 | "" 130 | ] 131 | }, 132 | "execution_count": 7, 133 | "metadata": {}, 134 | "output_type": "execute_result" 135 | }, 136 | { 137 | "data": { 138 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAANgAAADICAYAAACK9i92AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAEoNJREFUeJzt3X2QVNWZx/HvIzLDZBVEnWBEGLK+xBl3LQcWnLLyAhFK\nY2VDEpMglvFtghgK1GzFMpukVt1k3ZiY7GoSCrRGNMk6TioxanzZqBF0LReHIIpKo7g6w4su6UTU\nUIsw4rN/3DvSND1D07dPv/4+VV3cvn27z2noh3vuOec+x9wdEQnjoHJXQKSWKcBEAlKAiQSkABMJ\nSAEmEpACTCSg/QaYmXWZ2VYzW5ux7wtm9ryZ7TazycO890wzW29mL5nZVcWqtEi1yOcMtgw4I2vf\nc8DngMeGepOZHQT8JH7vScBcMzuxwHqKVKX9Bpi7PwFsy9r3ortvAGyYt04DNrh7v7sPAHcCs5NU\nVqTahLwGGw9syni+Od4nUjcOLncFAMxM87Wkorn7cK21IYU8g20BJmY8Pybel5O7l+xx9dVXqzyV\nl/cjiXwDzBj6emuo/auA48ysxcwagHOAew+wfiJVLZ9u+juAJ4ETzGyjmV1kZp81s01AB3CfmT0Y\nH/shM7sPwN13AwuBh4AXgDvdPRXqi4hUov1eg7n7uUO8dHeOY18HPp3x/D+BjxRcu0CmT5+u8lRe\nSVjSNmZRKmHmlVAPqRypVIre3l6mTZtGa2trWetiZngFdnKIFGTRoitoa5vChRdeR1vbFBYturzc\nVSqYzmBSUVKpFG1tU4CVwMnAWqCDdetWl+1MpjOY1Ize3l5gAlFwEf95TLy/+ijApKJMmzaNaALQ\n4NzytcDmeH/1UYBJRWltbWXhwnlEI0AnAB0sXDiv7B0dhdI1mFSkWulFVICJ7Ic6OaQqpdNpVq1a\nRTqdLndVglGASVl0d/fQ0nIis2ZdSkvLiXR395S7SkGoiSgll06naWk5kR07ljM41tXUNIP+/vU0\nNzeXu3r7UBNRqkpfXx8NDZPIHOsaObKFvr6+8lUqEAWYlNykSZPYtauPzLGugYF+Jk2aVL5KBaIA\nk5IZ7NQA6OpaTFPTDEaPnkxT0wy6uhZXZPMwqYpIGSC1r7u7h87OBTQ0RGevrq7F9Pevp6+vj0mT\nJtVkcIE6OaQEqq1TI5s6OaSi1VOnRjYFmARXT50a2RRgElxzc3PddGpk2+81mJl1EeXZ2OruJ8f7\nxgI9QAvQB3zJ3d/K8d7dwLNEmaf63f2zQ5Sha7A6kE6nq7JTI+hkXzP7KLAd+FlGgF0P/Nndvx8v\n6jDW3b+R471vu/voPL6AAkwqVtBODs+Rm54ox/zt8fbtQM4zE8PnrhepeYVeg33Q3bcCuPv/Ah8c\n4rhGM+s1syfNTAs/SN0p1kDzUO27Fnd/3cw+DDxqZmvd/dVcB15zzTXvb0+fPr2qct9JbVmxYgUr\nVqwoymflNdBsZi3AbzOuwVLAdHffamZHAcvdfdjbTs1sWfwZd+V4TddgUrFKMdCcnZv+XuDCePsC\n4J4clToszkmPmR0JnAasK6SSItWqoNz0wPeAWWb2InB6/Bwzm2JmN8dvbQX+YGZrgN8D/+ru60N8\nCZFKpbmIIvuhuYhScvWQT6MYFGBywOoln0YxqIkoB6Tabz0phJqIUjL1fOtJIRRgckDq+daTQijA\n5IDU860nhdA1mOQl+1aTar31pBC6BpOgli69hQkTjuP00zvf7zVsbm5m6tSpNR9cSekMJsNauvQW\nLr30cqKlhDYBV9HUdH1N9xpmS3IGU9o2GVIqleKyy65k7+VcZzBixNH09fXVTYAloSai5NTd3UN7\newe7do0jeznXgYGN6jXMkwJM9pFOp+nsXMDOnfcAf2Lv5Vw3cOONN+jslSc1EWUfg4PJO3ZMBxYD\nM4AjaGj4IzfddCPz588rbwWriAJM9rH3YPIcYByNjbNZs2Zl2ZdzrTZqIso+9h1MPptly25WcBVA\n3fQypHoaTB6OFkEXCUgzOUQqlAKsjqVSKW6//XZSqVS5q1Kz8kl602VmW81sbca+sWb2kJm9aGa/\nM7MxQ7z3AjN7KT7u/GJWXJJZtOgK2tqmcOGF19HWNoVFiy4vd5VqUrDc9PECEX8AJhOlfFsNTB5i\nkQhdg5VQKpWirW0Ke0+B6mDdutXqKcyhUnPTnwE85O5vufubwEPAmYVUUorrkUceASaQPQWqt7e3\nfJWqUSFz048nmn49aEu8T8qou7uHK6/8NtE/TeYUqM1MmzatfBWrUaFz0+dNuenD2zPH8L+ALqCD\n6P+8LSxcOE/Nw1gxc9Pj7vt9EC20tzbjeQoYF28fBaRyvOccYEnG8yXAnCE+3yW83t5eHzNmsoPH\nj3U+atRE7+npKXfVKlr8+8wrVrIfwXLTA78jSq89Ju7wmBXvkzLZN2HNAGbbmTFjRhlrVduC5aZ3\n923Ad4h6Ep8CrvWos0PKRAlrSk9TpeqQ5hgeGM1FFAlIcxFFKpQCTCQgBVgN0tJClUMBVmO0tFBl\nUSdHDUmlUrS3d8TZoKZTD0sLlYI6OSTOY3gaO3ceBZwN9KClhcpPWaVqwJ45ho+RmYEXxmlpoTLT\nGawG5FoUD46gsXG2ZmqUmc5gVWxwRsYhhxySMccwOoM1NqaVx7ACKMCqVHd3D52dC2hoiCbwdnae\nR1fXDEaObGFgoJ+uriUKrgqgXsQqNNRC5KtXP8H27ds1x7DItHxRndmTO37vhci3b9/O1KlTy1o3\n2Zs6OaqQFiKvHgqwKqT7uqqHrsGqmO7rKg3dDyYSkKZK1TjNjq9eCrAKp9nx1S1RE9HMLge+Ej+9\nxd1vynr9E0QZp16Jd93l7t/N8TlqIuYw1HiXZseXVlnGwczsJKAT+DvgXeBBM7vP3V/JOvRxd/9M\noeXUs6HGu/r6+hRgVSJJE7EVeMrdd7r7buBx4PM5jiso8kXjXbUgSYA9D3wsXsroA8BZRCsKZOsw\nszVmdr+ZtSUor24MdmoAGu+qcgU3Ed19fbyM0cNEyxutAXZnHbYaaHH3/zOzTwF3Ayfk+jzlpo9k\nT+Lt6lpMf/96jXeVUDFz0xdtHMzM/gXY5O5LhjnmVWCKu7+RtV+dHKhTo1KVbRzMzJrjPycCnwPu\nyHp9XMb2NKKA3iu4ZI9cN07qlv/qlnQ2/a/N7HBgAFjg7m+b2Xyi1ShuBr5gZl+NX98BzElYXs1K\np9Ns27aNnTtfIfPGSXVqVDdNlaoAmdddO3b8D+67aWo6Pr5xcjFz5+r/pXLSXMQqNtR11913d9Pe\n3q5rrwqguYhVbKjrrrFjxyq4aoACrMw0mFzbFGBlppsna5uuwSqEbp6sXOrkEAlInRxVQDdN1icF\nWAnopsn6pSZiYOl0mokTT+Cdd34KzAJe1/zCKqMmYgVbuvQW3nlnF/BD4EQgpfmFdURnsIByzdKA\n6Ywa5Wzc+JLOYFVCqbMrVK5b/uFwvvWtixVcdUJNxIByzdJoatrG/PnzylgrKSUFWJFldsdrlobo\nGqyIurt7uPjiSxkxYhy7d2/l1luXMHfuHM3SqHKayVEB0uk048cfy8DAwcCHgVcZOXKALVteUVBV\nOXXTV4Dly5czMLALuIso188KBgbeY82aNWWumZSTAqwIurt7OP/8+cBE4Gygh6jH8ENlrZeUn5qI\nCeUe65oB/JqGhs+yefMGNRGrXDmzSl1uZs/Fj8uGOOYmM9tgZs+Y2SlJyqtEue5IhiNobJzNbbct\nVXDVuaC56eNko8e6+/FmdiqwBOhIWOeKsvdYV3QGa2xMs2bNSlpbW8tbOSm70LnpZwM/A3D3p4Ax\nmbkSa0Gusa5ly5YouARINlXqeeC7ZjYW2EmUm35V1jHjgU0Zz7fE+7YmKLfizJ07h5kzP6mxLtlH\n6Nz0eav23PTNzc0KrBpRNbnpzWwJsNzde+Ln64FPuPvWrPdWbS+i1L6KzU0P3AucHx/TAbyZHVzV\nRLf9y4FKOtD8azN7nmiZ2Pdz05vZJQDu/gDwqpm9DCwFFiQsr2x0278UQgPNedCyQvVNcxED07JC\nUigFWB6U3loKpQAbhtZKlqSUk2MIWitZikGdHDmoU0MyqZOjyNSpIcWiAMtBnRpSLAqwHJQNSopF\n12DDUDYoAWWVSkyBJMNRJ0cCmmMoIdX1GUzd8ZIPncEKkE6neeCBBzj44PGoO15CqcsAG2wWLlp0\nI3/5y8vA9+NX1B0vxVV3TcTceQw7OOSQY9m9+zW6uhYzd+6cktRFqoPWBzsAudbsOvTQj/DjH1/B\nWWedpWsvKaq6aiKm02m2bdu2zyyNd9/dqOCSIOrmDHbDDT/im9/8JxoaJvLuu7toaPg4o0b9NQMD\n/ZqlIcHUxTVYZ+d8br3158AEojSNn2PUqAe4554e2tvbFVwyrHJmlfqamT1vZmvN7D/MrCHr9QvM\n7I9m9nT8uDhJeYVIpVJxcK0EXoz//A0jRhzF2LFjFVwSVMEBZmZHA4uAye5+MlFz85wch97p7pPj\nx62Flleo3t5eojNX5uIMx7Br1yZ1x0twSTs5RgB/ZWYHAx8AXstxTEGn1mJIp9M0NTURNQv3dGrA\nZq677p919pLgCg4wd38N+CGwkSjn/Jvu/kiOQz8fL130SzM7ptDyDtTgYPIll1zPQQc50aIuJwAd\nXHzxeXz96/9QqqpIHUuyfNFhRKuntABvAb8ys3PdPTO7773AHe4+ECcjvR04PdfnFTM3fTqdprNz\nATt2LI/Hu9bS2PgxfvCDRcycOVMrn8iwKiI3vZl9ATjD3efFz78MnOruC4c4/iDgDXc/LMdrRe1F\nXLVqFbNmXcpbb61+f9/o0ZN55JGlTJ06tWjlSH0oVy/iRqDDzEaZmRGdmVJZFTsq4+lsYF2C8vKm\nW/6lUiS5BusFfkW0bNGz8e6bzexaM/t0/PyyuBt/DbAQuDBJZfOlW/6lUtT0QLPuVJZiUMoAkYB0\nw6VIhVKAiQSkABMJSAEmEpACTCQgBZhIQAowkYAUYCIBKcBEAlKAiQSkABMJSAEmEpACTCQgBZhI\nQAowkYAUYCIBKcBEAlKAiQQUOjd9g5ndaWYbzOy/zWxisuoWR7Fy3qm8+igvidC56TuJciEeD/w7\ne9ZqLata/0GovMoROjf9bKJsvhCleMuZ1VekVoXOTT+eaOUF3H038KaZHV5omSJVx90LegCHAb8H\nDic6k/0GODfrmOeAozOevwwcnuOzXA89KvlRaJwkWUJ2JvCKu78BYGZ3AacBmYs/bCZanOs1MxsB\njB48PlOhOedEKl3Q3PTAb4EL4u0vAo8mKE+k6iTK7GtmVxP1HA4ATwPzgG8Dq9z9PjNrBH4OtAN/\nBs5x976klRapFhWROlukVpV0JoeZnWlm683sJTO7KsfrRR2YzqO8r5nZC/EKnA+b2YSQ5WUcd7aZ\nvWdmk0OXZ2Zfir/jc2b2i5DlmdkEM3s0XvD+GTP7VIKyusxsq5mtHeaYm+LfyjNmdkqhZeVTnpmd\na2bPxo8nzOxv8/rgQntHCuh1PIioF7EFGAk8A5yYdcxXgcXx9hyiBdRDlvcJYFS8fWno8uLjDgEe\nA54kGqQP+f2OA1YTdS4BHBm4vKXA/Hi7FXg1QXkfBU4B1g7x+qeA++PtU4GVCX+f+yuvAxgTb5+Z\nb3mlPINNAza4e7+7DwB3Eg1EZyrmwPR+y3P3x9z9nfjpSqJxu2Dlxb4DfA/YmaCsfMubB/zU3d8G\ncPc/BS7vPWB0vH0Y0fhoQdz9CWDbMIfMBn4WH/sUMMbMxoUqz91Xuvtb8dO8fyulDLD3B51jm9m3\nksUcmM6nvEydwIMFlpVXeWbWDhzj7knKybs8olXfPxI3aZ40szMCl3ct8GUz2wTcRzSVLpTs+mzJ\nUZ9QvkKev5Uk42ClUJLxMTM7D5hC1GQMVYYBP2LPsAWE/34HEzUTPw5MBB43s78ZPKMFMBdY5u7/\nZmYdwC+AkwKVVRZmNgO4iKhJuV+lPINtIfpHHnQM+zYhBgemGW5guojlYWYzgX8E/j5u+hRqf+Ud\nSvRjW2FmrxK16e9J0NGR79/nve7+nkfDIy8BxwcsrxP4JURNKmCUmR1ZYHn51CezUyrnv28xmdnJ\nwM3AZ9x9uObrHkkuDA/wInIEey6SG4gukluzjlnAnk6Oc0jW6ZBPee3xMceW4vtlHb8caA/8/c4A\nbou3jwT6gbEBy7sfuCDebgU2J/w7nQQ8N8RrZ7Gnk6ODhJ0ceZQ3EdgAdBzQZyat1AF+gTOBF+OK\nfiPedy3w6Xi7keh/wA1EF5KTApf3MPA60SD5GuDukOVlHfsoCXoR8y2PaEL2C0QL1X8x8N9nK/BE\nHHxPA6cnKOsOorszdhLNGroImA9cknHMT+Kgf7YIf5fDlgfcQjRZYvC30pvP52qgWSQgpQwQCUgB\nJhKQAkwkIAWYSEAKMJGAFGAiASnARAL6fwYejWFDRfGKAAAAAElFTkSuQmCC\n", 139 | "text/plain": [ 140 | "" 141 | ] 142 | }, 143 | "metadata": {}, 144 | "output_type": "display_data" 145 | } 146 | ], 147 | "source": [ 148 | "plt.scatter(x,y)" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 8, 154 | "metadata": { 155 | "collapsed": true 156 | }, 157 | "outputs": [], 158 | "source": [ 159 | "def sse(y,y_pred): return ((y-y_pred)**2).sum()\n", 160 | "def loss(y,a,b,x): return sse(y, lin(a,b,x))\n", 161 | "def avg_loss(y,a,b,x): return np.sqrt(loss(y,a,b,x)/n)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 9, 167 | "metadata": { 168 | "collapsed": false 169 | }, 170 | "outputs": [ 171 | { 172 | "data": { 173 | "text/plain": [ 174 | "9.1365" 175 | ] 176 | }, 177 | "execution_count": 9, 178 | "metadata": {}, 179 | "output_type": "execute_result" 180 | } 181 | ], 182 | "source": [ 183 | "a_guess=-1.\n", 184 | "b_guess=1.\n", 185 | "avg_loss(y, a_guess, b_guess, x)" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": null, 191 | "metadata": { 192 | "collapsed": true 193 | }, 194 | "outputs": [], 195 | "source": [ 196 | "lr=0.01\n", 197 | "# d[(y-(a*x+b))**2,b] = 2 (b + a x - y) = 2 (y_pred - y)\n", 198 | "# d[(y-(a*x+b))**2,a] = 2 x (b + a x - y) = x * dy/db" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": null, 204 | "metadata": { 205 | "collapsed": false 206 | }, 207 | "outputs": [], 208 | "source": [ 209 | "def upd():\n", 210 | " global a_guess, b_guess\n", 211 | " y_pred = lin(a_guess, b_guess, x)\n", 212 | " dydb = 2 * (y_pred - y)\n", 213 | " dyda = x*dydb\n", 214 | " a_guess -= lr*dyda.mean()\n", 215 | " b_guess -= lr*dydb.mean()" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": null, 221 | "metadata": { 222 | "collapsed": false 223 | }, 224 | "outputs": [], 225 | "source": [ 226 | "fig = plt.figure(dpi=100, figsize=(5, 4))\n", 227 | "plt.scatter(x,y)\n", 228 | "line, = plt.plot(x,lin(a_guess,b_guess,x))\n", 229 | "plt.close()\n", 230 | "\n", 231 | "def animate(i):\n", 232 | " line.set_ydata(lin(a_guess,b_guess,x))\n", 233 | " for i in range(10): upd()\n", 234 | " return line,\n", 235 | "\n", 236 | "ani = animation.FuncAnimation(fig, animate, np.arange(0, 40), interval=100)\n", 237 | "ani" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "metadata": { 244 | "collapsed": true 245 | }, 246 | "outputs": [], 247 | "source": [] 248 | } 249 | ], 250 | "metadata": { 251 | "kernelspec": { 252 | "display_name": "Python 2", 253 | "language": "python", 254 | "name": "python2" 255 | }, 256 | "language_info": { 257 | "codemirror_mode": { 258 | "name": "ipython", 259 | "version": 2 260 | }, 261 | "file_extension": ".py", 262 | "mimetype": "text/x-python", 263 | "name": "python", 264 | "nbconvert_exporter": "python", 265 | "pygments_lexer": "ipython2", 266 | "version": "2.7.12" 267 | }, 268 | "nav_menu": {}, 269 | "toc": { 270 | "navigate_menu": true, 271 | "number_sections": true, 272 | "sideBar": true, 273 | "threshold": 6, 274 | "toc_cell": true, 275 | "toc_section_display": "block", 276 | "toc_window_display": false 277 | }, 278 | "widgets": { 279 | "state": {}, 280 | "version": "1.1.2" 281 | } 282 | }, 283 | "nbformat": 4, 284 | "nbformat_minor": 0 285 | } 286 | -------------------------------------------------------------------------------- /deeplearning1/nbs/utils.py: -------------------------------------------------------------------------------- 1 | from __future__ import division,print_function 2 | import math, os, json, sys, re 3 | import cPickle as pickle 4 | from glob import glob 5 | import numpy as np 6 | from matplotlib import pyplot as plt 7 | from operator import itemgetter, attrgetter, methodcaller 8 | from collections import OrderedDict 9 | import itertools 10 | from itertools import chain 11 | 12 | import pandas as pd 13 | import PIL 14 | from PIL import Image 15 | from numpy.random import random, permutation, randn, normal, uniform, choice 16 | from numpy import newaxis 17 | import scipy 18 | from scipy import misc, ndimage 19 | from scipy.ndimage.interpolation import zoom 20 | from scipy.ndimage import imread 21 | from sklearn.metrics import confusion_matrix 22 | import bcolz 23 | from sklearn.preprocessing import OneHotEncoder 24 | from sklearn.manifold import TSNE 25 | 26 | from IPython.lib.display import FileLink 27 | 28 | import theano 29 | from theano import shared, tensor as T 30 | from theano.tensor.nnet import conv2d, nnet 31 | from theano.tensor.signal import pool 32 | 33 | import keras 34 | from keras import backend as K 35 | from keras.utils.data_utils import get_file 36 | from keras.utils import np_utils 37 | from keras.utils.np_utils import to_categorical 38 | from keras.models import Sequential, Model 39 | from keras.layers import Input, Embedding, Reshape, merge, LSTM, Bidirectional 40 | from keras.layers import TimeDistributed, Activation, SimpleRNN, GRU 41 | from keras.layers.core import Flatten, Dense, Dropout, Lambda 42 | from keras.regularizers import l2, activity_l2, l1, activity_l1 43 | from keras.layers.normalization import BatchNormalization 44 | from keras.optimizers import SGD, RMSprop, Adam 45 | from keras.utils.layer_utils import layer_from_config 46 | from keras.metrics import categorical_crossentropy, categorical_accuracy 47 | from keras.layers.convolutional import * 48 | from keras.preprocessing import image, sequence 49 | from keras.preprocessing.text import Tokenizer 50 | 51 | from vgg16 import * 52 | from vgg16bn import * 53 | np.set_printoptions(precision=4, linewidth=100) 54 | 55 | 56 | to_bw = np.array([0.299, 0.587, 0.114]) 57 | 58 | def gray(img): 59 | if K.image_dim_ordering() == 'tf': 60 | return np.rollaxis(img, 0, 1).dot(to_bw) 61 | else: 62 | return np.rollaxis(img, 0, 3).dot(to_bw) 63 | 64 | def to_plot(img): 65 | if K.image_dim_ordering() == 'tf': 66 | return np.rollaxis(img, 0, 1).astype(np.uint8) 67 | else: 68 | return np.rollaxis(img, 0, 3).astype(np.uint8) 69 | 70 | def plot(img): 71 | plt.imshow(to_plot(img)) 72 | 73 | 74 | def floor(x): 75 | return int(math.floor(x)) 76 | def ceil(x): 77 | return int(math.ceil(x)) 78 | 79 | def plots(ims, figsize=(12,6), rows=1, interp=False, titles=None): 80 | if type(ims[0]) is np.ndarray: 81 | ims = np.array(ims).astype(np.uint8) 82 | if (ims.shape[-1] != 3): 83 | ims = ims.transpose((0,2,3,1)) 84 | f = plt.figure(figsize=figsize) 85 | cols = len(ims)//rows if len(ims) % 2 == 0 else len(ims)//rows + 1 86 | for i in range(len(ims)): 87 | sp = f.add_subplot(rows, cols, i+1) 88 | sp.axis('Off') 89 | if titles is not None: 90 | sp.set_title(titles[i], fontsize=16) 91 | plt.imshow(ims[i], interpolation=None if interp else 'none') 92 | 93 | 94 | def do_clip(arr, mx): 95 | clipped = np.clip(arr, (1-mx)/1, mx) 96 | return clipped/clipped.sum(axis=1)[:, np.newaxis] 97 | 98 | 99 | def get_batches(dirname, gen=image.ImageDataGenerator(), shuffle=True, batch_size=4, class_mode='categorical', 100 | target_size=(224,224)): 101 | return gen.flow_from_directory(dirname, target_size=target_size, 102 | class_mode=class_mode, shuffle=shuffle, batch_size=batch_size) 103 | 104 | 105 | def onehot(x): 106 | return to_categorical(x) 107 | 108 | 109 | def wrap_config(layer): 110 | return {'class_name': layer.__class__.__name__, 'config': layer.get_config()} 111 | 112 | 113 | def copy_layer(layer): return layer_from_config(wrap_config(layer)) 114 | 115 | 116 | def copy_layers(layers): return [copy_layer(layer) for layer in layers] 117 | 118 | 119 | def copy_weights(from_layers, to_layers): 120 | for from_layer,to_layer in zip(from_layers, to_layers): 121 | to_layer.set_weights(from_layer.get_weights()) 122 | 123 | 124 | def copy_model(m): 125 | res = Sequential(copy_layers(m.layers)) 126 | copy_weights(m.layers, res.layers) 127 | return res 128 | 129 | 130 | def insert_layer(model, new_layer, index): 131 | res = Sequential() 132 | for i,layer in enumerate(model.layers): 133 | if i==index: res.add(new_layer) 134 | copied = layer_from_config(wrap_config(layer)) 135 | res.add(copied) 136 | copied.set_weights(layer.get_weights()) 137 | return res 138 | 139 | 140 | def adjust_dropout(weights, prev_p, new_p): 141 | scal = (1-prev_p)/(1-new_p) 142 | return [o*scal for o in weights] 143 | 144 | 145 | def get_data(path, target_size=(224,224)): 146 | batches = get_batches(path, shuffle=False, batch_size=1, class_mode=None, target_size=target_size) 147 | return np.concatenate([batches.next() for i in range(batches.nb_sample)]) 148 | 149 | 150 | def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): 151 | """ 152 | This function prints and plots the confusion matrix. 153 | Normalization can be applied by setting `normalize=True`. 154 | (This function is copied from the scikit docs.) 155 | """ 156 | plt.figure() 157 | plt.imshow(cm, interpolation='nearest', cmap=cmap) 158 | plt.title(title) 159 | plt.colorbar() 160 | tick_marks = np.arange(len(classes)) 161 | plt.xticks(tick_marks, classes, rotation=45) 162 | plt.yticks(tick_marks, classes) 163 | 164 | if normalize: 165 | cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] 166 | print(cm) 167 | thresh = cm.max() / 2. 168 | for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): 169 | plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") 170 | 171 | plt.tight_layout() 172 | plt.ylabel('True label') 173 | plt.xlabel('Predicted label') 174 | 175 | 176 | def save_array(fname, arr): 177 | c=bcolz.carray(arr, rootdir=fname, mode='w') 178 | c.flush() 179 | 180 | 181 | def load_array(fname): 182 | return bcolz.open(fname)[:] 183 | 184 | 185 | def mk_size(img, r2c): 186 | r,c,_ = img.shape 187 | curr_r2c = r/c 188 | new_r, new_c = r,c 189 | if r2c>curr_r2c: 190 | new_r = floor(c*r2c) 191 | else: 192 | new_c = floor(r/r2c) 193 | arr = np.zeros((new_r, new_c, 3), dtype=np.float32) 194 | r2=(new_r-r)//2 195 | c2=(new_c-c)//2 196 | arr[floor(r2):floor(r2)+r,floor(c2):floor(c2)+c] = img 197 | return arr 198 | 199 | 200 | def mk_square(img): 201 | x,y,_ = img.shape 202 | maxs = max(img.shape[:2]) 203 | y2=(maxs-y)//2 204 | x2=(maxs-x)//2 205 | arr = np.zeros((maxs,maxs,3), dtype=np.float32) 206 | arr[floor(x2):floor(x2)+x,floor(y2):floor(y2)+y] = img 207 | return arr 208 | 209 | 210 | def vgg_ft(out_dim): 211 | vgg = Vgg16() 212 | vgg.ft(out_dim) 213 | model = vgg.model 214 | return model 215 | 216 | def vgg_ft_bn(out_dim): 217 | vgg = Vgg16BN() 218 | vgg.ft(out_dim) 219 | model = vgg.model 220 | return model 221 | 222 | 223 | def get_classes(path): 224 | batches = get_batches(path+'train', shuffle=False, batch_size=1) 225 | val_batches = get_batches(path+'valid', shuffle=False, batch_size=1) 226 | test_batches = get_batches(path+'test', shuffle=False, batch_size=1) 227 | return (val_batches.classes, batches.classes, onehot(val_batches.classes), onehot(batches.classes), 228 | val_batches.filenames, batches.filenames, test_batches.filenames) 229 | 230 | 231 | def split_at(model, layer_type): 232 | layers = model.layers 233 | layer_idx = [index for index,layer in enumerate(layers) 234 | if type(layer) is layer_type][-1] 235 | return layers[:layer_idx+1], layers[layer_idx+1:] 236 | 237 | 238 | class MixIterator(object): 239 | def __init__(self, iters): 240 | self.iters = iters 241 | self.multi = type(iters) is list 242 | if self.multi: 243 | self.N = sum([it[0].N for it in self.iters]) 244 | else: 245 | self.N = sum([it.N for it in self.iters]) 246 | 247 | def reset(self): 248 | for it in self.iters: it.reset() 249 | 250 | def __iter__(self): 251 | return self 252 | 253 | def next(self, *args, **kwargs): 254 | if self.multi: 255 | nexts = [[next(it) for it in o] for o in self.iters] 256 | n0 = np.concatenate([n[0] for n in nexts]) 257 | n1 = np.concatenate([n[1] for n in nexts]) 258 | return (n0, n1) 259 | else: 260 | nexts = [next(it) for it in self.iters] 261 | n0 = np.concatenate([n[0] for n in nexts]) 262 | n1 = np.concatenate([n[1] for n in nexts]) 263 | return (n0, n1) 264 | 265 | -------------------------------------------------------------------------------- /deeplearning1/nbs/vgg16.py: -------------------------------------------------------------------------------- 1 | from __future__ import division, print_function 2 | 3 | import os, json 4 | from glob import glob 5 | import numpy as np 6 | from scipy import misc, ndimage 7 | from scipy.ndimage.interpolation import zoom 8 | 9 | from keras import backend as K 10 | from keras.layers.normalization import BatchNormalization 11 | from keras.utils.data_utils import get_file 12 | from keras.models import Sequential 13 | from keras.layers.core import Flatten, Dense, Dropout, Lambda 14 | from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D 15 | from keras.layers.pooling import GlobalAveragePooling2D 16 | from keras.optimizers import SGD, RMSprop, Adam 17 | from keras.preprocessing import image 18 | 19 | # In case we are going to use the TensorFlow backend we need to explicitly set the Theano image ordering 20 | from keras import backend as K 21 | K.set_image_dim_ordering('th') 22 | 23 | 24 | vgg_mean = np.array([123.68, 116.779, 103.939], dtype=np.float32).reshape((3,1,1)) 25 | def vgg_preprocess(x): 26 | """ 27 | Subtracts the mean RGB value, and transposes RGB to BGR. 28 | The mean RGB was computed on the image set used to train the VGG model. 29 | 30 | Args: 31 | x: Image array (height x width x channels) 32 | Returns: 33 | Image array (height x width x transposed_channels) 34 | """ 35 | x = x - vgg_mean 36 | return x[:, ::-1] # reverse axis rgb->bgr 37 | 38 | 39 | class Vgg16(): 40 | """ 41 | The VGG 16 Imagenet model 42 | """ 43 | 44 | 45 | def __init__(self): 46 | self.FILE_PATH = 'http://files.fast.ai/models/' 47 | self.create() 48 | self.get_classes() 49 | 50 | 51 | def get_classes(self): 52 | """ 53 | Downloads the Imagenet classes index file and loads it to self.classes. 54 | The file is downloaded only if it not already in the cache. 55 | """ 56 | fname = 'imagenet_class_index.json' 57 | fpath = get_file(fname, self.FILE_PATH+fname, cache_subdir='models') 58 | with open(fpath) as f: 59 | class_dict = json.load(f) 60 | self.classes = [class_dict[str(i)][1] for i in range(len(class_dict))] 61 | 62 | def predict(self, imgs, details=False): 63 | """ 64 | Predict the labels of a set of images using the VGG16 model. 65 | 66 | Args: 67 | imgs (ndarray) : An array of N images (size: N x width x height x channels). 68 | details : ?? 69 | 70 | Returns: 71 | preds (np.array) : Highest confidence value of the predictions for each image. 72 | idxs (np.ndarray): Class index of the predictions with the max confidence. 73 | classes (list) : Class labels of the predictions with the max confidence. 74 | """ 75 | # predict probability of each class for each image 76 | all_preds = self.model.predict(imgs) 77 | # for each image get the index of the class with max probability 78 | idxs = np.argmax(all_preds, axis=1) 79 | # get the values of the highest probability for each image 80 | preds = [all_preds[i, idxs[i]] for i in range(len(idxs))] 81 | # get the label of the class with the highest probability for each image 82 | classes = [self.classes[idx] for idx in idxs] 83 | return np.array(preds), idxs, classes 84 | 85 | 86 | def ConvBlock(self, layers, filters): 87 | """ 88 | Adds a specified number of ZeroPadding and Covolution layers 89 | to the model, and a MaxPooling layer at the very end. 90 | 91 | Args: 92 | layers (int): The number of zero padded convolution layers 93 | to be added to the model. 94 | filters (int): The number of convolution filters to be 95 | created for each layer. 96 | """ 97 | model = self.model 98 | for i in range(layers): 99 | model.add(ZeroPadding2D((1, 1))) 100 | model.add(Convolution2D(filters, 3, 3, activation='relu')) 101 | model.add(MaxPooling2D((2, 2), strides=(2, 2))) 102 | 103 | 104 | def FCBlock(self): 105 | """ 106 | Adds a fully connected layer of 4096 neurons to the model with a 107 | Dropout of 0.5 108 | 109 | Args: None 110 | Returns: None 111 | """ 112 | model = self.model 113 | model.add(Dense(4096, activation='relu')) 114 | model.add(Dropout(0.5)) 115 | 116 | 117 | def create(self): 118 | """ 119 | Creates the VGG16 network achitecture and loads the pretrained weights. 120 | 121 | Args: None 122 | Returns: None 123 | """ 124 | model = self.model = Sequential() 125 | model.add(Lambda(vgg_preprocess, input_shape=(3,224,224), output_shape=(3,224,224))) 126 | 127 | self.ConvBlock(2, 64) 128 | self.ConvBlock(2, 128) 129 | self.ConvBlock(3, 256) 130 | self.ConvBlock(3, 512) 131 | self.ConvBlock(3, 512) 132 | 133 | model.add(Flatten()) 134 | self.FCBlock() 135 | self.FCBlock() 136 | model.add(Dense(1000, activation='softmax')) 137 | 138 | fname = 'vgg16.h5' 139 | model.load_weights(get_file(fname, self.FILE_PATH+fname, cache_subdir='models')) 140 | 141 | 142 | def get_batches(self, path, gen=image.ImageDataGenerator(), shuffle=True, batch_size=8, class_mode='categorical'): 143 | """ 144 | Takes the path to a directory, and generates batches of augmented/normalized data. Yields batches indefinitely, in an infinite loop. 145 | 146 | See Keras documentation: https://keras.io/preprocessing/image/ 147 | """ 148 | return gen.flow_from_directory(path, target_size=(224,224), 149 | class_mode=class_mode, shuffle=shuffle, batch_size=batch_size) 150 | 151 | 152 | def ft(self, num): 153 | """ 154 | Replace the last layer of the model with a Dense (fully connected) layer of num neurons. 155 | Will also lock the weights of all layers except the new layer so that we only learn 156 | weights for the last layer in subsequent training. 157 | 158 | Args: 159 | num (int) : Number of neurons in the Dense layer 160 | Returns: 161 | None 162 | """ 163 | model = self.model 164 | model.pop() 165 | for layer in model.layers: layer.trainable=False 166 | model.add(Dense(num, activation='softmax')) 167 | self.compile() 168 | 169 | def finetune(self, batches): 170 | """ 171 | Modifies the original VGG16 network architecture and updates self.classes for new training data. 172 | 173 | Args: 174 | batches : A keras.preprocessing.image.ImageDataGenerator object. 175 | See definition for get_batches(). 176 | """ 177 | self.ft(batches.nb_class) 178 | classes = list(iter(batches.class_indices)) # get a list of all the class labels 179 | 180 | # batches.class_indices is a dict with the class name as key and an index as value 181 | # eg. {'cats': 0, 'dogs': 1} 182 | 183 | # sort the class labels by index according to batches.class_indices and update model.classes 184 | for c in batches.class_indices: 185 | classes[batches.class_indices[c]] = c 186 | self.classes = classes 187 | 188 | 189 | def compile(self, lr=0.001): 190 | """ 191 | Configures the model for training. 192 | See Keras documentation: https://keras.io/models/model/ 193 | """ 194 | self.model.compile(optimizer=Adam(lr=lr), 195 | loss='categorical_crossentropy', metrics=['accuracy']) 196 | 197 | 198 | def fit_data(self, trn, labels, val, val_labels, nb_epoch=1, batch_size=64): 199 | """ 200 | Trains the model for a fixed number of epochs (iterations on a dataset). 201 | See Keras documentation: https://keras.io/models/model/ 202 | """ 203 | self.model.fit(trn, labels, nb_epoch=nb_epoch, 204 | validation_data=(val, val_labels), batch_size=batch_size) 205 | 206 | 207 | def fit(self, batches, val_batches, nb_epoch=1): 208 | """ 209 | Fits the model on data yielded batch-by-batch by a Python generator. 210 | See Keras documentation: https://keras.io/models/model/ 211 | """ 212 | self.model.fit_generator(batches, samples_per_epoch=batches.nb_sample, nb_epoch=nb_epoch, 213 | validation_data=val_batches, nb_val_samples=val_batches.nb_sample) 214 | 215 | 216 | def test(self, path, batch_size=8): 217 | """ 218 | Predicts the classes using the trained model on data yielded batch-by-batch. 219 | 220 | Args: 221 | path (string): Path to the target directory. It should contain one subdirectory 222 | per class. 223 | batch_size (int): The number of images to be considered in each batch. 224 | 225 | Returns: 226 | test_batches, numpy array(s) of predictions for the test_batches. 227 | 228 | """ 229 | test_batches = self.get_batches(path, shuffle=False, batch_size=batch_size, class_mode=None) 230 | return test_batches, self.model.predict_generator(test_batches, test_batches.nb_sample) 231 | 232 | -------------------------------------------------------------------------------- /deeplearning1/nbs/vgg16bn.py: -------------------------------------------------------------------------------- 1 | from __future__ import division, print_function 2 | 3 | import os, json 4 | from glob import glob 5 | import numpy as np 6 | from scipy import misc, ndimage 7 | from scipy.ndimage.interpolation import zoom 8 | 9 | from keras import backend as K 10 | from keras.layers.normalization import BatchNormalization 11 | from keras.utils.data_utils import get_file 12 | from keras.models import Sequential 13 | from keras.layers.core import Flatten, Dense, Dropout, Lambda 14 | from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D 15 | from keras.layers.pooling import GlobalAveragePooling2D 16 | from keras.optimizers import SGD, Adam 17 | from keras.preprocessing import image 18 | 19 | # In case we are going to use the TensorFlow backend we need to explicitly set the Theano image ordering 20 | from keras import backend as K 21 | K.set_image_dim_ordering('th') 22 | 23 | vgg_mean = np.array([123.68, 116.779, 103.939], dtype=np.float32).reshape((3,1,1)) 24 | def vgg_preprocess(x): 25 | x = x - vgg_mean 26 | return x[:, ::-1] # reverse axis rgb->bgr 27 | 28 | 29 | class Vgg16BN(): 30 | """The VGG 16 Imagenet model with Batch Normalization for the Dense Layers""" 31 | 32 | 33 | def __init__(self, size=(224,224), include_top=True): 34 | self.FILE_PATH = 'http://files.fast.ai/models/' 35 | self.create(size, include_top) 36 | self.get_classes() 37 | 38 | 39 | def get_classes(self): 40 | fname = 'imagenet_class_index.json' 41 | fpath = get_file(fname, self.FILE_PATH+fname, cache_subdir='models') 42 | with open(fpath) as f: 43 | class_dict = json.load(f) 44 | self.classes = [class_dict[str(i)][1] for i in range(len(class_dict))] 45 | 46 | def predict(self, imgs, details=False): 47 | all_preds = self.model.predict(imgs) 48 | idxs = np.argmax(all_preds, axis=1) 49 | preds = [all_preds[i, idxs[i]] for i in range(len(idxs))] 50 | classes = [self.classes[idx] for idx in idxs] 51 | return np.array(preds), idxs, classes 52 | 53 | 54 | def ConvBlock(self, layers, filters): 55 | model = self.model 56 | for i in range(layers): 57 | model.add(ZeroPadding2D((1, 1))) 58 | model.add(Convolution2D(filters, 3, 3, activation='relu')) 59 | model.add(MaxPooling2D((2, 2), strides=(2, 2))) 60 | 61 | 62 | def FCBlock(self): 63 | model = self.model 64 | model.add(Dense(4096, activation='relu')) 65 | model.add(BatchNormalization()) 66 | model.add(Dropout(0.5)) 67 | 68 | 69 | def create(self, size, include_top): 70 | if size != (224,224): 71 | include_top=False 72 | 73 | model = self.model = Sequential() 74 | model.add(Lambda(vgg_preprocess, input_shape=(3,)+size, output_shape=(3,)+size)) 75 | 76 | self.ConvBlock(2, 64) 77 | self.ConvBlock(2, 128) 78 | self.ConvBlock(3, 256) 79 | self.ConvBlock(3, 512) 80 | self.ConvBlock(3, 512) 81 | 82 | if not include_top: 83 | fname = 'vgg16_bn_conv.h5' 84 | model.load_weights(get_file(fname, self.FILE_PATH+fname, cache_subdir='models')) 85 | return 86 | 87 | model.add(Flatten()) 88 | self.FCBlock() 89 | self.FCBlock() 90 | model.add(Dense(1000, activation='softmax')) 91 | 92 | fname = 'vgg16_bn.h5' 93 | model.load_weights(get_file(fname, self.FILE_PATH+fname, cache_subdir='models')) 94 | 95 | 96 | def get_batches(self, path, gen=image.ImageDataGenerator(), shuffle=True, batch_size=8, class_mode='categorical'): 97 | return gen.flow_from_directory(path, target_size=(224,224), 98 | class_mode=class_mode, shuffle=shuffle, batch_size=batch_size) 99 | 100 | 101 | def ft(self, num): 102 | model = self.model 103 | model.pop() 104 | for layer in model.layers: layer.trainable=False 105 | model.add(Dense(num, activation='softmax')) 106 | self.compile() 107 | 108 | def finetune(self, batches): 109 | self.ft(batches.nb_class) 110 | 111 | classes = list(iter(batches.class_indices)) 112 | for c in batches.class_indices: 113 | classes[batches.class_indices[c]] = c 114 | self.classes = classes 115 | 116 | 117 | def compile(self, lr=0.001): 118 | self.model.compile(optimizer=Adam(lr=lr), 119 | loss='categorical_crossentropy', metrics=['accuracy']) 120 | 121 | 122 | def fit_data(self, trn, labels, val, val_labels, nb_epoch=1, batch_size=64): 123 | self.model.fit(trn, labels, nb_epoch=nb_epoch, 124 | validation_data=(val, val_labels), batch_size=batch_size) 125 | 126 | 127 | def fit(self, batches, val_batches, nb_epoch=1): 128 | self.model.fit_generator(batches, samples_per_epoch=batches.nb_sample, nb_epoch=nb_epoch, 129 | validation_data=val_batches, nb_val_samples=val_batches.nb_sample) 130 | 131 | 132 | def test(self, path, batch_size=8): 133 | test_batches = self.get_batches(path, shuffle=False, batch_size=batch_size, class_mode=None) 134 | return test_batches, self.model.predict_generator(test_batches, test_batches.nb_sample) 135 | 136 | -------------------------------------------------------------------------------- /deeplearning2/Keras-Tensorflow-Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stderr", 12 | "output_type": "stream", 13 | "text": [ 14 | "Using TensorFlow backend.\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "import tensorflow as tf\n", 20 | "sess = tf.Session()\n", 21 | "\n", 22 | "from keras import backend as K\n", 23 | "K.set_session(sess)" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": { 30 | "collapsed": true 31 | }, 32 | "outputs": [], 33 | "source": [ 34 | "# this placeholder will contain our input digits, as flat vectors\n", 35 | "img = tf.placeholder(tf.float32, shape=(None, 784))" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 3, 41 | "metadata": { 42 | "collapsed": true 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "from keras.layers import Dense\n", 47 | "\n", 48 | "# Keras layers can be called on TensorFlow tensors:\n", 49 | "x = Dense(128, activation='relu')(img) # fully-connected layer with 128 units and ReLU activation\n", 50 | "x = Dense(128, activation='relu')(x)\n", 51 | "preds = Dense(10, activation='softmax')(x) # output layer with 10 units and a softmax activation" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 4, 57 | "metadata": { 58 | "collapsed": true 59 | }, 60 | "outputs": [], 61 | "source": [ 62 | "labels = tf.placeholder(tf.float32, shape=(None, 10))\n", 63 | "\n", 64 | "from keras.objectives import categorical_crossentropy\n", 65 | "loss = tf.reduce_mean(categorical_crossentropy(labels, preds))" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 6, 71 | "metadata": { 72 | "collapsed": false 73 | }, 74 | "outputs": [ 75 | { 76 | "ename": "URLError", 77 | "evalue": "", 78 | "output_type": "error", 79 | "traceback": [ 80 | "\u001b[0;31m----------------------------------------------------------------\u001b[0m", 81 | "\u001b[0;31mTimeoutError\u001b[0m Traceback (most recent call last)", 82 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/urllib/request.py\u001b[0m in \u001b[0;36mdo_open\u001b[0;34m(self, http_class, req, **http_conn_args)\u001b[0m\n\u001b[1;32m 1253\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1254\u001b[0;31m \u001b[0mh\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_method\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mselector\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1255\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# timeout error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 83 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/http/client.py\u001b[0m in \u001b[0;36mrequest\u001b[0;34m(self, method, url, body, headers)\u001b[0m\n\u001b[1;32m 1105\u001b[0m \u001b[0;34m\"\"\"Send a complete request to the server.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1106\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_send_request\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbody\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1107\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 84 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/http/client.py\u001b[0m in \u001b[0;36m_send_request\u001b[0;34m(self, method, url, body, headers)\u001b[0m\n\u001b[1;32m 1150\u001b[0m \u001b[0mbody\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_encode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbody\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'body'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1151\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mendheaders\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbody\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1152\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 85 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/http/client.py\u001b[0m in \u001b[0;36mendheaders\u001b[0;34m(self, message_body)\u001b[0m\n\u001b[1;32m 1101\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mCannotSendHeader\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1102\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_send_output\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmessage_body\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1103\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 86 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/http/client.py\u001b[0m in \u001b[0;36m_send_output\u001b[0;34m(self, message_body)\u001b[0m\n\u001b[1;32m 933\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 934\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 935\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mmessage_body\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 87 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/http/client.py\u001b[0m in \u001b[0;36msend\u001b[0;34m(self, data)\u001b[0m\n\u001b[1;32m 876\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mauto_open\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 877\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconnect\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 878\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 88 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/http/client.py\u001b[0m in \u001b[0;36mconnect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 848\u001b[0m self.sock = self._create_connection(\n\u001b[0;32m--> 849\u001b[0;31m (self.host,self.port), self.timeout, self.source_address)\n\u001b[0m\u001b[1;32m 850\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msock\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msetsockopt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mIPPROTO_TCP\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTCP_NODELAY\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 89 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/socket.py\u001b[0m in \u001b[0;36mcreate_connection\u001b[0;34m(address, timeout, source_address)\u001b[0m\n\u001b[1;32m 710\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0merr\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 711\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 712\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 90 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/socket.py\u001b[0m in \u001b[0;36mcreate_connection\u001b[0;34m(address, timeout, source_address)\u001b[0m\n\u001b[1;32m 701\u001b[0m \u001b[0msock\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbind\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msource_address\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 702\u001b[0;31m \u001b[0msock\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconnect\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msa\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 703\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0msock\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 91 | "\u001b[0;31mTimeoutError\u001b[0m: [Errno 110] Connection timed out", 92 | "\nDuring handling of the above exception, another exception occurred:\n", 93 | "\u001b[0;31mURLError\u001b[0m Traceback (most recent call last)", 94 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexamples\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtutorials\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmnist\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0minput_data\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmnist_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput_data\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread_data_sets\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'MNIST_data'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mone_hot\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mtrain_step\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mGradientDescentOptimizer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0.5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mminimize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mloss\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0msess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mas_default\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 95 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py\u001b[0m in \u001b[0;36mread_data_sets\u001b[0;34m(train_dir, fake_data, one_hot, dtype, reshape, validation_size)\u001b[0m\n\u001b[1;32m 209\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 210\u001b[0m local_file = base.maybe_download(TRAIN_IMAGES, train_dir,\n\u001b[0;32m--> 211\u001b[0;31m SOURCE_URL + TRAIN_IMAGES)\n\u001b[0m\u001b[1;32m 212\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlocal_file\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'rb'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 213\u001b[0m \u001b[0mtrain_images\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mextract_images\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 96 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py\u001b[0m in \u001b[0;36mmaybe_download\u001b[0;34m(filename, work_directory, source_url)\u001b[0m\n\u001b[1;32m 206\u001b[0m \u001b[0mfilepath\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwork_directory\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfilename\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 207\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mgfile\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mExists\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilepath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 208\u001b[0;31m \u001b[0mtemp_file_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0murlretrieve_with_retry\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msource_url\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 209\u001b[0m \u001b[0mgfile\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtemp_file_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfilepath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 210\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mgfile\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mGFile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilepath\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 97 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py\u001b[0m in \u001b[0;36mwrapped_fn\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 163\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mdelay\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdelays\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 164\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 165\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 166\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# pylint: disable=broad-except)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 167\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mis_retriable\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 98 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py\u001b[0m in \u001b[0;36murlretrieve_with_retry\u001b[0;34m(url, filename)\u001b[0m\n\u001b[1;32m 188\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0mretry\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minitial_delay\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax_delay\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m16.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mis_retriable\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0m_is_retriable\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 189\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0murlretrieve_with_retry\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfilename\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 190\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0murllib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0murlretrieve\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfilename\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 191\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 192\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 99 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/urllib/request.py\u001b[0m in \u001b[0;36murlretrieve\u001b[0;34m(url, filename, reporthook, data)\u001b[0m\n\u001b[1;32m 186\u001b[0m \u001b[0murl_type\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpath\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msplittype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 187\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 188\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mcontextlib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclosing\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0murlopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mfp\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 189\u001b[0m \u001b[0mheaders\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minfo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 190\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 100 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/urllib/request.py\u001b[0m in \u001b[0;36murlopen\u001b[0;34m(url, data, timeout, cafile, capath, cadefault, context)\u001b[0m\n\u001b[1;32m 161\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 162\u001b[0m \u001b[0mopener\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_opener\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 163\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mopener\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 164\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 165\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0minstall_opener\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mopener\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 101 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/urllib/request.py\u001b[0m in \u001b[0;36mopen\u001b[0;34m(self, fullurl, data, timeout)\u001b[0m\n\u001b[1;32m 464\u001b[0m \u001b[0mreq\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmeth\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreq\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 465\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 466\u001b[0;31m \u001b[0mresponse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_open\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreq\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 467\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 468\u001b[0m \u001b[0;31m# post-process response\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 102 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/urllib/request.py\u001b[0m in \u001b[0;36m_open\u001b[0;34m(self, req, data)\u001b[0m\n\u001b[1;32m 482\u001b[0m \u001b[0mprotocol\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mreq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 483\u001b[0m result = self._call_chain(self.handle_open, protocol, protocol +\n\u001b[0;32m--> 484\u001b[0;31m '_open', req)\n\u001b[0m\u001b[1;32m 485\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 486\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 103 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/urllib/request.py\u001b[0m in \u001b[0;36m_call_chain\u001b[0;34m(self, chain, kind, meth_name, *args)\u001b[0m\n\u001b[1;32m 442\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mhandler\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mhandlers\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 443\u001b[0m \u001b[0mfunc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhandler\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmeth_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 444\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 445\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 446\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 104 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/urllib/request.py\u001b[0m in \u001b[0;36mhttp_open\u001b[0;34m(self, req)\u001b[0m\n\u001b[1;32m 1280\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1281\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mhttp_open\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreq\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1282\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdo_open\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhttp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclient\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mHTTPConnection\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreq\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1283\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1284\u001b[0m \u001b[0mhttp_request\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mAbstractHTTPHandler\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdo_request_\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 105 | "\u001b[0;32m/home/jhoward/anaconda3/lib/python3.5/urllib/request.py\u001b[0m in \u001b[0;36mdo_open\u001b[0;34m(self, http_class, req, **http_conn_args)\u001b[0m\n\u001b[1;32m 1254\u001b[0m \u001b[0mh\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_method\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mselector\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1255\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# timeout error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1256\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mURLError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1257\u001b[0m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mh\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetresponse\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1258\u001b[0m \u001b[0;32mexcept\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 106 | "\u001b[0;31mURLError\u001b[0m: " 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "from tensorflow.examples.tutorials.mnist import input_data\n", 112 | "mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True)\n", 113 | "\n", 114 | "train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)\n", 115 | "with sess.as_default():\n", 116 | " for i in range(100):\n", 117 | " batch = mnist_data.train.next_batch(50)\n", 118 | " train_step.run(feed_dict={img: batch[0],\n", 119 | " labels: batch[1]})" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": { 126 | "collapsed": true 127 | }, 128 | "outputs": [], 129 | "source": [ 130 | "mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": { 137 | "collapsed": true 138 | }, 139 | "outputs": [], 140 | "source": [] 141 | } 142 | ], 143 | "metadata": { 144 | "anaconda-cloud": {}, 145 | "kernelspec": { 146 | "display_name": "Python [conda root]", 147 | "language": "python", 148 | "name": "conda-root-py" 149 | }, 150 | "language_info": { 151 | "codemirror_mode": { 152 | "name": "ipython", 153 | "version": 3 154 | }, 155 | "file_extension": ".py", 156 | "mimetype": "text/x-python", 157 | "name": "python", 158 | "nbconvert_exporter": "python", 159 | "pygments_lexer": "ipython3", 160 | "version": "3.5.2" 161 | } 162 | }, 163 | "nbformat": 4, 164 | "nbformat_minor": 2 165 | } 166 | -------------------------------------------------------------------------------- /deeplearning2/LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /deeplearning2/README.md: -------------------------------------------------------------------------------- 1 | # Part 2: Cutting Edge Deep Learning for Coders 2 | 3 | This is the repo for [Cutting Edge Deep Learning for Coders](course.fast.ai/part2.html). See the course page for details on these notebooks and the associated videos. 4 | -------------------------------------------------------------------------------- /deeplearning2/attention_wrapper.py: -------------------------------------------------------------------------------- 1 | from keras import backend as K 2 | from keras.initializations import zero 3 | from keras.engine import InputSpec 4 | from keras.models import Sequential 5 | from keras.layers import LSTM, activations, Wrapper, Recurrent, Layer 6 | 7 | class Attention(Layer): 8 | def __init__(self, fn_rnn, nlayers=1, **kwargs): 9 | self.supports_masking = True 10 | self.fn_rnn = fn_rnn 11 | self.nlayers = nlayers 12 | self.input_spec = [InputSpec(ndim=3), InputSpec(ndim=3)] 13 | super(Attention, self).__init__(**kwargs) 14 | 15 | 16 | def all_attrs(self, name): 17 | return sum([getattr(layer, name, []) for layer in self.layers], []) 18 | 19 | 20 | def w(self, dims, init, name): 21 | return self.add_weight(dims, init, name.format(self.name)) 22 | 23 | 24 | def build(self, input_shape): 25 | self.enc_shape, self.dec_shape = input_shape 26 | assert len(self.enc_shape) >= 3 27 | self.layers = [self.fn_rnn() for i in range(self.nlayers)] 28 | nb_samples, nb_time, nb_dims = self.dec_shape 29 | l0 = self.layers[0] 30 | 31 | out_shape = self.get_output_shape_for(input_shape) 32 | for layer in self.layers: 33 | if not layer.built: layer.build(out_shape) 34 | 35 | init = l0.init 36 | out_dim = l0.output_dim 37 | self.W1 = self.w((self.enc_shape[-1], nb_dims), init, '{}_W1') 38 | self.W2 = self.w((out_dim, nb_dims), init, '{}_W2') 39 | self.b2 = self.w((nb_dims,), zero, '{}_b2') 40 | self.V = self.w((nb_dims,), init, '{}_V') 41 | self.W3 = self.w((nb_dims+out_dim, out_dim), init, '{}_W3') 42 | self.b3 = self.w((out_dim,), zero, '{}_b3') 43 | 44 | self.trainable_weights += self.all_attrs( 'trainable_weights') 45 | self.non_trainable_weights += self.all_attrs( 'non_trainable_weights') 46 | self.losses += self.all_attrs( 'losses') 47 | self.updates = self.all_attrs( 'updates') 48 | self.constraints = getattr(self.layers[0], 'constraints', {}) # FIXME 49 | super(Attention, self).build(input_shape) 50 | 51 | 52 | def get_output_shape_for(self, input_shape): 53 | return self.layers[0].get_output_shape_for(input_shape[1]) 54 | 55 | 56 | def step(self, x, states): 57 | h = states[0] 58 | enc_output = states[-1] 59 | xW1 = states[-2] 60 | 61 | hW2 = K.expand_dims(K.dot(h,self.W2)+self.b2, 1) 62 | u = K.tanh(xW1+hW2) 63 | a = K.expand_dims(K.softmax(K.sum(self.V*u,2)), -1) 64 | Xa = K.sum(a*enc_output,1) 65 | h = K.dot(K.concatenate([x,Xa],1),self.W3)+self.b3 66 | 67 | for layer in self.layers: h, new_states = layer.step(h, states) 68 | return h, new_states 69 | 70 | 71 | def get_constants(self, enc_output, constants): 72 | constants.append(K.dot(enc_output,self.W1)) 73 | constants.append(enc_output) 74 | return constants 75 | 76 | 77 | def compute_mask(self, input, mask): 78 | return self.layers[0].compute_mask(input, mask[1]) 79 | 80 | 81 | def call(self, x, mask=None): 82 | l0 = self.layers[0] 83 | enc_output, dec_input = x 84 | 85 | if l0.stateful: initial_states = l0.states 86 | else: initial_states = l0.get_initial_states(dec_input) 87 | constants = l0.get_constants(dec_input) 88 | constants = self.get_constants(enc_output, constants) 89 | preprocessed_input = l0.preprocess_input(dec_input) 90 | 91 | last_output, outputs, states = K.rnn(self.step, preprocessed_input, 92 | initial_states, go_backwards=l0.go_backwards, mask=mask[1], 93 | constants=constants, unroll=l0.unroll, input_length=self.dec_shape[1]) 94 | if l0.stateful: 95 | self.updates = [] 96 | for i in range(len(states)): 97 | self.updates.append((l0.states[i], states[i])) 98 | 99 | return outputs if l0.return_sequences else last_output 100 | 101 | -------------------------------------------------------------------------------- /deeplearning2/batcher.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import print_function 3 | 4 | import numpy as np 5 | import re 6 | from scipy import linalg 7 | import scipy.ndimage as ndi 8 | from six.moves import range 9 | import os 10 | import threading 11 | import warnings 12 | 13 | class Iterator(object): 14 | 15 | def __init__(self, n, batch_size, shuffle, seed): 16 | self.n = n 17 | self.batch_size = batch_size 18 | self.shuffle = shuffle 19 | self.batch_index = 0 20 | self.total_batches_seen = 0 21 | self.lock = threading.Lock() 22 | self.index_generator = self._flow_index(n, batch_size, shuffle, seed) 23 | 24 | def reset(self): 25 | self.batch_index = 0 26 | 27 | def _flow_index(self, n, batch_size=32, shuffle=False, seed=None): 28 | # ensure self.batch_index is 0 29 | self.reset() 30 | while 1: 31 | if seed is not None: 32 | np.random.seed(seed + self.total_batches_seen) 33 | if self.batch_index == 0: 34 | index_array = np.arange(n) 35 | if shuffle: 36 | index_array = np.random.permutation(n) 37 | 38 | current_index = (self.batch_index * batch_size) % n 39 | if n >= current_index + batch_size: 40 | current_batch_size = batch_size 41 | self.batch_index += 1 42 | else: 43 | current_batch_size = n - current_index 44 | self.batch_index = 0 45 | self.total_batches_seen += 1 46 | yield (index_array[current_index: current_index + current_batch_size], 47 | current_index, current_batch_size) 48 | 49 | def __iter__(self): 50 | # needed if we want to do something like: 51 | # for x, y in data_gen.flow(...): 52 | return self 53 | 54 | def __next__(self, *args, **kwargs): 55 | return self.next(*args, **kwargs) 56 | 57 | class Batcher(Iterator): 58 | def __init__(self, x, y, batch_size=64, shuffle=False, seed=None, proc_fn=None): 59 | if y is not None and len(x) != len(y): 60 | raise ValueError('X (images tensor) and y (labels) ' 61 | 'should have the same length. ' 62 | 'Found: X.shape = %s, y.shape = %s' % 63 | (np.asarray(x).shape, np.asarray(y).shape)) 64 | self.x = np.asarray(x) 65 | self.y = np.asarray(y) if y is not None else None 66 | self.proc_fn = proc_fn 67 | super(Batcher, self).__init__(x.shape[0], batch_size, shuffle, seed) 68 | 69 | 70 | def next(self): 71 | with self.lock: 72 | index_array, current_index, current_batch_size = next(self.index_generator) 73 | batch_x = np.zeros(tuple([current_batch_size] + list(self.x.shape)[1:]), self.x.dtype) 74 | for i, j in enumerate(index_array): 75 | x = self.x[j] 76 | #x = self.image_data_generator.standardize(x) 77 | batch_x[i] = x 78 | res = batch_x if self.y is None else batch_x, self.y[index_array] 79 | if self.proc_fn: res=self.proc_fn(res) 80 | return res 81 | 82 | -------------------------------------------------------------------------------- /deeplearning2/bcolz_array_iterator.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import bcolz 3 | import threading 4 | 5 | class BcolzArrayIterator(object): 6 | """ 7 | Returns an iterator object into Bcolz carray files 8 | Original version by Thiago Ramon Gonçalves Montoya 9 | Docs (and discovery) by @MPJansen 10 | Refactoring, performance improvements, fixes by Jeremy Howard j@fast.ai 11 | :Example: 12 | X = bcolz.open('file_path/feature_file.bc', mode='r') 13 | y = bcolz.open('file_path/label_file.bc', mode='r') 14 | trn_batches = BcolzArrayIterator(X, y, batch_size=64, shuffle=True) 15 | model.fit_generator(generator=trn_batches, samples_per_epoch=trn_batches.N, nb_epoch=1) 16 | :param X: Input features 17 | :param y: (optional) Input labels 18 | :param w: (optional) Input feature weights 19 | :param batch_size: (optional) Batch size, defaults to 32 20 | :param shuffle: (optional) Shuffle batches, defaults to false 21 | :param seed: (optional) Provide a seed to shuffle, defaults to a random seed 22 | :rtype: BcolzArrayIterator 23 | >>> A = np.random.random((32*10 + 17, 10, 10)) 24 | >>> c = bcolz.carray(A, rootdir='test.bc', mode='w', expectedlen=A.shape[0], chunklen=16) 25 | >>> c.flush() 26 | >>> Bc = bcolz.open('test.bc') 27 | >>> bc_it = BcolzArrayIterator(Bc, shuffle=True) 28 | >>> C_list = [next(bc_it) for i in range(11)] 29 | >>> C = np.concatenate(C_list) 30 | >>> np.allclose(sorted(A.flatten()), sorted(C.flatten())) 31 | True 32 | """ 33 | 34 | def __init__(self, X, y=None, w=None, batch_size=32, shuffle=False, seed=None): 35 | if y is not None and len(X) != len(y): 36 | raise ValueError('X (features) and y (labels) should have the same length' 37 | 'Found: X.shape = %s, y.shape = %s' % (X.shape, y.shape)) 38 | if w is not None and len(X) != len(w): 39 | raise ValueError('X (features) and w (weights) should have the same length' 40 | 'Found: X.shape = %s, w.shape = %s' % (X.shape, w.shape)) 41 | if batch_size % X.chunklen != 0: 42 | raise ValueError('batch_size needs to be a multiple of X.chunklen') 43 | 44 | self.chunks_per_batch = batch_size // X.chunklen 45 | self.X = X 46 | self.y = y if y is not None else None 47 | self.w = w if w is not None else None 48 | self.N = X.shape[0] 49 | self.batch_size = batch_size 50 | self.batch_index = 0 51 | self.total_batches_seen = 0 52 | self.lock = threading.Lock() 53 | self.shuffle = shuffle 54 | self.seed = seed 55 | 56 | 57 | def reset(self): self.batch_index = 0 58 | 59 | 60 | def next(self): 61 | with self.lock: 62 | if self.batch_index == 0: 63 | if self.seed is not None: 64 | np.random.seed(self.seed + self.total_batches_seen) 65 | self.index_array = (np.random.permutation(self.X.nchunks + 1) if self.shuffle 66 | else np.arange(self.X.nchunks + 1)) 67 | 68 | #batches_x = np.zeros((self.batch_size,)+self.X.shape[1:]) 69 | batches_x, batches_y, batches_w = [],[],[] 70 | for i in range(self.chunks_per_batch): 71 | current_index = self.index_array[self.batch_index] 72 | if current_index == self.X.nchunks: 73 | batches_x.append(self.X.leftover_array[:self.X.leftover_elements]) 74 | current_batch_size = self.X.leftover_elements 75 | else: 76 | batches_x.append(self.X.chunks[current_index][:]) 77 | current_batch_size = self.X.chunklen 78 | self.batch_index += 1 79 | self.total_batches_seen += 1 80 | 81 | idx = current_index * self.X.chunklen 82 | if not self.y is None: batches_y.append(self.y[idx: idx + current_batch_size]) 83 | if not self.w is None: batches_w.append(self.w[idx: idx + current_batch_size]) 84 | if self.batch_index >= len(self.index_array): 85 | self.batch_index = 0 86 | break 87 | 88 | batch_x = np.concatenate(batches_x) 89 | if self.y is None: return batch_x 90 | 91 | batch_y = np.concatenate(batches_y) 92 | if self.w is None: return batch_x, batch_y 93 | 94 | batch_w = np.concatenate(batches_w) 95 | return batch_x, batch_y, batch_w 96 | 97 | 98 | def __iter__(self): return self 99 | 100 | def __next__(self, *args, **kwargs): return self.next(*args, **kwargs) 101 | 102 | -------------------------------------------------------------------------------- /deeplearning2/bcolz_iter_test.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true, 8 | "deletable": true, 9 | "editable": true 10 | }, 11 | "outputs": [], 12 | "source": [ 13 | "from bcolz_array_iterator2 import BcolzArrayIterator2 " 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 2, 19 | "metadata": { 20 | "collapsed": true, 21 | "deletable": true, 22 | "editable": true 23 | }, 24 | "outputs": [], 25 | "source": [ 26 | "from bcolz import carray" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 3, 32 | "metadata": { 33 | "collapsed": true, 34 | "deletable": true, 35 | "editable": true 36 | }, 37 | "outputs": [], 38 | "source": [ 39 | "import numpy as np" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 4, 45 | "metadata": { 46 | "collapsed": false, 47 | "deletable": true, 48 | "editable": true 49 | }, 50 | "outputs": [ 51 | { 52 | "data": { 53 | "text/plain": [ 54 | "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])" 55 | ] 56 | }, 57 | "execution_count": 4, 58 | "metadata": {}, 59 | "output_type": "execute_result" 60 | } 61 | ], 62 | "source": [ 63 | "x = np.arange(14); x" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 5, 69 | "metadata": { 70 | "collapsed": false, 71 | "deletable": true, 72 | "editable": true 73 | }, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])" 79 | ] 80 | }, 81 | "execution_count": 5, 82 | "metadata": {}, 83 | "output_type": "execute_result" 84 | } 85 | ], 86 | "source": [ 87 | "y = np.arange(14); y" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 16, 93 | "metadata": { 94 | "collapsed": true, 95 | "deletable": true, 96 | "editable": true 97 | }, 98 | "outputs": [], 99 | "source": [ 100 | "x = carray(x, chunklen=3)\n", 101 | "y = carray(y, chunklen=3)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 17, 107 | "metadata": { 108 | "collapsed": false, 109 | "deletable": true, 110 | "editable": true 111 | }, 112 | "outputs": [], 113 | "source": [ 114 | "b = BcolzArrayIterator2(x, y, shuffle=True, batch_size=3)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 18, 120 | "metadata": { 121 | "collapsed": false, 122 | "deletable": true, 123 | "editable": true 124 | }, 125 | "outputs": [ 126 | { 127 | "data": { 128 | "text/plain": [ 129 | "14" 130 | ] 131 | }, 132 | "execution_count": 18, 133 | "metadata": {}, 134 | "output_type": "execute_result" 135 | } 136 | ], 137 | "source": [ 138 | "b.N" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 19, 144 | "metadata": { 145 | "collapsed": false, 146 | "deletable": true, 147 | "editable": true 148 | }, 149 | "outputs": [ 150 | { 151 | "data": { 152 | "text/plain": [ 153 | "5" 154 | ] 155 | }, 156 | "execution_count": 19, 157 | "metadata": {}, 158 | "output_type": "execute_result" 159 | } 160 | ], 161 | "source": [ 162 | "nit = len(x)//b.batch_size+1; nit" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 20, 168 | "metadata": { 169 | "collapsed": false, 170 | "deletable": true, 171 | "editable": true 172 | }, 173 | "outputs": [], 174 | "source": [ 175 | "for j in range(10000):\n", 176 | " bx,by = list(zip(*[next(b) for i in range(nit)]))\n", 177 | " nx = np.concatenate(bx)\n", 178 | " ny = np.concatenate(by)\n", 179 | " assert(np.allclose(nx,ny))\n", 180 | " assert(len(np.unique(nx))==len(nx))" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 21, 186 | "metadata": { 187 | "collapsed": false, 188 | "deletable": true, 189 | "editable": true 190 | }, 191 | "outputs": [ 192 | { 193 | "data": { 194 | "text/plain": [ 195 | "[(array([ 9, 10, 11]), array([ 9, 10, 11])),\n", 196 | " (array([6, 7, 8]), array([6, 7, 8])),\n", 197 | " (array([0, 1, 2]), array([0, 1, 2])),\n", 198 | " (array([12, 13]), array([12, 13])),\n", 199 | " (array([3, 4, 5]), array([3, 4, 5])),\n", 200 | " (array([ 9, 10, 11]), array([ 9, 10, 11])),\n", 201 | " (array([0, 1, 2]), array([0, 1, 2])),\n", 202 | " (array([3, 4, 5]), array([3, 4, 5])),\n", 203 | " (array([6, 7, 8]), array([6, 7, 8])),\n", 204 | " (array([12, 13]), array([12, 13])),\n", 205 | " (array([12, 13]), array([12, 13])),\n", 206 | " (array([6, 7, 8]), array([6, 7, 8])),\n", 207 | " (array([3, 4, 5]), array([3, 4, 5])),\n", 208 | " (array([0, 1, 2]), array([0, 1, 2])),\n", 209 | " (array([ 9, 10, 11]), array([ 9, 10, 11])),\n", 210 | " (array([3, 4, 5]), array([3, 4, 5])),\n", 211 | " (array([6, 7, 8]), array([6, 7, 8])),\n", 212 | " (array([ 9, 10, 11]), array([ 9, 10, 11])),\n", 213 | " (array([12, 13]), array([12, 13])),\n", 214 | " (array([0, 1, 2]), array([0, 1, 2]))]" 215 | ] 216 | }, 217 | "execution_count": 21, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [ 223 | "[next(b) for i in range(20)]" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": null, 229 | "metadata": { 230 | "collapsed": true, 231 | "deletable": true, 232 | "editable": true 233 | }, 234 | "outputs": [], 235 | "source": [] 236 | } 237 | ], 238 | "metadata": { 239 | "kernelspec": { 240 | "display_name": "Python [conda root]", 241 | "language": "python", 242 | "name": "conda-root-py" 243 | }, 244 | "language_info": { 245 | "codemirror_mode": { 246 | "name": "ipython", 247 | "version": 3 248 | }, 249 | "file_extension": ".py", 250 | "mimetype": "text/x-python", 251 | "name": "python", 252 | "nbconvert_exporter": "python", 253 | "pygments_lexer": "ipython3", 254 | "version": "3.6.0" 255 | } 256 | }, 257 | "nbformat": 4, 258 | "nbformat_minor": 2 259 | } 260 | -------------------------------------------------------------------------------- /deeplearning2/dcgan.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.parallel 4 | 5 | class DCGAN_D(nn.Module): 6 | def conv_block(self, main, name, inf, of, a, b, c, bn=True): 7 | main.add_module(f'{name}-{inf}.{of}.conv', nn.Conv2d(inf, of, a, b, c, bias=False)) 8 | main.add_module(f'{name}-{of}.batchnorm', nn.BatchNorm2d(of)) 9 | main.add_module(f'{name}-{of}.relu', nn.LeakyReLU(0.2, inplace=True)) 10 | 11 | def __init__(self, isize, nc, ndf, ngpu, n_extra_layers=0): 12 | super(DCGAN_D, self).__init__() 13 | self.ngpu = ngpu 14 | assert isize % 16 == 0, "isize has to be a multiple of 16" 15 | 16 | main = nn.Sequential() 17 | # input is nc x isize x isize 18 | self.conv_block(main, 'initial', nc, ndf, 4, 2, 1, False) 19 | csize, cndf = isize / 2, ndf 20 | 21 | for t in range(n_extra_layers): 22 | self.conv_block(main, f'extra-{t}', cndf, cndf, 3, 1, 1) 23 | 24 | while csize > 4: 25 | self.conv_block(main, 'pyramid', cndf, cndf*2, 4, 2, 1) 26 | cndf *= 2; csize /= 2 27 | 28 | # state size. K x 4 x 4 29 | main.add_module(f'final.{cndf}-1.conv', nn.Conv2d(cndf, 1, 4, 1, 0, bias=False)) 30 | self.main = main 31 | 32 | 33 | def forward(self, input): 34 | gpu_ids = None 35 | if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu > 1: 36 | gpu_ids = range(self.ngpu) 37 | output = nn.parallel.data_parallel(self.main, input, gpu_ids) 38 | output = output.mean(0) 39 | return output.view(1) 40 | 41 | class DCGAN_G(nn.Module): 42 | def deconv_block(self, main, name, inf, of, a, b, c, bn=True): 43 | main.add_module(f'{name}-{inf}.{of}.convt', nn.ConvTranspose2d(inf, of, a, b, c, bias=False)) 44 | main.add_module(f'{name}-{of}.batchnorm', nn.BatchNorm2d(of)) 45 | main.add_module(f'{name}-{of}.relu', nn.ReLU(inplace=True)) 46 | 47 | def __init__(self, isize, nz, nc, ngf, ngpu, n_extra_layers=0): 48 | super(DCGAN_G, self).__init__() 49 | self.ngpu = ngpu 50 | assert isize % 16 == 0, "isize has to be a multiple of 16" 51 | 52 | cngf, tisize = ngf//2, 4 53 | while tisize != isize: cngf *= 2; tisize *= 2 54 | 55 | main = nn.Sequential() 56 | self.deconv_block(main, 'initial', nz, cngf, 4, 1, 0) 57 | 58 | csize, cndf = 4, cngf 59 | while csize < isize//2: 60 | self.deconv_block(main, 'pyramid', cngf, cngf//2, 4, 2, 1) 61 | cngf //= 2; csize *= 2 62 | 63 | for t in range(n_extra_layers): 64 | self.deconv_block(main, f'extra-{t}', cngf, cngf, 3, 1, 1) 65 | 66 | main.add_module(f'final.{cngf}-{nc}.convt', nn.ConvTranspose2d(cngf, nc, 4, 2, 1, bias=False)) 67 | main.add_module(f'final.{nc}.tanh', nn.Tanh()) 68 | self.main = main 69 | 70 | def forward(self, input): 71 | gpu_ids = None 72 | if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu > 1: 73 | gpu_ids = range(self.ngpu) 74 | return nn.parallel.data_parallel(self.main, input, gpu_ids) 75 | 76 | -------------------------------------------------------------------------------- /deeplearning2/kmeans.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import math, numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | def plot_data(centroids, data, n_samples): 7 | colour = plt.cm.rainbow(np.linspace(0,1,len(centroids))) 8 | for i, centroid in enumerate(centroids): 9 | samples = data[i*n_samples:(i+1)*n_samples] 10 | plt.scatter(samples[:,0], samples[:,1], c=colour[i], s=1) 11 | plt.plot(centroid[0], centroid[1], markersize=10, marker="x", color='k', mew=5) 12 | plt.plot(centroid[0], centroid[1], markersize=5, marker="x", color='m', mew=2) 13 | 14 | 15 | def all_distances(a, b): 16 | diff = tf.squared_difference(tf.expand_dims(a, 0), tf.expand_dims(b,1)) 17 | return tf.reduce_sum(diff, axis=2) 18 | 19 | 20 | class Kmeans(object): 21 | 22 | def __init__(self, data, n_clusters): 23 | self.n_data, self.n_dim = data.shape 24 | self.n_clusters = n_clusters 25 | self.data = data 26 | self.v_data = tf.Variable(data) 27 | self.n_samples = self.n_data//self.n_clusters 28 | 29 | def run(self): 30 | tf.global_variables_initializer().run() 31 | initial_centroids = self.find_initial_centroids(self.n_clusters).eval() 32 | curr_centroids = tf.Variable(initial_centroids) 33 | nearest_indices = self.assign_to_nearest(curr_centroids) 34 | updated_centroids = self.update_centroids(nearest_indices) 35 | # Begin main algorithm 36 | tf.global_variables_initializer().run() 37 | c = initial_centroids 38 | for i in range(10): 39 | c2 = curr_centroids.assign(updated_centroids).eval() 40 | if np.allclose(c,c2): break 41 | c=c2 42 | return c2 43 | 44 | 45 | def find_initial_centroids(self, k): 46 | r_index = tf.random_uniform([1], 0, self.n_data, dtype=tf.int32) 47 | r = tf.expand_dims(self.v_data[tf.squeeze(r_index)], dim=1) 48 | initial_centroids = [] 49 | for i in range(k): 50 | dist = all_distances(self.v_data, r) 51 | farthest_index = tf.argmax(tf.reduce_min(dist, axis=0), 0) 52 | farthest_point = self.v_data[tf.to_int32(farthest_index)] 53 | initial_centroids.append(farthest_point) 54 | r = tf.stack(initial_centroids) 55 | return r 56 | 57 | def choose_random_centroids(self): 58 | n_samples = tf.shape(v_data)[0] 59 | random_indices = tf.random_shuffle(tf.range(0, n_samples)) 60 | centroid_indices = random_indices[:self.n_clusters] 61 | return tf.gather(self.v_data, centroid_indices) 62 | 63 | def assign_to_nearest(self, centroids): 64 | return tf.argmin(all_distances(self.v_data, centroids), 0) 65 | 66 | def update_centroids(self, nearest_indices): 67 | partitions = tf.dynamic_partition(self.v_data, tf.to_int32(nearest_indices), self.n_clusters) 68 | return tf.concat([tf.expand_dims(tf.reduce_mean(partition, 0), 0) 69 | for partition in partitions], 0) 70 | 71 | -------------------------------------------------------------------------------- /deeplearning2/rossman_exp.py: -------------------------------------------------------------------------------- 1 | train_ratio=0.9 2 | use_dict=True 3 | use_scaler=False 4 | init_emb=False 5 | split_contins=True 6 | samp_size = 100000 7 | #samp_size = 0 8 | 9 | import math, keras, datetime, pandas as pd, numpy as np, keras.backend as K 10 | import matplotlib.pyplot as plt, xgboost, operator, random, pickle, os 11 | from sklearn_pandas import DataFrameMapper 12 | from sklearn.preprocessing import LabelEncoder, Imputer, StandardScaler 13 | from keras.models import Model 14 | from keras.layers import merge, Input 15 | from keras.layers.core import Dense, Activation, Reshape, Flatten, Dropout 16 | from keras.layers.embeddings import Embedding 17 | from keras.optimizers import Adam 18 | from keras.layers.normalization import BatchNormalization 19 | from keras.regularizers import l2 20 | from keras import initializations 21 | np.set_printoptions(4) 22 | 23 | cfg = K.tf.ConfigProto() 24 | cfg.gpu_options.allow_growth = True 25 | K.set_session(K.tf.Session(config=cfg)) 26 | 27 | os.chdir('data/rossman') 28 | cat_var_dict = {'Store': 50, 'DayOfWeek': 6, 'Year': 2, 'Month': 6, 29 | 'Day': 10, 'StateHoliday': 3, 'CompetitionMonthsOpen': 2, 30 | 'Promo2Weeks': 1, 'StoreType': 2, 'Assortment': 3, 'PromoInterval': 3, 31 | 'CompetitionOpenSinceYear': 4, 'Promo2SinceYear': 4, 'State': 6, 32 | 'Week': 2, 'Events': 4, 'Promo_fw': 1, 33 | 'Promo_bw': 1, 'StateHoliday_fw': 1, 34 | 'StateHoliday_bw': 1, 'SchoolHoliday_fw': 1, 35 | 'SchoolHoliday_bw': 1} 36 | 37 | cats, contins= [o for n,o in np.load('vars.npz').items()] 38 | y = np.load('deps.npz').items()[0][1] 39 | 40 | if samp_size != 0: 41 | np.random.seed(42) 42 | idxs = sorted(np.random.choice(len(y), samp_size, replace=False)) 43 | cats= cats[idxs] 44 | contins= contins[idxs] 45 | y= y[idxs] 46 | 47 | n=len(y) 48 | train_size = int(n*train_ratio) 49 | 50 | contins_trn_orig, contins_val_orig = contins[:train_size], contins[train_size:] 51 | cats_trn, cats_val = cats[:train_size], cats[train_size:] 52 | y_trn, y_val = y[:train_size], y[train_size:] 53 | 54 | contin_map_fit = pickle.load(open('contin_maps.pickle', 'rb')) 55 | cat_map_fit = pickle.load(open('cat_maps.pickle', 'rb')) 56 | 57 | def cat_map_info(feat): return feat[0], len(feat[1].classes_) 58 | 59 | co_enc = StandardScaler().fit(contins_trn_orig) 60 | tf_contins_trn = co_enc.transform(contins_trn_orig) 61 | tf_contins_val = co_enc.transform(contins_val_orig) 62 | 63 | 64 | """ 65 | def rmspe(y_pred, targ = y_valid_orig): 66 | return math.sqrt(np.square((targ - y_pred)/targ).mean()) 67 | def log_max_inv(preds, mx = max_log_y): return np.exp(preds * mx) 68 | def normalize_inv(preds): return preds * ystd + ymean 69 | """ 70 | 71 | 72 | def split_cols(arr): return np.hsplit(arr,arr.shape[1]) 73 | 74 | 75 | def emb_init(shape, name=None): 76 | return initializations.uniform(shape, scale=0.6/shape[1], name=name) 77 | 78 | 79 | def get_emb(feat): 80 | name, c = cat_map_info(feat) 81 | if use_dict: 82 | c2 = cat_var_dict[name] 83 | else: 84 | c2 = (c+2)//3 85 | if c2>50: c2=50 86 | inp = Input((1,), dtype='int64', name=name+'_in') 87 | if init_emb: 88 | u = Flatten(name=name+'_flt')(Embedding(c, c2, input_length=1)(inp)) 89 | else: 90 | u = Flatten(name=name+'_flt')(Embedding(c, c2, input_length=1, init=emb_init)(inp)) 91 | return inp,u 92 | 93 | 94 | def get_contin(feat): 95 | name = feat[0][0] 96 | inp = Input((1,), name=name+'_in') 97 | return inp, Dense(1, name=name+'_d')(inp) 98 | 99 | 100 | def split_data(): 101 | if split_contins: 102 | map_train = split_cols(cats_trn) + split_cols(contins_trn) 103 | map_valid = split_cols(cats_val) + split_cols(contins_val) 104 | else: 105 | map_train = split_cols(cats_trn) + [contins_trn] 106 | map_valid = split_cols(cats_val) + [contins_val] 107 | return (map_train, map_valid) 108 | 109 | 110 | def get_contin_one(): 111 | n_contin = contins_trn.shape[1] 112 | contin_inp = Input((n_contin,), name='contin') 113 | contin_out = BatchNormalization()(contin_inp) 114 | return contin_inp, contin_out 115 | 116 | 117 | def train(model, map_train, map_valid, bs=128, ne=10): 118 | return model.fit(map_train, y_trn, batch_size=bs, nb_epoch=ne, 119 | verbose=0, validation_data=(map_valid, y_val)) 120 | 121 | 122 | def get_model(): 123 | if split_contins: 124 | conts = [get_contin(feat) for feat in contin_map_fit.features] 125 | cont_out = [d for inp,d in conts] 126 | cont_inp = [inp for inp,d in conts] 127 | else: 128 | contin_inp, contin_out = get_contin_one() 129 | cont_out = [contin_out] 130 | cont_inp = [contin_inp] 131 | 132 | embs = [get_emb(feat) for feat in cat_map_fit.features] 133 | x = merge([emb for inp,emb in embs] + cont_out, mode='concat') 134 | 135 | x = Dropout(0.02)(x) 136 | x = Dense(1000, activation='relu', init='uniform')(x) 137 | x = Dense(500, activation='relu', init='uniform')(x) 138 | x = Dense(1, activation='sigmoid')(x) 139 | 140 | model = Model([inp for inp,emb in embs] + cont_inp, x) 141 | model.compile('adam', 'mean_absolute_error') 142 | #model.compile(Adam(), 'mse') 143 | return model 144 | 145 | for split_contins in [True, False]: 146 | for use_dict in [True, False]: 147 | for use_scaler in [True, False]: 148 | for init_emb in [True, False]: 149 | print ({'split_contins':split_contins, 'use_dict':use_dict, 150 | 'use_scaler':use_scaler, 'init_emb':init_emb}) 151 | if use_scaler: 152 | contins_trn = tf_contins_trn 153 | contins_val = tf_contins_val 154 | else: 155 | contins_trn = contins_trn_orig 156 | contins_val = contins_val_orig 157 | 158 | map_train, map_valid = split_data() 159 | model = get_model() 160 | hist = np.array(train(model, map_train, map_valid, 128, 10) 161 | .history['val_loss']) 162 | print(hist) 163 | print(hist.min()) 164 | 165 | -------------------------------------------------------------------------------- /deeplearning2/tiramisu-pytorch.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# One Hundred Layers Tiramisu" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import torch\n", 19 | "import torch.nn as nn\n", 20 | "import torch.nn.functional as F\n", 21 | "from collections import OrderedDict" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "### Initial Conv Block" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 29, 34 | "metadata": { 35 | "collapsed": true 36 | }, 37 | "outputs": [], 38 | "source": [ 39 | "class _FirstConv(nn.Sequential):\n", 40 | " def __init__(self, num_input_features):\n", 41 | " super(_FirstConv, self).__init__()\n", 42 | " self.add_module('conv0', nn.Conv2d(3, num_input_features, kernel_size=7, stride=2, padding=3, bias=False))\n", 43 | " self.add_module('norm0', nn.BatchNorm2d(num_input_features))\n", 44 | " self.add_module('relu0', nn.ReLU(inplace=True))\n", 45 | " self.add_module('pool0', nn.MaxPool2d(kernel_size=3, stride=2, padding=1))" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 30, 51 | "metadata": { 52 | "collapsed": false 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "# Test\n", 57 | "conv1 = _FirstConv(5)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "### Dense Layer" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 31, 70 | "metadata": { 71 | "collapsed": true 72 | }, 73 | "outputs": [], 74 | "source": [ 75 | "class _DenseLayer(nn.Sequential):\n", 76 | " def __init__(self, num_input_features, growth_rate, bn_size, drop_rate):\n", 77 | " super(_DenseLayer, self).__init__()\n", 78 | " self.add_module('norm.1', nn.BatchNorm2d(num_input_features)),\n", 79 | " self.add_module('relu.1', nn.ReLU(inplace=True)),\n", 80 | " self.add_module('conv.1', nn.Conv2d(num_input_features, bn_size *\n", 81 | " growth_rate, kernel_size=1, stride=1, bias=False)),\n", 82 | " self.add_module('norm.2', nn.BatchNorm2d(bn_size * growth_rate)),\n", 83 | " self.add_module('relu.2', nn.ReLU(inplace=True)),\n", 84 | " self.add_module('conv.2', nn.Conv2d(bn_size * growth_rate, growth_rate,\n", 85 | " kernel_size=3, stride=1, padding=1, bias=False)),\n", 86 | " self.drop_rate = drop_rate\n", 87 | "\n", 88 | " def forward(self, x):\n", 89 | " new_features = super(_DenseLayer, self).forward(x)\n", 90 | " if self.drop_rate > 0:\n", 91 | " new_features = F.dropout(new_features, p=self.drop_rate, training=self.training)\n", 92 | " return torch.cat([x, new_features], 1)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 32, 98 | "metadata": { 99 | "collapsed": true 100 | }, 101 | "outputs": [], 102 | "source": [ 103 | "# Test\n", 104 | "dense1 = _DenseLayer(5,3,1,.5)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "### Dense Block" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 33, 117 | "metadata": { 118 | "collapsed": true 119 | }, 120 | "outputs": [], 121 | "source": [ 122 | "class _DenseBlock(nn.Sequential):\n", 123 | " def __init__(self, num_layers, num_input_features, bn_size, growth_rate, drop_rate):\n", 124 | " super(_DenseBlock, self).__init__()\n", 125 | " for i in range(num_layers):\n", 126 | " layer = _DenseLayer(num_input_features + i * growth_rate, growth_rate, bn_size, drop_rate)\n", 127 | " self.add_module('denselayer%d' % (i + 1), layer)" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 34, 133 | "metadata": { 134 | "collapsed": true 135 | }, 136 | "outputs": [], 137 | "source": [ 138 | "# Test\n", 139 | "denseBlock1 = _DenseBlock(4,4,1,4,.5)" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "### Transition Up" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 35, 152 | "metadata": { 153 | "collapsed": true 154 | }, 155 | "outputs": [], 156 | "source": [ 157 | "class _TransitionUp(nn.Sequential):\n", 158 | " def __init__(self, num_input_features, num_output_features):\n", 159 | " super(_TransitionUp, self).__init__()\n", 160 | " self.add_module('norm', nn.BatchNorm2d(num_input_features))\n", 161 | " self.add_module('relu', nn.ReLU(inplace=True))\n", 162 | " self.add_module('conv', nn.Conv2d(num_input_features, num_output_features,\n", 163 | " kernel_size=1, stride=1, bias=False))\n", 164 | " self.add_module('pool', nn.AvgPool2d(kernel_size=2, stride=2))" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 36, 170 | "metadata": { 171 | "collapsed": false 172 | }, 173 | "outputs": [], 174 | "source": [ 175 | "# Test\n", 176 | "transUp = _TransitionUp(5,10)" 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": {}, 182 | "source": [ 183 | "### Transition Down" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 37, 189 | "metadata": { 190 | "collapsed": true 191 | }, 192 | "outputs": [], 193 | "source": [ 194 | "class _TransitionDown(nn.Sequential):\n", 195 | " def __init__(self, num_input_features, num_output_features):\n", 196 | " super(_TransitionDown, self).__init__()\n", 197 | " self.add_module('norm', nn.BatchNorm2d(num_input_features))\n", 198 | " self.add_module('relu', nn.ReLU(inplace=True))\n", 199 | " self.add_module('conv', nn.Conv2d(num_input_features, num_output_features,\n", 200 | " kernel_size=1, stride=1, bias=False))\n", 201 | " self.add_module('pool', nn.AvgPool2d(kernel_size=2, stride=2))" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 38, 207 | "metadata": { 208 | "collapsed": true 209 | }, 210 | "outputs": [], 211 | "source": [ 212 | "# Test\n", 213 | "transDown = _TransitionDown(5,10)" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": {}, 219 | "source": [ 220 | "### Final Model" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 45, 226 | "metadata": { 227 | "collapsed": true 228 | }, 229 | "outputs": [], 230 | "source": [ 231 | "class FCDenseNet(nn.Module):\n", 232 | " r\"\"\"FC-DenseNet model class, based on\n", 233 | " `\"The One Hundred Layers Tiramisu: Fully Convolutional DenseNets for Semantic Segmentation\" `\n", 234 | "\n", 235 | " Args:\n", 236 | " growth_rate (int) - how many filters to add each layer (`k` in paper)\n", 237 | " block_config (list of 4 ints) - how many layers in each pooling block\n", 238 | " num_init_features (int) - the number of filters to learn in the first convolution layer\n", 239 | " bn_size (int) - multiplicative factor for number of bottle neck layers\n", 240 | " (i.e. bn_size * k features in the bottleneck layer)\n", 241 | " drop_rate (float) - dropout rate after each dense layer\n", 242 | " num_classes (int) - number of classification classes\n", 243 | " \"\"\"\n", 244 | " def __init__(self, growth_rate=32, block_config=(6, 12, 24, 16),\n", 245 | " num_init_features=64, bn_size=4, drop_rate=0, num_classes=1000):\n", 246 | "\n", 247 | " super(FCDenseNet, self).__init__()\n", 248 | " \n", 249 | " self.features = nn.Sequential()\n", 250 | " self.features.add_module('firstConv', _FirstConv(num_init_features))\n", 251 | "\n", 252 | " # Each denseblock\n", 253 | " num_features = num_init_features\n", 254 | " for i, num_layers in enumerate(block_config):\n", 255 | " block = _DenseBlock(num_layers=num_layers, num_input_features=num_features,\n", 256 | " bn_size=bn_size, growth_rate=growth_rate, drop_rate=drop_rate)\n", 257 | " self.features.add_module('denseblock%d' % (i + 1), block)\n", 258 | " num_features = num_features + num_layers * growth_rate\n", 259 | " if i != len(block_config) - 1:\n", 260 | " trans = _Transition(num_input_features=num_features, num_output_features=num_features // 2)\n", 261 | " self.features.add_module('transition%d' % (i + 1), trans)\n", 262 | " num_features = num_features // 2\n", 263 | "\n", 264 | " # Final batch norm\n", 265 | " self.features.add_module('norm5', nn.BatchNorm2d(num_features))\n", 266 | "\n", 267 | " # Linear layer\n", 268 | " self.classifier = nn.Linear(num_features, num_classes)\n", 269 | "\n", 270 | " def forward(self, x):\n", 271 | " features = self.features(x)\n", 272 | " out = F.relu(features, inplace=True)\n", 273 | " out = F.avg_pool2d(out, kernel_size=7).view(features.size(0), -1)\n", 274 | " out = self.classifier(out)\n", 275 | " return out" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 46, 281 | "metadata": { 282 | "collapsed": false 283 | }, 284 | "outputs": [], 285 | "source": [ 286 | "# Test\n", 287 | "model = FCDenseNet()" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": null, 293 | "metadata": { 294 | "collapsed": true 295 | }, 296 | "outputs": [], 297 | "source": [] 298 | } 299 | ], 300 | "metadata": { 301 | "kernelspec": { 302 | "display_name": "Python 3", 303 | "language": "python", 304 | "name": "python3" 305 | }, 306 | "language_info": { 307 | "codemirror_mode": { 308 | "name": "ipython", 309 | "version": 3 310 | }, 311 | "file_extension": ".py", 312 | "mimetype": "text/x-python", 313 | "name": "python", 314 | "nbconvert_exporter": "python", 315 | "pygments_lexer": "ipython3", 316 | "version": "3.6.2" 317 | }, 318 | "latex_envs": { 319 | "LaTeX_envs_menu_present": true, 320 | "autocomplete": true, 321 | "bibliofile": "biblio.bib", 322 | "cite_by": "apalike", 323 | "current_citInitial": 1, 324 | "eqLabelWithNumbers": true, 325 | "eqNumInitial": 1, 326 | "hotkeys": { 327 | "equation": "Ctrl-E", 328 | "itemize": "Ctrl-I" 329 | }, 330 | "labels_anchors": false, 331 | "latex_user_defs": false, 332 | "report_style_numbering": false, 333 | "user_envs_cfg": false 334 | } 335 | }, 336 | "nbformat": 4, 337 | "nbformat_minor": 2 338 | } 339 | -------------------------------------------------------------------------------- /deeplearning2/torch_utils.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch import optim, nn, FloatTensor as FT 3 | import torch.nn.parallel 4 | import torch.utils.data 5 | from torch.backends import cudnn 6 | from torchvision import datasets, transforms, utils as vutils 7 | from torch.autograd import Variable 8 | 9 | import operator 10 | 11 | def unit_prefix(x, n=1): 12 | for i in range(n): x = x.unsqueeze(0) 13 | return x 14 | 15 | def align(x, y, start_dim=2): 16 | xd, yd = x.dim(), y.dim() 17 | if xd > yd: y = unit_prefix(y, xd - yd) 18 | elif yd > xd: x = unit_prefix(x, yd - xd) 19 | 20 | xs, ys = list(x.size()), list(y.size()) 21 | nd = len(ys) 22 | for i in range(start_dim, nd): 23 | td = nd-i-1 24 | if ys[td]==1: ys[td] = xs[td] 25 | elif xs[td]==1: xs[td] = ys[td] 26 | return x.expand(*xs), y.expand(*ys) 27 | 28 | def dot(x, y): 29 | assert(1 0.95: 56 | label_position = height - (y_height * 0.06) 57 | else: 58 | label_position = height + (y_height * 0.01) 59 | txt = ax.text(rect.get_x() + rect.get_width()/2., label_position, 60 | fmt % height, ha='center', va='bottom') 61 | txt.set_path_effects([PathEffects.withStroke(linewidth=3, foreground='w')]) 62 | 63 | 64 | def column_chart(lbls, vals, val_lbls='%.2f'): 65 | n = len(lbls) 66 | p = plt.bar(np.arange(n), vals) 67 | plt.xticks(np.arange(n), lbls) 68 | if val_lbls: autolabel(p, val_lbls) 69 | 70 | 71 | def save_array(fname, arr): 72 | c=bcolz.carray(arr, rootdir=fname, mode='w') 73 | c.flush() 74 | 75 | 76 | def load_array(fname): return bcolz.open(fname)[:] 77 | 78 | 79 | def load_glove(loc): 80 | return (load_array(loc+'.dat'), 81 | pickle.load(open(loc+'_words.pkl','rb'), encoding='latin1'), 82 | pickle.load(open(loc+'_idx.pkl','rb'), encoding='latin1')) 83 | 84 | def plot_multi(im, dim=(4,4), figsize=(6,6), **kwargs ): 85 | plt.figure(figsize=figsize) 86 | for i,img in enumerate(im): 87 | plt.subplot(*((dim)+(i+1,))) 88 | plt.imshow(img, **kwargs) 89 | plt.axis('off') 90 | plt.tight_layout() 91 | 92 | 93 | def plot_train(hist): 94 | h = hist.history 95 | if 'acc' in h: 96 | meas='acc' 97 | loc='lower right' 98 | else: 99 | meas='loss' 100 | loc='upper right' 101 | plt.plot(hist.history[meas]) 102 | plt.plot(hist.history['val_'+meas]) 103 | plt.title('model '+meas) 104 | plt.ylabel(meas) 105 | plt.xlabel('epoch') 106 | plt.legend(['train', 'validation'], loc=loc) 107 | 108 | 109 | def fit_gen(gen, fn, eval_fn, nb_iter): 110 | for i in range(nb_iter): 111 | fn(*next(gen)) 112 | if i % (nb_iter//10) == 0: eval_fn() 113 | 114 | 115 | def wrap_config(layer): 116 | return {'class_name': layer.__class__.__name__, 'config': layer.get_config()} 117 | 118 | 119 | def copy_layer(layer): return layer_from_config(wrap_config(layer)) 120 | 121 | 122 | def copy_layers(layers): return [copy_layer(layer) for layer in layers] 123 | 124 | 125 | def copy_weights(from_layers, to_layers): 126 | for from_layer,to_layer in zip(from_layers, to_layers): 127 | to_layer.set_weights(from_layer.get_weights()) 128 | 129 | 130 | def copy_model(m): 131 | res = Sequential(copy_layers(m.layers)) 132 | copy_weights(m.layers, res.layers) 133 | return res 134 | 135 | 136 | def insert_layer(model, new_layer, index): 137 | res = Sequential() 138 | for i,layer in enumerate(model.layers): 139 | if i==index: res.add(new_layer) 140 | copied = layer_from_config(wrap_config(layer)) 141 | res.add(copied) 142 | copied.set_weights(layer.get_weights()) 143 | return res 144 | 145 | -------------------------------------------------------------------------------- /deeplearning2/vgg16.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from __future__ import print_function 3 | from __future__ import absolute_import 4 | 5 | import warnings 6 | 7 | from keras.models import Model 8 | from keras.layers import Flatten, Dense, Input, Dropout 9 | from keras.layers import Convolution2D, MaxPooling2D 10 | from keras.engine.topology import get_source_inputs 11 | from keras.utils.layer_utils import convert_all_kernels_in_model 12 | from keras.utils.data_utils import get_file 13 | from keras import backend as K 14 | from keras.applications.imagenet_utils import decode_predictions, preprocess_input, _obtain_input_shape 15 | 16 | 17 | PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/' 18 | TF_WEIGHTS = 'vgg16_weights_tf_dim_ordering_tf_kernels.h5' 19 | TF_WEIGHTS_NO_TOP = 'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5' 20 | 21 | 22 | def VGG16(include_top=True, weights='imagenet', 23 | input_tensor=None, input_shape=None): 24 | if weights not in {'imagenet', None}: 25 | raise ValueError('The `weights` argument should be either ' 26 | '`None` (random initialization) or `imagenet` ' 27 | '(pre-training on ImageNet).') 28 | # Determine proper input shape 29 | input_shape = _obtain_input_shape(input_shape, 30 | default_size=224, 31 | min_size=48, 32 | dim_ordering=K.image_dim_ordering(), 33 | include_top=include_top) 34 | 35 | if input_tensor is None: 36 | img_input = Input(shape=input_shape) 37 | else: 38 | if not K.is_keras_tensor(input_tensor): 39 | img_input = Input(tensor=input_tensor, shape=input_shape) 40 | else: 41 | img_input = input_tensor 42 | # Block 1 43 | x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv1')(img_input) 44 | x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv2')(x) 45 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) 46 | 47 | # Block 2 48 | x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='block2_conv1')(x) 49 | x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='block2_conv2')(x) 50 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) 51 | 52 | # Block 3 53 | x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv1')(x) 54 | x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv2')(x) 55 | x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv3')(x) 56 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) 57 | 58 | # Block 4 59 | x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv1')(x) 60 | x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv2')(x) 61 | x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv3')(x) 62 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) 63 | 64 | # Block 5 65 | x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv1')(x) 66 | x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv2')(x) 67 | x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv3')(x) 68 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) 69 | 70 | if include_top: 71 | # Classification block 72 | x = Flatten(name='flatten')(x) 73 | x = Dense(4096, activation='relu', name='fc1')(x) 74 | x = Dropout(0.5)(x) 75 | x = Dense(4096, activation='relu', name='fc2')(x) 76 | x = Dropout(0.5)(x) 77 | x = Dense(1000, activation='softmax', name='predictions')(x) 78 | 79 | # Ensure that the model takes into account 80 | # any potential predecessors of `input_tensor`. 81 | if input_tensor is not None: 82 | inputs = get_source_inputs(input_tensor) 83 | else: 84 | inputs = img_input 85 | # Create model. 86 | model = Model(inputs, x, name='vgg16') 87 | 88 | # load weights 89 | if weights == 'imagenet': 90 | if K.image_dim_ordering() == 'tf': 91 | wname = TF_WEIGHTS if include_top else TF_WEIGHTS_NO_TOP 92 | weights_path = get_file(wname, PATH+wname, cache_subdir='models') 93 | model.load_weights(weights_path) 94 | return model 95 | -------------------------------------------------------------------------------- /deeplearning2/vgg16_avg.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | from __future__ import absolute_import 3 | 4 | import warnings 5 | 6 | from keras.models import Model 7 | from keras.layers import Flatten, Dense, Input 8 | from keras.layers import Convolution2D, AveragePooling2D 9 | from keras.engine.topology import get_source_inputs 10 | from keras.utils.layer_utils import convert_all_kernels_in_model 11 | from keras.utils.data_utils import get_file 12 | from keras import backend as K 13 | from keras.applications.imagenet_utils import decode_predictions, preprocess_input, _obtain_input_shape 14 | 15 | 16 | TH_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_th_dim_ordering_th_kernels.h5' 17 | TF_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5' 18 | TH_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_th_dim_ordering_th_kernels_notop.h5' 19 | TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5' 20 | 21 | 22 | def VGG16_Avg(include_top=True, weights='imagenet', 23 | input_tensor=None, input_shape=None, 24 | classes=1000): 25 | if weights not in {'imagenet', None}: 26 | raise ValueError('The `weights` argument should be either ' 27 | '`None` (random initialization) or `imagenet` ' 28 | '(pre-training on ImageNet).') 29 | 30 | if weights == 'imagenet' and include_top and classes != 1000: 31 | raise ValueError('If using `weights` as imagenet with `include_top`' 32 | ' as true, `classes` should be 1000') 33 | # Determine proper input shape 34 | input_shape = _obtain_input_shape(input_shape, 35 | default_size=224, 36 | min_size=48, 37 | dim_ordering=K.image_dim_ordering(), 38 | include_top=include_top) 39 | 40 | if input_tensor is None: 41 | img_input = Input(shape=input_shape) 42 | else: 43 | if not K.is_keras_tensor(input_tensor): 44 | img_input = Input(tensor=input_tensor, shape=input_shape) 45 | else: 46 | img_input = input_tensor 47 | # Block 1 48 | x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv1')(img_input) 49 | x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv2')(x) 50 | x = AveragePooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) 51 | 52 | # Block 2 53 | x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='block2_conv1')(x) 54 | x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='block2_conv2')(x) 55 | x = AveragePooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) 56 | 57 | # Block 3 58 | x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv1')(x) 59 | x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv2')(x) 60 | x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv3')(x) 61 | x = AveragePooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) 62 | 63 | # Block 4 64 | x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv1')(x) 65 | x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv2')(x) 66 | x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv3')(x) 67 | x = AveragePooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) 68 | 69 | # Block 5 70 | x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv1')(x) 71 | x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv2')(x) 72 | x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv3')(x) 73 | x = AveragePooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) 74 | 75 | if include_top: 76 | # Classification block 77 | x = Flatten(name='flatten')(x) 78 | x = Dense(4096, activation='relu', name='fc1')(x) 79 | x = Dense(4096, activation='relu', name='fc2')(x) 80 | x = Dense(classes, activation='softmax', name='predictions')(x) 81 | 82 | # Ensure that the model takes into account 83 | # any potential predecessors of `input_tensor`. 84 | if input_tensor is not None: 85 | inputs = get_source_inputs(input_tensor) 86 | else: 87 | inputs = img_input 88 | # Create model. 89 | model = Model(inputs, x, name='vgg16') 90 | 91 | # load weights 92 | if weights == 'imagenet': 93 | if K.image_dim_ordering() == 'th': 94 | if include_top: 95 | weights_path = get_file('vgg16_weights_th_dim_ordering_th_kernels.h5', 96 | TH_WEIGHTS_PATH, 97 | cache_subdir='models') 98 | else: 99 | weights_path = get_file('vgg16_weights_th_dim_ordering_th_kernels_notop.h5', 100 | TH_WEIGHTS_PATH_NO_TOP, 101 | cache_subdir='models') 102 | model.load_weights(weights_path) 103 | if K.backend() == 'tensorflow': 104 | warnings.warn('You are using the TensorFlow backend, yet you ' 105 | 'are using the Theano ' 106 | 'image dimension ordering convention ' 107 | '(`image_dim_ordering="th"`). ' 108 | 'For best performance, set ' 109 | '`image_dim_ordering="tf"` in ' 110 | 'your Keras config ' 111 | 'at ~/.keras/keras.json.') 112 | convert_all_kernels_in_model(model) 113 | else: 114 | if include_top: 115 | weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels.h5', 116 | TF_WEIGHTS_PATH, 117 | cache_subdir='models') 118 | else: 119 | weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', 120 | TF_WEIGHTS_PATH_NO_TOP, 121 | cache_subdir='models') 122 | model.load_weights(weights_path) 123 | if K.backend() == 'theano': 124 | convert_all_kernels_in_model(model) 125 | return model 126 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | jupyter 2 | matplotlib 3 | pillow 4 | keras==1.2.2 5 | theano 6 | tensorflow 7 | pandas 8 | scikit-learn 9 | bcolz 10 | sympy -------------------------------------------------------------------------------- /setup/README.md: -------------------------------------------------------------------------------- 1 | ## List of files 2 | | File | Purpose | 3 | | --------------------- | ------------- | 4 | | `aws-alias.sh` | Command aliases that make AWS server management easier. | 5 | | `instal-gpu-azure.sh` | Installs required software on an Azure Ubuntu server. Instructions available on the [wiki](http://wiki.fast.ai/index.php/Azure_install). | 6 | | `install-gpu.sh` | Installs required software on an Ubuntu machine. Instructions available on the [wiki](http://wiki.fast.ai/index.php/Ubuntu_installation). | 7 | | `setup_instance.sh` | Sets up an AWS environment for use in the course including a server that has the required software installed. This script is used by `setup_p2.sh` or `setup_t2.sh`. You probably don't need to call it by itself. | 8 | | `setup_p2.sh` and `setup_t2.sh` | Configure environment variables for use in `setup_instance.sh`. These files call `setup_instance.sh`, which does the actual work of setting up the AWS instance. | 9 | 10 | ## Setup Instructions 11 | 12 | ### AWS 13 | If you haven't already, view the video at http://course.fast.ai/lessons/aws.html for the steps you need to complete before running these scripts. More information is available on the [wiki](http://wiki.fast.ai/index.php/AWS_install). 14 | 1. Decide if you will use a GPU server or a standard server. GPU servers process deep learning jobs faster than general purpose servers, but they cost more per hour. Compare server pricing at https://aws.amazon.com/ec2/pricing/on-demand/. 15 | 2. Download `setup_p2.sh` if you decided on the GPU server or `setup_t2.sh` for the general purpose server. Also download `setup_instance.sh`. 16 | 3. Run the command `bash setup_p2.sh` or `bash setup_t2.sh`, depending on the file you downloaded. Run the command locally from the folder where the files were downloaded. Running `bash setup_p2.sh` sets up a p2.xlarge GPU server. `bash setup_p2.sh` sets up a t2.xlarge general purpose server. 17 | 4. The script will set up the server you selected along with other pieces of AWS infrastructure. When it finishes, it will print out the command for connecting to the new server. The server is preloaded with the software required for the course. 18 | 5. Learn how to use the provided AWS aliases on the [wiki](http://wiki.fast.ai/index.php/AWS_install#Once_you_create_an_instance). 19 | 20 | ### Azure 21 | Once you have an Azure GPU server set up, download and run the `install-gpu-azure.sh` script on that server. More instructions available on the [wiki](http://wiki.fast.ai/index.php/Azure_install). 22 | 23 | ### Ubuntu 24 | Download and run the `install-gpu.sh` script to install required software on an Ubuntu machine. More instructions available on the [wiki](http://wiki.fast.ai/index.php/Ubuntu_installation). -------------------------------------------------------------------------------- /setup/aws-alias.sh: -------------------------------------------------------------------------------- 1 | alias aws-get-p2='export instanceId=`aws ec2 describe-instances --filters "Name=instance-state-name,Values=stopped,Name=instance-type,Values=p2.xlarge" --query "Reservations[0].Instances[0].InstanceId"` && echo $instanceId' 2 | alias aws-get-t2='export instanceId=`aws ec2 describe-instances --filters "Name=instance-state-name,Values=stopped,Name=instance-type,Values=t2.xlarge" --query "Reservations[0].Instances[0].InstanceId"` && echo $instanceId' 3 | alias aws-start='aws ec2 start-instances --instance-ids $instanceId && aws ec2 wait instance-running --instance-ids $instanceId && export instanceIp=`aws ec2 describe-instances --filters "Name=instance-id,Values=$instanceId" --query "Reservations[0].Instances[0].PublicIpAddress"` && echo $instanceIp' 4 | alias aws-ip='export instanceIp=`aws ec2 describe-instances --filters "Name=instance-id,Values=$instanceId" --query "Reservations[0].Instances[0].PublicIpAddress"` && echo $instanceIp' 5 | alias aws-ssh='ssh -i ~/.ssh/aws-key-fast-ai.pem ubuntu@$instanceIp' 6 | alias aws-stop='aws ec2 stop-instances --instance-ids $instanceId' 7 | alias aws-state='aws ec2 describe-instances --instance-ids $instanceId --query "Reservations[0].Instances[0].State.Name"' 8 | 9 | 10 | if [[ `uname` == *"CYGWIN"* ]] 11 | then 12 | # This is cygwin. Use cygstart to open the notebook 13 | alias aws-nb='cygstart http://$instanceIp:8888' 14 | fi 15 | 16 | if [[ `uname` == *"Linux"* ]] 17 | then 18 | # This is linux. Use xdg-open to open the notebook 19 | alias aws-nb='xdg-open http://$instanceIp:8888' 20 | fi 21 | 22 | if [[ `uname` == *"Darwin"* ]] 23 | then 24 | # This is Mac. Use open to open the notebook 25 | alias aws-nb='open http://$instanceIp:8888' 26 | fi 27 | -------------------------------------------------------------------------------- /setup/install-gpu-azure.sh: -------------------------------------------------------------------------------- 1 | sudo apt-get update && sudo apt-get --assume-yes upgrade 2 | sudo apt-get --assume-yes install tmux build-essential gcc g++ make binutils 3 | sudo apt-get --assume-yes install software-properties-common 4 | 5 | wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.44-1_amd64.deb 6 | sudo dpkg -i cuda-repo-ubuntu1604_8.0.44-1_amd64.deb 7 | sudo apt-get update 8 | sudo apt-get -y install cuda 9 | # If you get an error like "could not insert 'nvidia_367': No such device" for the following command, restart the VM using command : sudo shutdown -r now 10 | sudo modprobe nvidia 11 | nvidia-smi 12 | 13 | mkdir downloads 14 | cd downloads 15 | wget https://repo.continuum.io/archive/Anaconda2-4.2.0-Linux-x86_64.sh 16 | bash Anaconda2-4.2.0-Linux-x86_64.sh -b 17 | echo 'export PATH="/usr/local/cuda/bin:$HOME/anaconda2/bin:$PATH"' >> ~/.bashrc 18 | export PATH="/usr/local/cuda/bin:$HOME/anaconda2/bin:$PATH" 19 | conda install -y bcolz 20 | conda upgrade -y --all 21 | 22 | pip install theano 23 | echo "[global] 24 | device = gpu 25 | floatX = float32" > ~/.theanorc 26 | 27 | pip install keras==1.2.2 28 | mkdir ~/.keras 29 | echo '{ 30 | "image_dim_ordering": "th", 31 | "epsilon": 1e-07, 32 | "floatx": "float32", 33 | "backend": "theano" 34 | }' > ~/.keras/keras.json 35 | 36 | wget http://files.fast.ai/files/cudnn.tgz 37 | tar -zxf cudnn.tgz 38 | cd cuda 39 | sudo cp lib64/* /usr/local/cuda/lib64/ 40 | sudo cp include/* /usr/local/cuda/include/ 41 | 42 | jupyter notebook --generate-config 43 | jupass=`python -c "from notebook.auth import passwd; print(passwd())"` 44 | cd ~ 45 | echo "c.NotebookApp.password = u'"$jupass"'" >> .jupyter/jupyter_notebook_config.py 46 | echo "c.NotebookApp.ip = '*' 47 | c.NotebookApp.open_browser = False" >> .jupyter/jupyter_notebook_config.py 48 | mkdir nbs 49 | 50 | -------------------------------------------------------------------------------- /setup/install-gpu.sh: -------------------------------------------------------------------------------- 1 | # This script is designed to work with ubuntu 16.04 LTS 2 | 3 | # ensure system is updated and has basic build tools 4 | sudo apt-get update 5 | sudo apt-get --assume-yes upgrade 6 | sudo apt-get --assume-yes install tmux build-essential gcc g++ make binutils 7 | sudo apt-get --assume-yes install software-properties-common 8 | 9 | # download and install GPU drivers 10 | wget "http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.44-1_amd64.deb" -O "cuda-repo-ubuntu1604_8.0.44-1_amd64.deb" 11 | 12 | sudo dpkg -i cuda-repo-ubuntu1604_8.0.44-1_amd64.deb 13 | sudo apt-get update 14 | sudo apt-get -y install cuda 15 | sudo modprobe nvidia 16 | nvidia-smi 17 | 18 | # install Anaconda for current user 19 | mkdir downloads 20 | cd downloads 21 | wget "https://repo.continuum.io/archive/Anaconda2-4.2.0-Linux-x86_64.sh" -O "Anaconda2-4.2.0-Linux-x86_64.sh" 22 | bash "Anaconda2-4.2.0-Linux-x86_64.sh" -b 23 | 24 | echo "export PATH=\"$HOME/anaconda2/bin:\$PATH\"" >> ~/.bashrc 25 | export PATH="$HOME/anaconda2/bin:$PATH" 26 | conda install -y bcolz 27 | conda upgrade -y --all 28 | 29 | # install and configure theano 30 | pip install theano 31 | echo "[global] 32 | device = gpu 33 | floatX = float32 34 | 35 | [cuda] 36 | root = /usr/local/cuda" > ~/.theanorc 37 | 38 | # install and configure keras 39 | pip install keras==1.2.2 40 | mkdir ~/.keras 41 | echo '{ 42 | "image_dim_ordering": "th", 43 | "epsilon": 1e-07, 44 | "floatx": "float32", 45 | "backend": "theano" 46 | }' > ~/.keras/keras.json 47 | 48 | # install cudnn libraries 49 | wget "http://files.fast.ai/files/cudnn.tgz" -O "cudnn.tgz" 50 | tar -zxf cudnn.tgz 51 | cd cuda 52 | sudo cp lib64/* /usr/local/cuda/lib64/ 53 | sudo cp include/* /usr/local/cuda/include/ 54 | 55 | # configure jupyter and prompt for password 56 | jupyter notebook --generate-config 57 | jupass=`python -c "from notebook.auth import passwd; print(passwd())"` 58 | echo "c.NotebookApp.password = u'"$jupass"'" >> $HOME/.jupyter/jupyter_notebook_config.py 59 | echo "c.NotebookApp.ip = '*' 60 | c.NotebookApp.open_browser = False" >> $HOME/.jupyter/jupyter_notebook_config.py 61 | 62 | # clone the fast.ai course repo and prompt to start notebook 63 | cd ~ 64 | git clone https://github.com/fastai/courses.git 65 | echo "\"jupyter notebook\" will start Jupyter on port 8888" 66 | echo "If you get an error instead, try restarting your session so your $PATH is updated" 67 | -------------------------------------------------------------------------------- /setup/setup_instance.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # This script should be invoked via setup_t2.sh or setup_p2.sh; those scripts 4 | # will export the right environment variables for this to succeed. 5 | 6 | # uncomment for debugging 7 | # set -x 8 | 9 | if [ -z "$ami" ] || [ -z "$instanceType" ]; then 10 | echo "Missing \$ami or \$instanceType; this script should be called from" 11 | echo "setup_t2.sh or setup_p2.sh!" 12 | exit 1 13 | fi 14 | 15 | # settings 16 | export name="fast-ai" 17 | export cidr="0.0.0.0/0" 18 | 19 | hash aws 2>/dev/null 20 | if [ $? -ne 0 ]; then 21 | echo >&2 "'aws' command line tool required, but not installed. Aborting." 22 | exit 1 23 | fi 24 | 25 | if [ -z "$(aws configure get aws_access_key_id)" ]; then 26 | echo "AWS credentials not configured. Aborting" 27 | exit 1 28 | fi 29 | 30 | export vpcId=$(aws ec2 create-vpc --cidr-block 10.0.0.0/28 --query 'Vpc.VpcId' --output text) 31 | aws ec2 create-tags --resources $vpcId --tags --tags Key=Name,Value=$name 32 | aws ec2 modify-vpc-attribute --vpc-id $vpcId --enable-dns-support "{\"Value\":true}" 33 | aws ec2 modify-vpc-attribute --vpc-id $vpcId --enable-dns-hostnames "{\"Value\":true}" 34 | 35 | export internetGatewayId=$(aws ec2 create-internet-gateway --query 'InternetGateway.InternetGatewayId' --output text) 36 | aws ec2 create-tags --resources $internetGatewayId --tags --tags Key=Name,Value=$name-gateway 37 | aws ec2 attach-internet-gateway --internet-gateway-id $internetGatewayId --vpc-id $vpcId 38 | 39 | export subnetId=$(aws ec2 create-subnet --vpc-id $vpcId --cidr-block 10.0.0.0/28 --query 'Subnet.SubnetId' --output text) 40 | aws ec2 create-tags --resources $subnetId --tags --tags Key=Name,Value=$name-subnet 41 | 42 | export routeTableId=$(aws ec2 create-route-table --vpc-id $vpcId --query 'RouteTable.RouteTableId' --output text) 43 | aws ec2 create-tags --resources $routeTableId --tags --tags Key=Name,Value=$name-route-table 44 | export routeTableAssoc=$(aws ec2 associate-route-table --route-table-id $routeTableId --subnet-id $subnetId --output text) 45 | aws ec2 create-route --route-table-id $routeTableId --destination-cidr-block 0.0.0.0/0 --gateway-id $internetGatewayId 46 | 47 | export securityGroupId=$(aws ec2 create-security-group --group-name $name-security-group --description "SG for fast.ai machine" --vpc-id $vpcId --query 'GroupId' --output text) 48 | # ssh 49 | aws ec2 authorize-security-group-ingress --group-id $securityGroupId --protocol tcp --port 22 --cidr $cidr 50 | # jupyter notebook 51 | aws ec2 authorize-security-group-ingress --group-id $securityGroupId --protocol tcp --port 8888-8898 --cidr $cidr 52 | 53 | if [ ! -d ~/.ssh ] 54 | then 55 | mkdir ~/.ssh 56 | fi 57 | 58 | if [ ! -f ~/.ssh/aws-key-$name.pem ] 59 | then 60 | aws ec2 create-key-pair --key-name aws-key-$name --query 'KeyMaterial' --output text > ~/.ssh/aws-key-$name.pem 61 | chmod 400 ~/.ssh/aws-key-$name.pem 62 | fi 63 | 64 | export instanceId=$(aws ec2 run-instances --image-id $ami --count 1 --instance-type $instanceType --key-name aws-key-$name --security-group-ids $securityGroupId --subnet-id $subnetId --associate-public-ip-address --block-device-mapping "[ { \"DeviceName\": \"/dev/sda1\", \"Ebs\": { \"VolumeSize\": 128, \"VolumeType\": \"gp2\" } } ]" --query 'Instances[0].InstanceId' --output text) 65 | aws ec2 create-tags --resources $instanceId --tags --tags Key=Name,Value=$name-gpu-machine 66 | export allocAddr=$(aws ec2 allocate-address --domain vpc --query 'AllocationId' --output text) 67 | 68 | echo Waiting for instance start... 69 | aws ec2 wait instance-running --instance-ids $instanceId 70 | sleep 10 # wait for ssh service to start running too 71 | export assocId=$(aws ec2 associate-address --instance-id $instanceId --allocation-id $allocAddr --query 'AssociationId' --output text) 72 | export instanceUrl=$(aws ec2 describe-instances --instance-ids $instanceId --query 'Reservations[0].Instances[0].PublicDnsName' --output text) 73 | #export ebsVolume=$(aws ec2 describe-instance-attribute --instance-id $instanceId --attribute blockDeviceMapping --query BlockDeviceMappings[0].Ebs.VolumeId --output text) 74 | 75 | # reboot instance, because I was getting "Failed to initialize NVML: Driver/library version mismatch" 76 | # error when running the nvidia-smi command 77 | # see also http://forums.fast.ai/t/no-cuda-capable-device-is-detected/168/13 78 | aws ec2 reboot-instances --instance-ids $instanceId 79 | 80 | # save commands to file 81 | echo \# Connect to your instance: > $name-commands.txt # overwrite existing file 82 | echo ssh -i ~/.ssh/aws-key-$name.pem ubuntu@$instanceUrl >> $name-commands.txt 83 | echo \# Stop your instance: : >> $name-commands.txt 84 | echo aws ec2 stop-instances --instance-ids $instanceId >> $name-commands.txt 85 | echo \# Start your instance: >> $name-commands.txt 86 | echo aws ec2 start-instances --instance-ids $instanceId >> $name-commands.txt 87 | echo \# Reboot your instance: >> $name-commands.txt 88 | echo aws ec2 reboot-instances --instance-ids $instanceId >> $name-commands.txt 89 | echo "" 90 | # export vars to be sure 91 | echo export instanceId=$instanceId >> $name-commands.txt 92 | echo export subnetId=$subnetId >> $name-commands.txt 93 | echo export securityGroupId=$securityGroupId >> $name-commands.txt 94 | echo export instanceUrl=$instanceUrl >> $name-commands.txt 95 | echo export routeTableId=$routeTableId >> $name-commands.txt 96 | echo export name=$name >> $name-commands.txt 97 | echo export vpcId=$vpcId >> $name-commands.txt 98 | echo export internetGatewayId=$internetGatewayId >> $name-commands.txt 99 | echo export subnetId=$subnetId >> $name-commands.txt 100 | echo export allocAddr=$allocAddr >> $name-commands.txt 101 | echo export assocId=$assocId >> $name-commands.txt 102 | echo export routeTableAssoc=$routeTableAssoc >> $name-commands.txt 103 | 104 | # save delete commands for cleanup 105 | echo "#!/bin/bash" > $name-remove.sh # overwrite existing file 106 | echo aws ec2 disassociate-address --association-id $assocId >> $name-remove.sh 107 | echo aws ec2 release-address --allocation-id $allocAddr >> $name-remove.sh 108 | 109 | # volume gets deleted with the instance automatically 110 | echo aws ec2 terminate-instances --instance-ids $instanceId >> $name-remove.sh 111 | echo aws ec2 wait instance-terminated --instance-ids $instanceId >> $name-remove.sh 112 | echo aws ec2 delete-security-group --group-id $securityGroupId >> $name-remove.sh 113 | 114 | echo aws ec2 disassociate-route-table --association-id $routeTableAssoc >> $name-remove.sh 115 | echo aws ec2 delete-route-table --route-table-id $routeTableId >> $name-remove.sh 116 | 117 | echo aws ec2 detach-internet-gateway --internet-gateway-id $internetGatewayId --vpc-id $vpcId >> $name-remove.sh 118 | echo aws ec2 delete-internet-gateway --internet-gateway-id $internetGatewayId >> $name-remove.sh 119 | echo aws ec2 delete-subnet --subnet-id $subnetId >> $name-remove.sh 120 | 121 | echo aws ec2 delete-vpc --vpc-id $vpcId >> $name-remove.sh 122 | echo echo If you want to delete the key-pair, please do it manually. >> $name-remove.sh 123 | 124 | chmod +x $name-remove.sh 125 | 126 | echo All done. Find all you need to connect in the $name-commands.txt file and to remove the stack call $name-remove.sh 127 | echo Connect to your instance: ssh -i ~/.ssh/aws-key-$name.pem ubuntu@$instanceUrl 128 | -------------------------------------------------------------------------------- /setup/setup_p2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Configure a p2.xlarge instance 4 | 5 | # get the correct ami 6 | export region=$(aws configure get region) 7 | if [ $region = "us-west-2" ]; then 8 | export ami="ami-bc508adc" # Oregon 9 | elif [ $region = "eu-west-1" ]; then 10 | export ami="ami-b43d1ec7" # Ireland 11 | elif [ $region = "us-east-1" ]; then 12 | export ami="ami-31ecfb26" # Virginia 13 | else 14 | echo "Only us-west-2 (Oregon), eu-west-1 (Ireland), and us-east-1 (Virginia) are currently supported" 15 | exit 1 16 | fi 17 | 18 | export instanceType="p2.xlarge" 19 | 20 | . $(dirname "$0")/setup_instance.sh 21 | -------------------------------------------------------------------------------- /setup/setup_t2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Configure a t2.xlarge instance 4 | 5 | # get the correct ami 6 | export region=$(aws configure get region) 7 | if [ $region = "us-west-2" ]; then 8 | export ami="ami-f8fd5998" # Oregon 9 | elif [ $region = "eu-west-1" ]; then 10 | export ami="ami-9e1a35ed" # Ireland 11 | elif [ $region = "us-east-1" ]; then 12 | export ami="ami-9c5b438b" # Virginia 13 | else 14 | echo "Only us-west-2 (Oregon), eu-west-1 (Ireland), and us-east-1 (Virginia) are currently supported" 15 | exit 1 16 | fi 17 | 18 | export instanceType="t2.xlarge" 19 | 20 | . $(dirname "$0")/setup_instance.sh 21 | --------------------------------------------------------------------------------