Neural Nets

PHOTON supports neural nets from any library. We have integrated a basic DNN Model using keras. However, you can integrate any custom neural net using any kind of library by creating a custom class adhering to the estimator interface. In this example, we let PHOTON decide which neural net implementation to use.

           
from photonai.base.PhotonBase import Hyperpipe, PipelineElement, PipelineSwitch
from photonai.optimization.Hyperparameters import Categorical
from photonai.configuration.Register import PhotonRegister
from sklearn.model_selection import KFold
from sklearn.datasets import load_breast_cancer

X, y = load_breast_cancer(True)


my_pipe = Hyperpipe('neual_net_pipe',
                    optimizer='grid_search',
                    metrics=['accuracy', 'precision', 'recall'],
                    best_config_metric='f1_score',
                    outer_cv=KFold(n_splits=3),
                    inner_cv=KFold(n_splits=2),
                    verbosity=1)

# NOW FIND OUT MORE ABOUT THE MLPClassifier ELEMENT
PhotonRegister.info('MLPClassifier')

mlp = PipelineElement('MLPClassifier')

keras_dnn = PipelineElement('KerasDNNClassifier',
                            hyperparameters={'hidden_layer_sizes':Categorical([[100, 50],[50,25]]),
                                             'dropout_rate':Categorical([0.2,0.5])},
                            target_dimension=2,
                            act_func='relu',
                            batch_size=32)


# switch of two different estimators: keras_DNN and skLearn_MLP
switch = PipelineSwitch('estimator_switch')
switch += mlp
switch += keras_dnn

# add StandardScaler in front of the estimator_switch
my_pipe += PipelineElement('StandardScaler')
my_pipe += switch

my_pipe.fit(X, y)