Advertisement
Guest User

Untitled

a guest
Jul 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. import talos as ta
  2. from talos.metrics.keras_metrics import fmeasure
  3.  
  4. import pandas as pd
  5.  
  6. from keras.models import Sequential
  7. from keras.layers import Dropout, Dense
  8.  
  9. # Keras items
  10. from keras.optimizers import Adam, Nadam
  11. from keras.activations import relu, elu
  12. from keras.losses import binary_crossentropy
  13. from keras.activations import relu, elu, sigmoid
  14. from keras.losses import binary_crossentropy, mse
  15. %matplotlib inline
  16.  
  17. def breast_cancer_model(x_train, y_train, x_val, y_val, params):
  18.  
  19.     model = Sequential()
  20.     model.add(Dense(params['first_neuron'], input_dim=x_train.shape[1],
  21.                     activation=params['activation'],
  22.                     kernel_initializer=params['kernel_initializer']))
  23.    
  24.     model.add(Dropout(params['dropout']))
  25.  
  26.     model.add(Dense(1, activation=params['last_activation'],
  27.                     kernel_initializer=params['kernel_initializer']))
  28.    
  29.     model.compile(loss=params['losses'],
  30.                   optimizer=params['optimizer'](),
  31.                   metrics=['acc', fmeasure])
  32.    
  33.     history = model.fit(x_train, y_train,
  34.                         validation_data=[x_val, y_val],
  35.                         batch_size=params['batch_size'],
  36.                         epochs=params['epochs'],
  37.                         verbose=1)
  38.  
  39.     return history, model
  40.  
  41. # then we load the dataset
  42. x, y = ta.datasets.breast_cancer()
  43.  
  44. # then we can go ahead and set the parameter space
  45. p = {'first_neuron':[9,10],
  46.      'hidden_layers':[0],
  47.      'batch_size': [30],
  48.      'epochs': [100],
  49.      'dropout': [0],
  50.      'kernel_initializer': ['uniform'],
  51.      'optimizer': [Nadam],
  52.      'losses': [mse],
  53.      'activation':[relu],
  54.      'last_activation': [sigmoid]}
  55.  
  56.  
  57. # and run the experiment
  58. t = ta.Scan(x=x,
  59.             y=y,
  60.             model=breast_cancer_model,
  61.             grid_downsample=1,
  62.             search_method='linear',
  63.             params=p,
  64.             dataset_name='breast_cancer',
  65.             experiment_no='k')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement