data-science-ipython-notebooks/deep-learning/keras-tutorial/2.2.2 Supervised Learning - ConvNet HandsOn Part II.ipynb
2017-08-26 08:47:27 -04:00

1006 lines
37 KiB
Python

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Credits: Forked from [deep-learning-keras-tensorflow](https://github.com/leriomaggio/deep-learning-keras-tensorflow) by Valerio Maggio"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Convolution Nets for MNIST"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Deep Learning models can take quite a bit of time to run, particularly if GPU isn't used. \n",
"\n",
"In the interest of time, you could sample a subset of observations (e.g. $1000$) that are a particular number of your choice (e.g. $6$) and $1000$ observations that aren't that particular number (i.e. $\\neq 6$). \n",
"\n",
"We will build a model using that and see how it performs on the test dataset"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using Theano backend.\n",
"Using gpu device 0: GeForce GTX 760 (CNMeM is enabled with initial size: 90.0% of memory, cuDNN 4007)\n"
]
}
],
"source": [
"#Import the required libraries\n",
"import numpy as np\n",
"np.random.seed(1338)\n",
"\n",
"from keras.datasets import mnist"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"from keras.models import Sequential"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"from keras.layers.core import Dense, Dropout, Activation, Flatten"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"from keras.layers.convolutional import Convolution2D\n",
"from keras.layers.pooling import MaxPooling2D"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"from keras.utils import np_utils\n",
"from keras.optimizers import SGD"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## Loading Data"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"path_to_dataset = \"euroscipy_2016_dl-keras/data/mnist.pkl.gz\"\n",
"\n",
"#Load the training and testing data\n",
"(X_train, y_train), (X_test, y_test) = mnist.load_data(path_to_dataset)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"X_test_orig = X_test"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## Data Preparation"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"img_rows, img_cols = 28, 28\n",
"\n",
"X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)\n",
"X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)\n",
"\n",
"X_train = X_train.astype('float32')\n",
"X_test = X_test.astype('float32')\n",
"\n",
"X_train /= 255\n",
"X_test /= 255"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# Seed for reproducibilty\n",
"np.random.seed(1338)\n",
"\n",
"# Test data\n",
"X_test = X_test.copy()\n",
"Y = y_test.copy()\n",
"\n",
"# Converting the output to binary classification(Six=1,Not Six=0)\n",
"Y_test = Y == 6\n",
"Y_test = Y_test.astype(int)\n",
"\n",
"# Selecting the 5918 examples where the output is 6\n",
"X_six = X_train[y_train == 6].copy()\n",
"Y_six = y_train[y_train == 6].copy()\n",
"\n",
"# Selecting the examples where the output is not 6\n",
"X_not_six = X_train[y_train != 6].copy()\n",
"Y_not_six = y_train[y_train != 6].copy()\n",
"\n",
"# Selecting 6000 random examples from the data that \n",
"# only contains the data where the output is not 6\n",
"random_rows = np.random.randint(0,X_six.shape[0],6000)\n",
"X_not_six = X_not_six[random_rows]\n",
"Y_not_six = Y_not_six[random_rows]"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# Appending the data with output as 6 and data with output as <> 6\n",
"X_train = np.append(X_six,X_not_six)\n",
"\n",
"# Reshaping the appended data to appropraite form\n",
"X_train = X_train.reshape(X_six.shape[0] + X_not_six.shape[0], \n",
" 1, img_rows, img_cols)\n",
"\n",
"# Appending the labels and converting the labels to \n",
"# binary classification(Six=1,Not Six=0)\n",
"Y_labels = np.append(Y_six,Y_not_six)\n",
"Y_train = Y_labels == 6 \n",
"Y_train = Y_train.astype(int)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(11918, 1, 28, 28) (11918,) (10000, 1, 28, 28) (10000, 2)\n"
]
}
],
"source": [
"print(X_train.shape, Y_labels.shape, X_test.shape, Y_test.shape)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"# Converting the classes to its binary categorical form\n",
"nb_classes = 2\n",
"Y_train = np_utils.to_categorical(Y_train, nb_classes)\n",
"Y_test = np_utils.to_categorical(Y_test, nb_classes)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# A simple CNN"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"#Initializing the values for the convolution neural network\n",
"nb_epoch = 2\n",
"batch_size = 128\n",
"# number of convolutional filters to use\n",
"nb_filters = 32\n",
"# size of pooling area for max pooling\n",
"nb_pool = 2\n",
"# convolution kernel size\n",
"nb_conv = 3\n",
"\n",
"sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Step 1: Model Definition"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"model = Sequential()\n",
"\n",
"model.add(Convolution2D(nb_filters, nb_conv, nb_conv,\n",
" border_mode='valid',\n",
" input_shape=(1, img_rows, img_cols)))\n",
"model.add(Activation('relu'))\n",
"\n",
"model.add(Flatten())\n",
"model.add(Dense(nb_classes))\n",
"model.add(Activation('softmax'))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Step 2: Compile"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model.compile(loss='categorical_crossentropy',\n",
" optimizer='sgd',\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Step 3: Fit"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 11918 samples, validate on 10000 samples\n",
"Epoch 1/2\n",
"11918/11918 [==============================] - 0s - loss: 0.2890 - acc: 0.9326 - val_loss: 0.1251 - val_acc: 0.9722\n",
"Epoch 2/2\n",
"11918/11918 [==============================] - 0s - loss: 0.1341 - acc: 0.9612 - val_loss: 0.1298 - val_acc: 0.9599\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7f6ccb68f630>"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.fit(X_train, Y_train, batch_size=batch_size, \n",
" nb_epoch=nb_epoch,verbose=1,\n",
" validation_data=(X_test, Y_test))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Step 4: Evaluate"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test score: 0.129807630396\n",
"Test accuracy: 0.9599\n"
]
}
],
"source": [
"# Evaluating the model on the test data \n",
"score, accuracy = model.evaluate(X_test, Y_test, verbose=0)\n",
"print('Test score:', score)\n",
"print('Test accuracy:', accuracy)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Let's plot our model Predictions!"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA50AAABVCAYAAADKf4AOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJztnXm4VmW5/z+PgMIJA4UABYckPZoYaJSWeEmWph3MMicQ\nTpAoTimCx711b3NhbGWTECAKKhYdUBQnNAtSK0xIJcejRqeccjghDj9NTAXx+f1x32tP7I17eOf3\n+7mufT3rfdaz1nvfe71reu4pxBgRQgghhBBCCCGywTb5FkAIIYQQQgghROmil04hhBBCCCGEEFkj\nKy+d3bp1WxdCiG3969at27psyJNtpK/0lc7St5iQvtJXOkvfYkL6St9S0rnc9E0J2YjpDCHE9uw3\nhECMMWRcoCwjfVu9X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TIGnrpFEIIIYQQQgiRNfTSKYQQQgghhBAia+ilUwghhBBC\nCCFE1tBLpxBCCCGEEEKIrKGXTiGEEEIIIYQQWUMvnUIIIYQQQgghsoZeOoUQQgghhBBCZI3/D4vq\nWbumY8f6AAAAAElFTkSuQmCC\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x7f6cab246898>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"slice = 15\n",
"predicted = model.predict(X_test[:slice]).argmax(-1)\n",
"\n",
"plt.figure(figsize=(16,8))\n",
"for i in range(slice):\n",
" plt.subplot(1, slice, i+1)\n",
" plt.imshow(X_test_orig[i], interpolation='nearest')\n",
" plt.text(0, 0, predicted[i], color='black', \n",
" bbox=dict(facecolor='white', alpha=1))\n",
" plt.axis('off')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Adding more Dense Layers"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"model = Sequential()\n",
"model.add(Convolution2D(nb_filters, nb_conv, nb_conv,\n",
" border_mode='valid',\n",
" input_shape=(1, img_rows, img_cols)))\n",
"model.add(Activation('relu'))\n",
"\n",
"model.add(Flatten())\n",
"model.add(Dense(128))\n",
"model.add(Activation('relu'))\n",
"\n",
"model.add(Dense(nb_classes))\n",
"model.add(Activation('softmax'))"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 11918 samples, validate on 10000 samples\n",
"Epoch 1/2\n",
"11918/11918 [==============================] - 0s - loss: 0.3044 - acc: 0.9379 - val_loss: 0.1469 - val_acc: 0.9625\n",
"Epoch 2/2\n",
"11918/11918 [==============================] - 0s - loss: 0.1189 - acc: 0.9640 - val_loss: 0.1058 - val_acc: 0.9655\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7f6cf59f7358>"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.compile(loss='categorical_crossentropy',\n",
" optimizer='sgd',\n",
" metrics=['accuracy'])\n",
"\n",
"model.fit(X_train, Y_train, batch_size=batch_size, \n",
" nb_epoch=nb_epoch,verbose=1,\n",
" validation_data=(X_test, Y_test))"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test score: 0.105762729073\n",
"Test accuracy: 0.9655\n"
]
}
],
"source": [
"#Evaluating the model on the test data \n",
"score, accuracy = model.evaluate(X_test, Y_test, verbose=0)\n",
"print('Test score:', score)\n",
"print('Test accuracy:', accuracy)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Adding Dropout"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"model = Sequential()\n",
"\n",
"model.add(Convolution2D(nb_filters, nb_conv, nb_conv,\n",
" border_mode='valid',\n",
" input_shape=(1, img_rows, img_cols)))\n",
"model.add(Activation('relu'))\n",
"\n",
"model.add(Flatten())\n",
"model.add(Dense(128))\n",
"model.add(Activation('relu'))\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(nb_classes))\n",
"model.add(Activation('softmax'))"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 11918 samples, validate on 10000 samples\n",
"Epoch 1/2\n",
"11918/11918 [==============================] - 0s - loss: 0.3128 - acc: 0.9097 - val_loss: 0.1438 - val_acc: 0.9624\n",
"Epoch 2/2\n",
"11918/11918 [==============================] - 0s - loss: 0.1362 - acc: 0.9580 - val_loss: 0.1145 - val_acc: 0.9628\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7f6ccb180208>"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.compile(loss='categorical_crossentropy',\n",
" optimizer='sgd',\n",
" metrics=['accuracy'])\n",
"\n",
"model.fit(X_train, Y_train, batch_size=batch_size, \n",
" nb_epoch=nb_epoch,verbose=1,\n",
" validation_data=(X_test, Y_test))"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test score: 0.11448907243\n",
"Test accuracy: 0.9628\n"
]
}
],
"source": [
"#Evaluating the model on the test data \n",
"score, accuracy = model.evaluate(X_test, Y_test, verbose=0)\n",
"print('Test score:', score)\n",
"print('Test accuracy:', accuracy)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Adding more Convolution Layers"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"model = Sequential()\n",
"model.add(Convolution2D(nb_filters, nb_conv, nb_conv,\n",
" border_mode='valid',\n",
" input_shape=(1, img_rows, img_cols)))\n",
"model.add(Activation('relu'))\n",
"model.add(Convolution2D(nb_filters, nb_conv, nb_conv))\n",
"model.add(Activation('relu'))\n",
"model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))\n",
"model.add(Dropout(0.25))\n",
" \n",
"model.add(Flatten())\n",
"model.add(Dense(128))\n",
"model.add(Activation('relu'))\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(nb_classes))\n",
"model.add(Activation('softmax'))"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 11918 samples, validate on 10000 samples\n",
"Epoch 1/2\n",
"11918/11918 [==============================] - 1s - loss: 0.4707 - acc: 0.8288 - val_loss: 0.2307 - val_acc: 0.9399\n",
"Epoch 2/2\n",
"11918/11918 [==============================] - 1s - loss: 0.1882 - acc: 0.9383 - val_loss: 0.1195 - val_acc: 0.9621\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7f6cc97b8748>"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.compile(loss='categorical_crossentropy',\n",
" optimizer='sgd',\n",
" metrics=['accuracy'])\n",
"\n",
"model.fit(X_train, Y_train, batch_size=batch_size, \n",
" nb_epoch=nb_epoch,verbose=1,\n",
" validation_data=(X_test, Y_test))"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test score: 0.11954063682\n",
"Test accuracy: 0.9621\n"
]
}
],
"source": [
"#Evaluating the model on the test data \n",
"score, accuracy = model.evaluate(X_test, Y_test, verbose=0)\n",
"print('Test score:', score)\n",
"print('Test accuracy:', accuracy)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise\n",
"\n",
"The above code has been written as a function. \n",
"\n",
"Change some of the **hyperparameters** and see what happens. "
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"# Function for constructing the convolution neural network\n",
"# Feel free to add parameters, if you want\n",
"\n",
"def build_model():\n",
" \"\"\"\"\"\"\n",
" model = Sequential()\n",
" model.add(Convolution2D(nb_filters, nb_conv, nb_conv,\n",
" border_mode='valid',\n",
" input_shape=(1, img_rows, img_cols)))\n",
" model.add(Activation('relu'))\n",
" model.add(Convolution2D(nb_filters, nb_conv, nb_conv))\n",
" model.add(Activation('relu'))\n",
" model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))\n",
" model.add(Dropout(0.25))\n",
" \n",
" model.add(Flatten())\n",
" model.add(Dense(128))\n",
" model.add(Activation('relu'))\n",
" model.add(Dropout(0.5))\n",
" model.add(Dense(nb_classes))\n",
" model.add(Activation('softmax'))\n",
" \n",
" model.compile(loss='categorical_crossentropy',\n",
" optimizer='sgd',\n",
" metrics=['accuracy'])\n",
"\n",
" model.fit(X_train, Y_train, batch_size=batch_size, \n",
" nb_epoch=nb_epoch,verbose=1,\n",
" validation_data=(X_test, Y_test))\n",
" \n",
"\n",
" #Evaluating the model on the test data \n",
" score, accuracy = model.evaluate(X_test, Y_test, verbose=0)\n",
" print('Test score:', score)\n",
" print('Test accuracy:', accuracy)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 11918 samples, validate on 10000 samples\n",
"Epoch 1/2\n",
"11918/11918 [==============================] - 1s - loss: 0.5634 - acc: 0.7860 - val_loss: 0.3574 - val_acc: 0.9363\n",
"Epoch 2/2\n",
"11918/11918 [==============================] - 1s - loss: 0.2372 - acc: 0.9292 - val_loss: 0.2253 - val_acc: 0.9190\n",
"Test score: 0.225333989978\n",
"Test accuracy: 0.919\n",
"1 loop, best of 1: 5.45 s per loop\n"
]
}
],
"source": [
"#Timing how long it takes to build the model and test it.\n",
"%timeit -n1 -r1 build_model()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Batch Normalisation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Normalize the activations of the previous layer at each batch, i.e. applies a transformation that maintains the mean activation close to 0 and the activation standard deviation close to 1."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## How to BatchNorm in Keras\n",
"\n",
"```python\n",
"from keras.layers.normalization import BatchNormalization\n",
"\n",
"BatchNormalization(epsilon=1e-06, mode=0, \n",
" axis=-1, momentum=0.99, \n",
" weights=None, beta_init='zero', \n",
" gamma_init='one')\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# Try to add a new BatchNormalization layer to the Model \n",
"# (after the Dropout layer)"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}