{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## mnist-hpo\n", "This notebook uses Keras to build a model for the mnist dataset with the network's learning rate configurable as a hyperparameter in order to publish in Watson Studios' Deep Learning as a Service\n", "\n", "First, libraries, including Keras, are imported" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import json\n", "import keras\n", "import os.path\n", "from os import environ\n", "from keras.datasets import mnist\n", "from keras.models import Sequential\n", "from keras.optimizers import RMSprop\n", "from keras.layers import Dense, Dropout" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Watson Studio will send a json for the Keras model with information on how to vary the models' learning rate. More information on how this is performed is in the Guide of the DLaaSWorkshop repo" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "###############################################################################\n", "# Set up HPO.\n", "###############################################################################\n", "\n", "config_file = \"config.json\"\n", "\n", "if os.path.exists(config_file):\n", " with open(config_file, 'r') as f:\n", " json_obj = json.load(f)\n", " learning_rate = json_obj[\"learning_rate\"]\n", "else:\n", " learning_rate = 0.001\n", "\n", "\n", "###############################################################################" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#loading the dataset, setting parameters\n", "batch_size = 128\n", "num_classes = 10\n", "epochs = 1\n", "\n", "# the data, split between train and test sets\n", "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", "\n", "x_train = x_train.reshape(60000, 784)\n", "x_test = x_test.reshape(10000, 784)\n", "x_train = x_train.astype('float32')\n", "x_test = x_test.astype('float32')\n", "x_train /= 255\n", "x_test /= 255\n", "print(x_train.shape[0], 'train samples')\n", "print(x_test.shape[0], 'test samples')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# convert class vectors to binary class matrices, \n", "# each class has a node in the last layer of the network, which calculates the strength of a prediction for each class\n", "print(y_train[0])\n", "y_train = keras.utils.to_categorical(y_train, num_classes)\n", "y_test = keras.utils.to_categorical(y_test, num_classes)\n", "print(y_train[0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# model building with Keras\n", "\n", "model = Sequential()\n", "model.add(Dense(512, activation='relu', input_shape=(784,)))\n", "model.add(Dropout(0.2))\n", "model.add(Dense(512, activation='relu'))\n", "model.add(Dropout(0.2))\n", "model.add(Dense(num_classes, activation='softmax'))\n", "\n", "model.summary()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Code to execute Keras training, observe model statistics\n", "model.compile(loss='categorical_crossentropy',\n", " optimizer=RMSprop(lr=learning_rate),\n", " metrics=['accuracy'])\n", "\n", "\n", "history = model.fit(x_train, y_train,\n", " batch_size=batch_size,\n", " epochs=epochs,\n", " validation_data=(x_test, y_test))\n", "\n", "\n", "\n", "print(\"Training history:\" + str(history.history))\n", "\n", "score = model.evaluate(x_test, y_test, verbose=0)\n", "print('Test loss:', score[0])\n", "print('Test accuracy:', score[1])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }