#!/usr/bin/env python # coding: utf-8 # # ## This notebook is an adaptation of the Breath Cancer Example with Functional Model # ## Overview # # There are four steps to setting up an experiment with Talos: # # 1) Imports and data # # 2) Creating the Keras model # # 3) Defining the Parameter Space Boundaries # # 4) Running the Experiment # ## 1. The Required Inputs and Data # In[ ]: import talos from keras.models import Sequential, Model from keras.layers import Dropout, Dense, Input from keras.losses import binary_crossentropy # In[ ]: # then we load the dataset x, y = talos.templates.datasets.breast_cancer() # and normalize every feature to mean 0, std 1 x = talos.utils.rescale_meanzero(x) # ## 2. Creating the Keras Model # In[ ]: # first we have to make sure to input data and params into the function def breast_cancer_model(x_train, y_train, x_val, y_val, params): inputs = Input(shape=(x_train.shape[1],)) layer = Dense(params['first_neuron'], activation=params['activation'], kernel_initializer=params['kernel_initializer'])(inputs) layer = Dropout(params['dropout'])(layer) outputs = Dense(1, activation=params['last_activation'], kernel_initializer=params['kernel_initializer'])(layer) model = Model(inputs, outputs) model.compile(loss=params['losses'], optimizer=params['optimizer'], metrics=['acc']) history = model.fit(x_train, y_train, validation_data=[x_val, y_val], batch_size=params['batch_size'], epochs=params['epochs'], verbose=0) return history, model # ## 3. Defining the Parameter Space Boundary # In[ ]: # then we can go ahead and set the parameter space p = {'first_neuron':[9, 10, 11], 'batch_size': [30], 'epochs': [100], 'dropout': [0], 'kernel_initializer': ['uniform','normal'], 'optimizer': ['Nadam', 'Adam'], 'losses': ['binary_crossentropy'], 'activation':['relu', 'elu'], 'last_activation': ['sigmoid']} # ## 4. Starting the Experiment # In[ ]: # and run the experiment t = talos.Scan(x=x, y=y, model=breast_cancer_model, params=p, experiment_name='breast_cancer', fraction_limit=0.5)