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 as ta
import wrangle as wr
import pandas as pd

from keras.models import Sequential, Model
from keras.layers import Dropout, Dense, Input
from keras.optimizers import Adam, Nadam
from keras.activations import relu, elu, sigmoid
from keras.losses import binary_crossentropy
In [ ]:
# then we load the dataset
x, y = ta.datasets.breast_cancer()

# and normalize every feature to mean 0, std 1
x = wr.mean_zero(pd.DataFrame(x)).values

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 = ta.Scan(x=x,
            y=y,
            model=breast_cancer_model,
            grid_downsample=1, 
            params=p,
            dataset_name='breast_cancer',
            experiment_no='1',
            functional_model=True)
In [ ]:
r = ta.Reporting("breast_cancer_1.csv")