From 475f9bb57431d97a8e86fb05b855a0f6801d350f Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Fri, 28 Feb 2020 14:50:20 +0100 Subject: [PATCH 01/24] first approximation working --- flame/children/parameters.yaml | 66 ++++++++ flame/config.yaml | 4 +- flame/learn.py | 2 + flame/stats/Keras.py | 271 +++++++++++++++++++++++++++++++++ 4 files changed, 341 insertions(+), 2 deletions(-) create mode 100644 flame/stats/Keras.py diff --git a/flame/children/parameters.yaml b/flame/children/parameters.yaml index 5a6b9406..c20fc40c 100644 --- a/flame/children/parameters.yaml +++ b/flame/children/parameters.yaml @@ -1007,6 +1007,72 @@ XGBOOST_optimize: comments: group: modeling +Keras_parameters: + advanced: advanced + object_type: dictionary + writable: false + options: null + value: + epochs: + object_type: list + writable: false + value: 150 + options: + - 100 + - 200 + - 250 + description: Maximum tree depth for base learners. + batch_size: + object_type: float + writable: true + value: 1 + options: + - 1 + - 10 + - 30 + description: Boosting learning rate (xgb's "eta") + description: + dependencies: + model: SKLEARN + comments: + group: modeling + +Keras_optimize: + advanced: advanced + object_type: dictionary + writable: false + options: null + value: + epoch: + object_type: list + writable: false + value: + - 50 + - 100 + - 150 + options: + - 1 + - 3 + - 6 + description: + learning_rate: + object_type: float + writable: true + value: + - 1 + - 10 + - 30 + options: + - 1 + - 10 + - 30 + description: + + description: Keras optimize parameters + dependencies: + model: Keras + comments: + group: modeling output_format: advanced: regular diff --git a/flame/config.yaml b/flame/config.yaml index 64049601..b9bd75e2 100644 --- a/flame/config.yaml +++ b/flame/config.yaml @@ -2,7 +2,7 @@ admin_email: manuel.pastor@upf.edu admin_name: Manuel Pastor config_status: true homepage: http://phi.upf.edu -model_repository_path: C:\Users\mpastor\Documents\soft\flame\flame\models +model_repository_path: /home/jcgomez/projects/internals/flame/eye_irritation space_repository_path: C:\Users\mpastor\Documents\soft\flame\flame\spaces -predictions_repository_path: C:\Users\mpastor\Documents\soft\flame\flame\predictions +predictions_repository_path: /home/jcgomez/projects/internals/flame/eye_irritation provider: UPF (Pompeu Fabra University) diff --git a/flame/learn.py b/flame/learn.py index d906c927..b73b0058 100644 --- a/flame/learn.py +++ b/flame/learn.py @@ -31,6 +31,7 @@ from flame.stats.PLSR import PLSR from flame.stats.PLSDA import PLSDA from flame.stats.XGboost import XGBOOST +from flame.stats.Keras import Keras_nn from flame.stats.combo import median, mean, majority, matrix from sklearn.preprocessing import MinMaxScaler from sklearn.preprocessing import StandardScaler @@ -67,6 +68,7 @@ def __init__(self, parameters, conveyor): ('PLSDA', PLSDA), ('median', median), ('mean', mean), + ('Keras', Keras_nn), ('majority', majority), ('matrix', matrix)] diff --git a/flame/stats/Keras.py b/flame/stats/Keras.py new file mode 100644 index 00000000..43ca48d5 --- /dev/null +++ b/flame/stats/Keras.py @@ -0,0 +1,271 @@ +#! -*- coding: utf-8 -*- + +# Description Flame Parent Model Class +## +# Authors: Jose Carlos Gómez (josecarlos.gomez@upf.edu) +## +# Copyright 2018 Manuel Pastor +## +# This file is part of Flame +## +# Flame is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation version 3. +## +# Flame is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +## +# You should have received a copy of the GNU General Public License +# along with Flame. If not, see . + +from copy import copy + +from nonconformist.base import ClassifierAdapter, RegressorAdapter +from nonconformist.acp import AggregatedCp +from nonconformist.acp import BootstrapSampler +from nonconformist.icp import IcpClassifier, IcpRegressor +from nonconformist.nc import ClassifierNc, MarginErrFunc, RegressorNc +from nonconformist.nc import AbsErrorErrFunc, RegressorNormalizer +import os +os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"; +# The GPU id to use, usually either "0" or "1"; +os.environ["CUDA_VISIBLE_DEVICES"]="0" +from keras.models import Sequential +from keras.layers import Dense + +import numpy as np + +from flame.stats.base_model import BaseEstimator +from flame.util import get_logger +LOG = get_logger(__name__) + + +class Keras_nn(BaseEstimator): + """ + This class inherits from BaseEstimator and wraps SKLEARN + RandomForestClassifier or RandomForestRegressor estimator + + ... + + Attributes + ---------- + + estimator_parameters : dict + parameter values + name : string + name of the estimator + tune_parameters: dict + Hyperparameter optimization settings + + Methods + ------- + + build(X) + Instance the estimator optimizing it + if tune=true. + + """ + def __init__(self, X, Y, parameters, conveyor): + # Initialize parent class + try: + BaseEstimator.__init__(self, X, Y, parameters, conveyor) + LOG.debug('Initialize BaseEstimator parent class') + except Exception as e: + self.conveyor.setError(f'Error initializing BaseEstimator parent class with exception: {e}') + LOG.error(f'Error initializing BaseEstimator parent class with exception: {e}') + return + + # Load estimator parameters + self.estimator_parameters = self.param.getDict('Keras_parameters') + + # Load tune parameters + self.tune_parameters = self.param.getDict('Keras_optimize') + + if self.param.getVal('quantitative'): + self.name = "Keras-Regressor" + else: + self.name = "Keras-Classifier" + + def build(self): + '''Build a new DL model with the X and Y numpy matrices ''' + + + try: + from keras.wrappers.scikit_learn import KerasClassifier + from keras.wrappers.scikit_learn import KerasRegressor + except Exception as e: + return False, 'Keras not found, please revise your environment' + + # Make a copy of data matrices + X = self.X.copy() + Y = self.Y.copy() + + results = [] + results.append(('nobj', 'number of objects', self.nobj)) + results.append(('nvarx', 'number of predictor variables', self.nvarx)) + + # If tune then call gridsearch to optimize the estimator + if self.param.getVal('tune'): + + LOG.info("Optimizing Keras estimator") + + try: + # Check type of model + if self.param.getVal('quantitative'): + self.estimator = KerasRegressor( + **self.estimator_parameters) + self.optimize(X, Y, self.estimator, self.tune_parameters) + results.append(('model','model type','KERAS quantitative (optimized)')) + else: + self.estimator = KerasClassifier( + **self.estimator_parameters) + params = self.estimator.get_params() + params['num_class'] = 2 + self.optimize(X, Y, self.estimator, + self.tune_parameters) + results.append(('model','model type','KERAS qualitative (optimized)')) + + except Exception as e: + return False, f'Exception optimizing KERAS estimator with exception {e}' + + else: + try: + if self.param.getVal('quantitative'): + + LOG.info("Building Quantitative KERAS model") + params = { + 'objective': 'reg:squarederror', + # 'max_depth': 20, + # 'learning_rate': 1.0, + # 'silent': 1, + # 'n_estimators': 25 + } + self.estimator = KerasRegressor(**params) + results.append(('model', 'model type', 'Keras quantitative')) + else: + + LOG.info("Building Qualitative Keras model") + params = { + 'objective': 'binary:logistic', + 'max_depth': 3, + #'learning_rate': 0.7, + #'silent': 1, + 'n_estimators': 100 + } + self.estimator = KerasClassifier(build_fn=self.create_model, **self.estimator_parameters) + results.append(('model', 'model type', 'Keras qualitative')) + + self.estimator.fit(X, Y) + print(self.estimator) + + except Exception as e: + raise e + return False, f'Exception building Keras estimator with exception {e}' + + self.estimator_temp = copy(self.estimator) + + if not self.param.getVal('conformal'): + return True, results + # Create the conformal estimator + try: + # Conformal regressor + if self.param.getVal('quantitative'): + + LOG.info("Building conformal Quantitative Keras model") + + underlying_model = RegressorAdapter(self.estimator_temp) + #normalizing_model = RegressorAdapter( + #KNeighborsRegressor(n_neighbors=5)) + normalizing_model = RegressorAdapter(self.estimator_temp) + normalizer = RegressorNormalizer( + underlying_model, + normalizing_model, + AbsErrorErrFunc()) + nc = RegressorNc(underlying_model, + AbsErrorErrFunc(), + normalizer) + + # self.conformal_pred = AggregatedCp(IcpRegressor + # (RegressorNc(RegressorAdapter(self.estimator))), + # BootstrapSampler()) + + self.estimator = AggregatedCp(IcpRegressor(nc), + BootstrapSampler()) + + self.estimator.fit(X, Y) + results.append(('model', 'model type', 'conformal Keras quantitative')) + + # Conformal classifier + else: + + LOG.info("Building conformal Qualitative Keras model") + + self.estimator = AggregatedCp( + IcpClassifier( + ClassifierNc( + ClassifierAdapter(self.estimator_temp), + MarginErrFunc() + ) + ), + BootstrapSampler()) + + # Fit estimator to the data + self.estimator.fit(X, Y) + results.append(('model', 'model type', 'conformal Keras qualitative')) + + except Exception as e: + raise e + return False, f'Exception building conformal Keras estimator with exception {e}' + + return True, results + +# Function to create model, required for KerasClassifier + def create_model(self): + # create model + model = Sequential() + model.add(Dense(5, input_dim=25, activation='relu')) + model.add(Dense(5, activation='relu')) + model.add(Dense(1, activation='sigmoid')) + # Compile model + model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) + return model + + +## Overriding of parent methods + + # def CF_quantitative_validation(self): + # ''' performs validation for conformal quantitative models ''' + + + + # def CF_qualitative_validation(self): + # ''' performs validation for conformal qualitative models ''' + + + # def quantitativeValidation(self): + # ''' performs validation for quantitative models ''' + + # def qualitativeValidation(self): + # ''' performs validation for qualitative models ''' + + + # def validate(self): + # ''' Validates the model and computes suitable model quality scoring values''' + + + # def optimize(self, X, Y, estimator, tune_parameters): + # ''' optimizes a model using a grid search over a range of values for diverse parameters''' + + + # def regularProject(self, Xb, results): + # ''' projects a collection of query objects in a regular model, for obtaining predictions ''' + + + # def conformalProject(self, Xb, results): + # ''' projects a collection of query objects in a conformal model, for obtaining predictions ''' + + + # def project(self, Xb, results): + # ''' Uses the X matrix provided as argument to predict Y''' \ No newline at end of file From ec9c518fa2527ba2f9e43fcb5eb51792b811f181 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Mon, 2 Mar 2020 16:53:23 +0100 Subject: [PATCH 02/24] keras nn implemented in apply --- flame/apply.py | 2 ++ flame/learn.py | 2 +- flame/stats/Keras.py | 28 +++++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/flame/apply.py b/flame/apply.py index bd7678ac..e1445327 100644 --- a/flame/apply.py +++ b/flame/apply.py @@ -32,6 +32,7 @@ from flame.stats.PLSDA import PLSDA from flame.stats.combo import median, mean, majority, matrix from flame.stats.XGboost import XGBOOST +from flame.stats.Keras import Keras_nn from sklearn.metrics import mean_squared_error, matthews_corrcoef as mcc from sklearn.metrics import f1_score from sklearn.metrics import make_scorer @@ -55,6 +56,7 @@ def __init__(self, parameters, conveyor): ('GNB', GNB), ('PLSR', PLSR), ('PLSDA', PLSDA), + ('Keras', Keras_nn), ('median', median), ('mean', mean), ('majority', majority), diff --git a/flame/learn.py b/flame/learn.py index b73b0058..9d92b998 100644 --- a/flame/learn.py +++ b/flame/learn.py @@ -66,9 +66,9 @@ def __init__(self, parameters, conveyor): ('GNB', GNB), ('PLSR', PLSR), ('PLSDA', PLSDA), + ('Keras', Keras_nn), ('median', median), ('mean', mean), - ('Keras', Keras_nn), ('majority', majority), ('matrix', matrix)] diff --git a/flame/stats/Keras.py b/flame/stats/Keras.py index 43ca48d5..e596b7e8 100644 --- a/flame/stats/Keras.py +++ b/flame/stats/Keras.py @@ -232,7 +232,33 @@ def create_model(self): model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) return model - + # Overrides regular project to single class prediction + def regularProject(self, Xb): + ''' projects a collection of query objects in a regular model, + for obtaining predictions ''' + print("**************HERE***********************") + Yp = self.estimator.predict(Xb) + Yp = np.asarray([x[0] for x in Yp]) + print(Yp) + + + # if conveyor contains experimental values for any of the objects replace the + # predictions with the experimental results + exp = self.conveyor.getVal('experim') + if exp is not None: + if len(exp) == len(Yp): + for i in range (len(Yp)): + if not np.isnan(exp[i]): + # print (exp[i], Yp[i]) + Yp[i] = exp[i] + else: + # if exp is nan, substitute it with a number which can be recognized + # to facilitate handling and do not replace Yp + exp[i]= float ('-99999') + + self.conveyor.addVal(Yp, 'values', 'Prediction', + 'result', 'objs', + 'Results of the prediction', 'main') ## Overriding of parent methods # def CF_quantitative_validation(self): From 6d5af12150c9027d97a10e83752fbf0211fa52c5 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 24 Mar 2020 12:38:36 +0100 Subject: [PATCH 03/24] modelpath --- flame/config.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flame/config.yaml b/flame/config.yaml index b9bd75e2..0f25b5b7 100644 --- a/flame/config.yaml +++ b/flame/config.yaml @@ -2,7 +2,7 @@ admin_email: manuel.pastor@upf.edu admin_name: Manuel Pastor config_status: true homepage: http://phi.upf.edu -model_repository_path: /home/jcgomez/projects/internals/flame/eye_irritation -space_repository_path: C:\Users\mpastor\Documents\soft\flame\flame\spaces -predictions_repository_path: /home/jcgomez/projects/internals/flame/eye_irritation +model_repository_path: D:\synced\internals\bsep +space_repository_path: D:\synced\internals\bsep +predictions_repository_path: D:\synced\internals\bsep provider: UPF (Pompeu Fabra University) From d5db13b6a442cb4801f1d5d2626a31f867366849 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Wed, 25 Mar 2020 17:06:00 +0100 Subject: [PATCH 04/24] -method added to nonconformist to clear data after nn building. - keras verbose=0 -n_jobs must be=1 for nn in crossval. --- flame/config.yaml | 6 +++--- flame/stats/Keras.py | 13 +++++++++---- flame/stats/base_model.py | 7 +++++-- flame/stats/model_validation.py | 2 +- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/flame/config.yaml b/flame/config.yaml index 0f25b5b7..0d2fe765 100644 --- a/flame/config.yaml +++ b/flame/config.yaml @@ -2,7 +2,7 @@ admin_email: manuel.pastor@upf.edu admin_name: Manuel Pastor config_status: true homepage: http://phi.upf.edu -model_repository_path: D:\synced\internals\bsep -space_repository_path: D:\synced\internals\bsep -predictions_repository_path: D:\synced\internals\bsep +model_repository_path: D:\synced\internals\flame\eye_irritation +space_repository_path: D:\synced\internals\flame\eye_irritation +predictions_repository_path: D:\synced\internals\flame\eye_irritation provider: UPF (Pompeu Fabra University) diff --git a/flame/stats/Keras.py b/flame/stats/Keras.py index e596b7e8..b51dd547 100644 --- a/flame/stats/Keras.py +++ b/flame/stats/Keras.py @@ -154,7 +154,8 @@ def build(self): #'silent': 1, 'n_estimators': 100 } - self.estimator = KerasClassifier(build_fn=self.create_model, **self.estimator_parameters) + self.estimator = KerasClassifier(build_fn=self.create_model, + **self.estimator_parameters, verbose=0) results.append(('model', 'model type', 'Keras qualitative')) self.estimator.fit(X, Y) @@ -219,14 +220,14 @@ def build(self): raise e return False, f'Exception building conformal Keras estimator with exception {e}' - return True, results + return True, [] # Function to create model, required for KerasClassifier def create_model(self): # create model model = Sequential() - model.add(Dense(5, input_dim=25, activation='relu')) - model.add(Dense(5, activation='relu')) + model.add(Dense(10, input_dim=25, activation='relu')) + model.add(Dense(50, activation='sigmoid')) model.add(Dense(1, activation='sigmoid')) # Compile model model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) @@ -259,6 +260,10 @@ def regularProject(self, Xb): self.conveyor.addVal(Yp, 'values', 'Prediction', 'result', 'objs', 'Results of the prediction', 'main') + # Overrides base_model validation + # def validate(self,): + # results ['quality'] = [] + # return True, None ## Overriding of parent methods # def CF_quantitative_validation(self): diff --git a/flame/stats/base_model.py b/flame/stats/base_model.py index 0fcac246..bc0149b7 100644 --- a/flame/stats/base_model.py +++ b/flame/stats/base_model.py @@ -464,6 +464,8 @@ def quantitativeValidation(self): results ['Y_pred'] = y_pred return True, results + + def qualitativeValidation(self): ''' performs validation for qualitative models ''' @@ -510,7 +512,7 @@ def qualitativeValidation(self): try: y_pred = cross_val_predict(self.estimator, X, Y, cv=self.cv, - n_jobs=-1) + n_jobs=1) except Exception as e: LOG.error(f'Cross-validation failed with exception' f'exception {e}') @@ -835,4 +837,5 @@ def load_model(self): raise Exception('Loaded estimator is None.' 'Probably model building was not successful') - return \ No newline at end of file + return + diff --git a/flame/stats/model_validation.py b/flame/stats/model_validation.py index 78088a71..697f49e3 100644 --- a/flame/stats/model_validation.py +++ b/flame/stats/model_validation.py @@ -125,7 +125,7 @@ def getCrossVal(cv, rs, n, p): # Splitter Classes: # K-Folds cross-validator - kfold = KFold(n_splits=n, random_state=rs, shuffle=False) + kfold = KFold(n_splits=n, random_state=rs, shuffle=True) # K-fold iterator variant with non-overlapping groups. gkfold = GroupKFold(n_splits=n) # Stratified K-Folds cross-validator From 351d3047a7d4d739ee738460b284b6b5875100f4 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Mon, 30 Mar 2020 13:55:55 +0200 Subject: [PATCH 05/24] keras implementation alpha version --- environment.yml | 2 ++ flame/learn.py | 10 +++++++++- flame/stats/Keras.py | 35 ++++++++++++++--------------------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/environment.yml b/environment.yml index d5d8b479..1a543f84 100644 --- a/environment.yml +++ b/environment.yml @@ -2,9 +2,11 @@ name: flame channels: - default - rdkit + - anaconda - mordred-descriptor - conda-forge dependencies: + - keras-gpu - cherrypy=>18 - numpy=>1.14.0 - matplotlib=>2.1.2 diff --git a/flame/learn.py b/flame/learn.py index 9d92b998..8a606ac9 100644 --- a/flame/learn.py +++ b/flame/learn.py @@ -24,6 +24,7 @@ import os import pickle import numpy as np +import time from flame.stats.RF import RF from flame.stats.SVM import SVM @@ -318,6 +319,8 @@ def run(self): ''' Builds the model using the appropriate toolkit (internal or custom). ''' + # Count the time + start = time.time() toolkit = self.param.getVal('modelingToolkit') @@ -332,5 +335,10 @@ def run(self): LOG.error("Modeling toolkit is not yet supported") self.conveyor.setError( 'modeling Toolkit ' + \ toolkit+' is not supported yet') - + + end = time.time() + hours, rem = divmod(end-start, 3600) + minutes, seconds = divmod(rem, 60) + LOG.info("{:0>2}:{:0>2}:{:05.2f}".format(int(hours), + int(minutes),seconds)) return diff --git a/flame/stats/Keras.py b/flame/stats/Keras.py index b51dd547..1516d59b 100644 --- a/flame/stats/Keras.py +++ b/flame/stats/Keras.py @@ -34,6 +34,7 @@ os.environ["CUDA_VISIBLE_DEVICES"]="0" from keras.models import Sequential from keras.layers import Dense +from sklearn.base import clone import numpy as np @@ -134,26 +135,13 @@ def build(self): try: if self.param.getVal('quantitative'): - LOG.info("Building Quantitative KERAS model") - params = { - 'objective': 'reg:squarederror', - # 'max_depth': 20, - # 'learning_rate': 1.0, - # 'silent': 1, - # 'n_estimators': 25 - } - self.estimator = KerasRegressor(**params) + LOG.info("Building Quantitative KERAS mode") + self.estimator = KerasRegressor(build_fn=self.create_model, + **self.estimator_parameters, verbose=0) results.append(('model', 'model type', 'Keras quantitative')) else: LOG.info("Building Qualitative Keras model") - params = { - 'objective': 'binary:logistic', - 'max_depth': 3, - #'learning_rate': 0.7, - #'silent': 1, - 'n_estimators': 100 - } self.estimator = KerasClassifier(build_fn=self.create_model, **self.estimator_parameters, verbose=0) results.append(('model', 'model type', 'Keras qualitative')) @@ -165,7 +153,7 @@ def build(self): raise e return False, f'Exception building Keras estimator with exception {e}' - self.estimator_temp = copy(self.estimator) + self.estimator_temp = clone(self.estimator) if not self.param.getVal('conformal'): return True, results @@ -213,6 +201,7 @@ def build(self): BootstrapSampler()) # Fit estimator to the data + print('build finished') self.estimator.fit(X, Y) results.append(('model', 'model type', 'conformal Keras qualitative')) @@ -226,18 +215,22 @@ def build(self): def create_model(self): # create model model = Sequential() - model.add(Dense(10, input_dim=25, activation='relu')) - model.add(Dense(50, activation='sigmoid')) + model.add(Dense(10, input_dim=199, activation='relu')) + model.add(Dense(20, activation='sigmoid')) model.add(Dense(1, activation='sigmoid')) # Compile model - model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) + + if self.param.getVal('quantitative'): + loss = 'mean_squared_error' + else: + loss = 'binary_crossentropy' + model.compile(loss=loss, optimizer='adam', metrics=['accuracy']) return model # Overrides regular project to single class prediction def regularProject(self, Xb): ''' projects a collection of query objects in a regular model, for obtaining predictions ''' - print("**************HERE***********************") Yp = self.estimator.predict(Xb) Yp = np.asarray([x[0] for x in Yp]) print(Yp) From 137cc72f052628dd24e791c02e568cc0765d0544 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 31 Mar 2020 13:42:52 +0200 Subject: [PATCH 06/24] shuffle=True in Kfold --- flame/stats/base_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flame/stats/base_model.py b/flame/stats/base_model.py index c876447f..d015fa59 100644 --- a/flame/stats/base_model.py +++ b/flame/stats/base_model.py @@ -102,7 +102,7 @@ def getCrossVal(cv, rs, n, p): if cv == 'kfold': from sklearn.model_selection import KFold - return KFold(n_splits=n, random_state=rs, shuffle=False) + return KFold(n_splits=n, random_state=rs, shuffle=True) if cv == 'lpo': from sklearn.model_selection import LeavePOut From e0050f57e8ae118a706f7730a667f0083445c406 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 31 Mar 2020 15:57:47 +0200 Subject: [PATCH 07/24] default modelling folders --- flame/config.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flame/config.yaml b/flame/config.yaml index 0d2fe765..64049601 100644 --- a/flame/config.yaml +++ b/flame/config.yaml @@ -2,7 +2,7 @@ admin_email: manuel.pastor@upf.edu admin_name: Manuel Pastor config_status: true homepage: http://phi.upf.edu -model_repository_path: D:\synced\internals\flame\eye_irritation -space_repository_path: D:\synced\internals\flame\eye_irritation -predictions_repository_path: D:\synced\internals\flame\eye_irritation +model_repository_path: C:\Users\mpastor\Documents\soft\flame\flame\models +space_repository_path: C:\Users\mpastor\Documents\soft\flame\flame\spaces +predictions_repository_path: C:\Users\mpastor\Documents\soft\flame\flame\predictions provider: UPF (Pompeu Fabra University) From e021373745469611fe7c267ff1913a34a56ab451 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 31 Mar 2020 16:00:49 +0200 Subject: [PATCH 08/24] keras =>2.3.1 veresion required --- environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index 1484bead..2c180570 100644 --- a/environment.yml +++ b/environment.yml @@ -12,7 +12,7 @@ dependencies: - scikit-learn=>0.19.1 - requests - rdkit - - keras-gpu + - keras-gpu=>2.3.1 - pyyaml - pytest - psycopg2 From b5295a154bf9165027596beed647c33b8f92972c Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 31 Mar 2020 16:27:45 +0200 Subject: [PATCH 09/24] tensorflow=>2.3.3 --- environment.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/environment.yml b/environment.yml index 2c180570..c6ec5f5b 100644 --- a/environment.yml +++ b/environment.yml @@ -12,6 +12,7 @@ dependencies: - scikit-learn=>0.19.1 - requests - rdkit + - tensorflow-gpu=>2.1.0 - keras-gpu=>2.3.1 - pyyaml - pytest From 3d95b31d9fcb078c77435c6e6044203668a97037 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 14 Apr 2020 14:57:25 +0200 Subject: [PATCH 10/24] Environment for GPU implemented --- flame/environment.yml | 27 +++++++++++++++++++++++++++ flame/environment_gpu.yml | 27 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 flame/environment.yml create mode 100644 flame/environment_gpu.yml diff --git a/flame/environment.yml b/flame/environment.yml new file mode 100644 index 00000000..32acc14a --- /dev/null +++ b/flame/environment.yml @@ -0,0 +1,27 @@ +name: flame +channels: + - default + - rdkit + - conda-forge + - chembl + - anaconda +dependencies: + - pip + - python=3.6 + - setuptools=>38.4.0 + - scikit-learn=>0.19.1 + - requests + - rdkit + - keras + - pyyaml + - pytest + - psycopg2 + - chembl_structure_pipeline + - pip: + - xgboost + - django==2.2.8 + - djangorestframework + - django-cors-headers + - djangorestframework-yaml + - "https://github.com/phi-grib/standardiser/archive/master.zip" + - "https://github.com/josecarlosgomezt/nonconformist/archive/master.zip" \ No newline at end of file diff --git a/flame/environment_gpu.yml b/flame/environment_gpu.yml new file mode 100644 index 00000000..1484bead --- /dev/null +++ b/flame/environment_gpu.yml @@ -0,0 +1,27 @@ +name: flame +channels: + - default + - rdkit + - conda-forge + - chembl + - anaconda +dependencies: + - pip + - python=3.6 + - setuptools=>38.4.0 + - scikit-learn=>0.19.1 + - requests + - rdkit + - keras-gpu + - pyyaml + - pytest + - psycopg2 + - chembl_structure_pipeline + - pip: + - xgboost + - django==2.2.8 + - djangorestframework + - django-cors-headers + - djangorestframework-yaml + - "https://github.com/phi-grib/standardiser/archive/master.zip" + - "https://github.com/josecarlosgomezt/nonconformist/archive/master.zip" \ No newline at end of file From 726a998429fc90ae5538db841c8433be41ed1047 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 14 Apr 2020 15:18:30 +0200 Subject: [PATCH 11/24] # test error --- flame/environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flame/environment.yml b/flame/environment.yml index 32acc14a..cbbfc6b6 100644 --- a/flame/environment.yml +++ b/flame/environment.yml @@ -18,7 +18,7 @@ dependencies: - psycopg2 - chembl_structure_pipeline - pip: - - xgboost + #- xgboost - django==2.2.8 - djangorestframework - django-cors-headers From 0bb07ca300fd8ade1af21175c9c8f510dd271d8a Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 14 Apr 2020 15:28:15 +0200 Subject: [PATCH 12/24] # test2 --- flame/environment.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flame/environment.yml b/flame/environment.yml index cbbfc6b6..5de4fd97 100644 --- a/flame/environment.yml +++ b/flame/environment.yml @@ -12,13 +12,13 @@ dependencies: - scikit-learn=>0.19.1 - requests - rdkit - - keras + #- keras - pyyaml - pytest - psycopg2 - chembl_structure_pipeline - pip: - #- xgboost + - xgboost - django==2.2.8 - djangorestframework - django-cors-headers From 8b7b883fb2e1af827fab689f13ae6103ccfb3dab Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 14 Apr 2020 15:43:27 +0200 Subject: [PATCH 13/24] # test3 --- flame/environment.yml | 1 + flame/environment_gpu.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/flame/environment.yml b/flame/environment.yml index 5de4fd97..e0be0a3b 100644 --- a/flame/environment.yml +++ b/flame/environment.yml @@ -12,6 +12,7 @@ dependencies: - scikit-learn=>0.19.1 - requests - rdkit + - matplotlib #- keras - pyyaml - pytest diff --git a/flame/environment_gpu.yml b/flame/environment_gpu.yml index 1484bead..cde9f165 100644 --- a/flame/environment_gpu.yml +++ b/flame/environment_gpu.yml @@ -12,6 +12,7 @@ dependencies: - scikit-learn=>0.19.1 - requests - rdkit + - matplotlib - keras-gpu - pyyaml - pytest From 9763bfa8d6f73630f30df1158605d3a3f8dd10af Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 14 Apr 2020 15:59:33 +0200 Subject: [PATCH 14/24] # graph folder to module --- flame/graph/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 flame/graph/__init__.py diff --git a/flame/graph/__init__.py b/flame/graph/__init__.py new file mode 100644 index 00000000..e69de29b From c39d3942dbc64e713dbba6f9c22ffa64af41ec83 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 14 Apr 2020 16:15:12 +0200 Subject: [PATCH 15/24] # test4 --- flame/environment.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/flame/environment.yml b/flame/environment.yml index e0be0a3b..82b0243b 100644 --- a/flame/environment.yml +++ b/flame/environment.yml @@ -13,13 +13,11 @@ dependencies: - requests - rdkit - matplotlib - #- keras - pyyaml - pytest - psycopg2 - chembl_structure_pipeline - pip: - - xgboost - django==2.2.8 - djangorestframework - django-cors-headers From a60dba1456e0553d589f53e5b1bb8d6effe03b22 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 14 Apr 2020 16:26:42 +0200 Subject: [PATCH 16/24] # test5 --- flame/environment.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flame/environment.yml b/flame/environment.yml index 82b0243b..32acc14a 100644 --- a/flame/environment.yml +++ b/flame/environment.yml @@ -12,12 +12,13 @@ dependencies: - scikit-learn=>0.19.1 - requests - rdkit - - matplotlib + - keras - pyyaml - pytest - psycopg2 - chembl_structure_pipeline - pip: + - xgboost - django==2.2.8 - djangorestframework - django-cors-headers From ef6cd44af63090a931463a9f3e1e02eb35cb9f63 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 14 Apr 2020 16:40:51 +0200 Subject: [PATCH 17/24] # test 6 --- flame/environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flame/environment.yml b/flame/environment.yml index 32acc14a..baf36965 100644 --- a/flame/environment.yml +++ b/flame/environment.yml @@ -18,7 +18,7 @@ dependencies: - psycopg2 - chembl_structure_pipeline - pip: - - xgboost + # - xgboost - django==2.2.8 - djangorestframework - django-cors-headers From d0a23b5b677ae387acb30f922198cf2e1502ac7e Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 14 Apr 2020 16:47:29 +0200 Subject: [PATCH 18/24] # test7 --- environment.yml | 3 +-- ...environment_gpu.yml => environment_gpu.yml | 0 flame/environment.yml | 27 ------------------- 3 files changed, 1 insertion(+), 29 deletions(-) rename flame/environment_gpu.yml => environment_gpu.yml (100%) delete mode 100644 flame/environment.yml diff --git a/environment.yml b/environment.yml index 3a175622..0ea879e7 100644 --- a/environment.yml +++ b/environment.yml @@ -13,8 +13,7 @@ dependencies: - matplotlib - requests - rdkit - - tensorflow-gpu=>2.1.0 - - keras-gpu=>2.3.1 + - keras - pyyaml - pytest - psycopg2 diff --git a/flame/environment_gpu.yml b/environment_gpu.yml similarity index 100% rename from flame/environment_gpu.yml rename to environment_gpu.yml diff --git a/flame/environment.yml b/flame/environment.yml deleted file mode 100644 index baf36965..00000000 --- a/flame/environment.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: flame -channels: - - default - - rdkit - - conda-forge - - chembl - - anaconda -dependencies: - - pip - - python=3.6 - - setuptools=>38.4.0 - - scikit-learn=>0.19.1 - - requests - - rdkit - - keras - - pyyaml - - pytest - - psycopg2 - - chembl_structure_pipeline - - pip: - # - xgboost - - django==2.2.8 - - djangorestframework - - django-cors-headers - - djangorestframework-yaml - - "https://github.com/phi-grib/standardiser/archive/master.zip" - - "https://github.com/josecarlosgomezt/nonconformist/archive/master.zip" \ No newline at end of file From 9d722d0c1ffa1fd2a56012e5a27742117f886745 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Thu, 16 Apr 2020 16:30:29 +0200 Subject: [PATCH 19/24] # implemented my_keras to override keras model with custom design # parallelization for keras dissable by setting self.n_jobs=1 # Optimization of keras overrided with nothing to do --- flame/children/my_keras.py | 44 ++++++++++++++++++++++++++++++++++++++ flame/stats/Keras.py | 28 +++++++++++------------- flame/stats/base_model.py | 15 +++++++++++-- 3 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 flame/children/my_keras.py diff --git a/flame/children/my_keras.py b/flame/children/my_keras.py new file mode 100644 index 00000000..e1846d77 --- /dev/null +++ b/flame/children/my_keras.py @@ -0,0 +1,44 @@ +#! -*- coding: utf-8 -*- + +# Description Flame Apply internal class +## +# Authors: Manuel Pastor (manuel.pastor@upf.edu) +## +# Copyright 2018 Manuel Pastor +## +# This file is part of Flame +## +# Flame is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation version 3. +## +# Flame is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +## +# You should have received a copy of the GNU General Public License +# along with Flame. If not, see . + +import numpy as np +from flame.stats.Keras import Keras_nn + +class my_keras(Keras_nn): + def __init__(self, X, Y, parameters, conveyor): + Keras_nn.__init__(self, X, Y, parameters, conveyor) + +# Function to create model, required for KerasClassifier + def create_model(self, dim=20): + # create model + model = Sequential() + model.add(Dense(10, input_dim=dim, activation='relu')) + model.add(Dense(20, activation='sigmoid')) + model.add(Dense(1, activation='sigmoid')) + # Compile model + + if self.param.getVal('quantitative'): + loss = 'mean_squared_error' + else: + loss = 'binary_crossentropy' + model.compile(loss=loss, optimizer='adam', metrics=['accuracy']) + return model \ No newline at end of file diff --git a/flame/stats/Keras.py b/flame/stats/Keras.py index 1516d59b..4b4d2d0e 100644 --- a/flame/stats/Keras.py +++ b/flame/stats/Keras.py @@ -122,8 +122,8 @@ def build(self): else: self.estimator = KerasClassifier( **self.estimator_parameters) - params = self.estimator.get_params() - params['num_class'] = 2 + #params = self.estimator.get_params() + #params['num_class'] = 2 self.optimize(X, Y, self.estimator, self.tune_parameters) results.append(('model','model type','KERAS qualitative (optimized)')) @@ -142,7 +142,7 @@ def build(self): else: LOG.info("Building Qualitative Keras model") - self.estimator = KerasClassifier(build_fn=self.create_model, + self.estimator = KerasClassifier(build_fn=self.create_model, dim=self.X.shape[1], **self.estimator_parameters, verbose=0) results.append(('model', 'model type', 'Keras qualitative')) @@ -212,18 +212,18 @@ def build(self): return True, [] # Function to create model, required for KerasClassifier - def create_model(self): + def create_model(self, dim=20): # create model model = Sequential() - model.add(Dense(10, input_dim=199, activation='relu')) + model.add(Dense(10, input_dim=dim, activation='relu')) model.add(Dense(20, activation='sigmoid')) model.add(Dense(1, activation='sigmoid')) # Compile model - if self.param.getVal('quantitative'): - loss = 'mean_squared_error' - else: - loss = 'binary_crossentropy' + #if self.param.getVal('quantitative'): + #loss = 'mean_squared_error' + #else: + loss = 'binary_crossentropy' model.compile(loss=loss, optimizer='adam', metrics=['accuracy']) return model @@ -233,8 +233,6 @@ def regularProject(self, Xb): for obtaining predictions ''' Yp = self.estimator.predict(Xb) Yp = np.asarray([x[0] for x in Yp]) - print(Yp) - # if conveyor contains experimental values for any of the objects replace the # predictions with the experimental results @@ -279,10 +277,10 @@ def regularProject(self, Xb): # ''' Validates the model and computes suitable model quality scoring values''' - # def optimize(self, X, Y, estimator, tune_parameters): - # ''' optimizes a model using a grid search over a range of values for diverse parameters''' - - + def optimize(self, X, Y, estimator, tune_parameters): + # TODO Perhaps not suitable + ''' optimizes a model using a grid search over a range of values for diverse parameters''' + LOG.info('Keras model parameter optimization not implemented. skipping....') # def regularProject(self, Xb, results): # ''' projects a collection of query objects in a regular model, for obtaining predictions ''' diff --git a/flame/stats/base_model.py b/flame/stats/base_model.py index 7539fca2..072427ab 100644 --- a/flame/stats/base_model.py +++ b/flame/stats/base_model.py @@ -146,6 +146,15 @@ def __init__(self, X, Y, parameters, conveyor=None): self.Y = Y self.nobj, self.nvarx = np.shape(X) + # Check if model is a keras model and + # adjust n_jobs=1 if so + if self.param.getVal('model') == 'my_keras'\ + or self.param.getVal('model') == 'Keras': + self.n_jobs = 1 + else: + self.n_jobs = -1 + + # Get cross-validator # Consider to include a Random Seed for cross-validator if self.param.getVal('ModelValidationCV'): @@ -426,12 +435,13 @@ def quantitativeValidation(self): raise e # Compute Cross-validation quality metrics + try: # Get predicted Y y_pred = cross_val_predict(copy.copy(self.estimator), copy.copy(X), copy.copy(Y), cv=self.cv, - n_jobs=1) + n_jobs=self.n_jobs) SSY0_out = np.sum(np.square(Ym - Y)) SSY_out = np.sum(np.square(Y - y_pred)) self.scoringP = mean_squared_error(Y, y_pred) @@ -512,7 +522,8 @@ def qualitativeValidation(self): # Get cross-validated Y try: - y_pred = cross_val_predict(self.estimator, X, Y, cv=self.cv, n_jobs=-1) + y_pred = cross_val_predict(self.estimator, X, Y, cv=self.cv, + n_jobs=self.n_jobs) except Exception as e: LOG.error(f'Cross-validation failed with exception' f'exception {e}') From d20aabed47f7bc34ebb07a0f36f0d11387182136 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Wed, 29 Apr 2020 10:18:28 +0200 Subject: [PATCH 20/24] changed environment name to flame_gpu --- environment_gpu.yml | 2 +- flame/config.yaml | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/environment_gpu.yml b/environment_gpu.yml index cde9f165..bff84d92 100644 --- a/environment_gpu.yml +++ b/environment_gpu.yml @@ -1,4 +1,4 @@ -name: flame +name: flame_gpu channels: - default - rdkit diff --git a/flame/config.yaml b/flame/config.yaml index 08df04e4..41ac591b 100644 --- a/flame/config.yaml +++ b/flame/config.yaml @@ -2,7 +2,7 @@ admin_email: manuel.pastor@upf.edu admin_name: Manuel Pastor config_status: true homepage: http://phi.upf.edu -model_repository_path: /mnt/e/synced/internals/flame/eye_irritation/models -predictions_repository_path: /mnt/e/synced/internals/flame/eye_irritation/predictions +model_repository_path: E:\synced\internals\flame\eye_irritation\models +predictions_repository_path: E:\synced\internals\flame\eye_irritation\predictions provider: UPF (Pompeu Fabra University) -space_repository_path: /mnt/e/synced/internals/flame/eye_irritation/spaces +space_repository_path: E:\synced\internals\flame\eye_irritation\spaces From d510690008f60d7ee845e7e8c1c551bc533cdf91 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Wed, 29 Apr 2020 10:52:24 +0200 Subject: [PATCH 21/24] Improved keras parameter default settings --- flame/children/parameters.yaml | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/flame/children/parameters.yaml b/flame/children/parameters.yaml index 6444c7a7..78e98ff4 100644 --- a/flame/children/parameters.yaml +++ b/flame/children/parameters.yaml @@ -1036,12 +1036,12 @@ Keras_parameters: epochs: object_type: list writable: false - value: 150 + value: 100 options: - 100 - 200 - 250 - description: Maximum tree depth for base learners. + description: Number of epochs batch_size: object_type: float writable: true @@ -1050,7 +1050,16 @@ Keras_parameters: - 1 - 10 - 30 - description: Boosting learning rate (xgb's "eta") + description: Batch size for model Keras batch learning + learning_rate: + object_type: float + writable: true + value: 0.1 + options: + - 0.1 + - 0.01 + - 0.001 + description: Learning rate value description: dependencies: model: SKLEARN @@ -1071,23 +1080,10 @@ Keras_optimize: - 100 - 150 options: - - 1 - - 3 - - 6 - description: - learning_rate: - object_type: float - writable: true - value: - - 1 - - 10 - - 30 - options: - - 1 - - 10 - - 30 + - 50 + - 100 + - 150 description: - description: Keras optimize parameters dependencies: model: Keras From 1d02d19a328d9e437fa99b3e22f76fb643423eaa Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Wed, 29 Apr 2020 11:31:02 +0200 Subject: [PATCH 22/24] default parameters for keras estimator adjusted --- flame/children/parameters.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/flame/children/parameters.yaml b/flame/children/parameters.yaml index 78e98ff4..259c3496 100644 --- a/flame/children/parameters.yaml +++ b/flame/children/parameters.yaml @@ -1045,7 +1045,7 @@ Keras_parameters: batch_size: object_type: float writable: true - value: 1 + value: 36 options: - 1 - 10 @@ -1054,8 +1054,9 @@ Keras_parameters: learning_rate: object_type: float writable: true - value: 0.1 + value: 1 options: + - 1 - 0.1 - 0.01 - 0.001 From ff9d13651486a8b93b39bed210485579762220cb Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Tue, 14 Jul 2020 11:15:26 +0200 Subject: [PATCH 23/24] keras implementation cleaning Learning rate removed from parameters.yaml --- flame/children/parameters.yaml | 10 ---------- flame/stats/Keras.py | 17 +++++++++++++---- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/flame/children/parameters.yaml b/flame/children/parameters.yaml index 9208315b..fcd8a58e 100644 --- a/flame/children/parameters.yaml +++ b/flame/children/parameters.yaml @@ -1091,16 +1091,6 @@ Keras_parameters: - 10 - 30 description: Batch size for model Keras batch learning - learning_rate: - object_type: float - writable: true - value: 1 - options: - - 1 - - 0.1 - - 0.01 - - 0.001 - description: Learning rate value description: dependencies: model: SKLEARN diff --git a/flame/stats/Keras.py b/flame/stats/Keras.py index 41216ef2..7b4147aa 100644 --- a/flame/stats/Keras.py +++ b/flame/stats/Keras.py @@ -35,6 +35,7 @@ from keras.models import Sequential from keras.layers import Dense from sklearn.base import clone +import keras import numpy as np @@ -140,7 +141,7 @@ def build(self): **self.estimator_parameters, verbose=0) results.append(('model', 'model type', 'Keras quantitative')) else: - + print(self.estimator_parameters) LOG.info("Building Qualitative Keras model") self.estimator = KerasClassifier(build_fn=self.create_model, dim=self.X.shape[1], **self.estimator_parameters, verbose=0) @@ -212,10 +213,10 @@ def build(self): return True, [] # Function to create model, required for KerasClassifier - def create_model(self, dim=20): + def create_model(self, dim=568): # create model model = Sequential() - model.add(Dense(10, input_dim=dim, activation='relu')) + model.add(Dense(50, input_dim=dim, activation='relu')) model.add(Dense(20, activation='sigmoid')) model.add(Dense(1, activation='sigmoid')) # Compile model @@ -224,7 +225,15 @@ def create_model(self, dim=20): #loss = 'mean_squared_error' #else: loss = 'binary_crossentropy' - model.compile(loss=loss, optimizer='adam', metrics=['accuracy']) + optimizer = keras.optimizers.Adam(lr=0.1) + model.compile(loss=loss, optimizer=optimizer, metrics=['accuracy']) + # model.compile( + # optimizer=keras.optimizers.Adam( + # hp.Choice('learning_rate', + # values=[1e-2, 1e-3, 1e-4])), + # loss='sparse_categorical_crossentropy', + # metrics=['accuracy']) + return model # Overrides regular project to single class prediction From 80f877130282d962ac707198f791c4fc79cc7952 Mon Sep 17 00:00:00 2001 From: josecarlosgomezt Date: Mon, 20 Jul 2020 18:53:01 +0200 Subject: [PATCH 24/24] Keras update. Fixed model settings incompatibility. --- flame/stats/Keras.py | 6 +++--- flame/stats/base_model.py | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/flame/stats/Keras.py b/flame/stats/Keras.py index 7b4147aa..3cd49c60 100644 --- a/flame/stats/Keras.py +++ b/flame/stats/Keras.py @@ -210,13 +210,13 @@ def build(self): raise e return False, f'Exception building conformal Keras estimator with exception {e}' - return True, [] + return True, results # Function to create model, required for KerasClassifier - def create_model(self, dim=568): + def create_model(self, dim=25): # create model model = Sequential() - model.add(Dense(50, input_dim=dim, activation='relu')) + model.add(Dense(30, input_dim=dim, activation='relu')) model.add(Dense(20, activation='sigmoid')) model.add(Dense(1, activation='sigmoid')) # Compile model diff --git a/flame/stats/base_model.py b/flame/stats/base_model.py index fcb3d2ff..343d1f34 100644 --- a/flame/stats/base_model.py +++ b/flame/stats/base_model.py @@ -1160,6 +1160,8 @@ def save_model(self): params = self.estimator_temp.get_params() else: params = self.estimator.get_params() + if self.param.getVal('model') == "Keras": + params = ["Keras sequential model"] self.conveyor.addVal(params, 'estimator_parameters', 'estimator parameters', 'method', 'single',