{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## This notebook is an adaptation of the Breath Cancer Example with Functional Model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview\n", "\n", "There are four steps to setting up an experiment with Talos:\n", "\n", "1) Imports and data\n", "\n", "2) Creating the Keras model\n", "\n", "3) Defining the Parameter Space Boundaries \n", "\n", "4) Running the Experiment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. The Required Inputs and Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import talos as ta\n", "import wrangle as wr\n", "import pandas as pd\n", "\n", "from keras.models import Sequential, Model\n", "from keras.layers import Dropout, Dense, Input\n", "from keras.optimizers import Adam, Nadam\n", "from keras.activations import relu, elu, sigmoid\n", "from keras.losses import binary_crossentropy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# then we load the dataset\n", "x, y = ta.datasets.breast_cancer()\n", "\n", "# and normalize every feature to mean 0, std 1\n", "x = wr.mean_zero(pd.DataFrame(x)).values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Creating the Keras Model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# first we have to make sure to input data and params into the function\n", "def breast_cancer_model(x_train, y_train, x_val, y_val, params):\n", " \n", " inputs = Input(shape=(x_train.shape[1],))\n", " \n", " layer = Dense(params['first_neuron'], activation=params['activation'], \n", " kernel_initializer=params['kernel_initializer'])(inputs)\n", " \n", " layer = Dropout(params['dropout'])(layer)\n", " \n", " outputs = Dense(1, activation=params['last_activation'],\n", " kernel_initializer=params['kernel_initializer'])(layer)\n", " \n", " model = Model(inputs, outputs)\n", " \n", " \n", " model.compile(loss=params['losses'],\n", " optimizer=params['optimizer'](),\n", " metrics=['acc'])\n", " \n", " history = model.fit(x_train, y_train, \n", " validation_data=[x_val, y_val],\n", " batch_size=params['batch_size'],\n", " epochs=params['epochs'],\n", " verbose=0)\n", "\n", " return history, model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Defining the Parameter Space Boundary" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# then we can go ahead and set the parameter space\n", "p = {'first_neuron':[9, 10, 11],\n", " 'batch_size': [30],\n", " 'epochs': [100],\n", " 'dropout': [0],\n", " 'kernel_initializer': ['uniform','normal'],\n", " 'optimizer': [Nadam, Adam],\n", " 'losses': [binary_crossentropy],\n", " 'activation':[relu, elu],\n", " 'last_activation': [sigmoid]}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Starting the Experiment" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# and run the experiment\n", "t = ta.Scan(x=x,\n", " y=y,\n", " model=breast_cancer_model,\n", " grid_downsample=1, \n", " params=p,\n", " dataset_name='breast_cancer',\n", " experiment_no='1',\n", " functional_model=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r = ta.Reporting(\"breast_cancer_1.csv\")" ] } ], "metadata": { "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }