Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
475f9bb
first approximation working
Feb 28, 2020
ec9c518
keras nn implemented in apply
Mar 2, 2020
6d5af12
modelpath
josecarlosgomezt Mar 24, 2020
d5db13b
-method added to nonconformist to clear data after nn building.
josecarlosgomezt Mar 25, 2020
351d304
keras implementation alpha version
josecarlosgomezt Mar 30, 2020
d4adc4f
merged
josecarlosgomezt Mar 30, 2020
137cc72
shuffle=True in Kfold
josecarlosgomezt Mar 31, 2020
2768347
merged with master
josecarlosgomezt Mar 31, 2020
e0050f5
default modelling folders
josecarlosgomezt Mar 31, 2020
e021373
keras =>2.3.1 veresion required
josecarlosgomezt Mar 31, 2020
b5295a1
tensorflow=>2.3.3
josecarlosgomezt Mar 31, 2020
3d95b31
Environment for GPU implemented
josecarlosgomezt Apr 14, 2020
c65e09c
Merge branch 'nn_implement' of https://github.com/phi-grib/flame into…
josecarlosgomezt Apr 14, 2020
7467ef6
Merge branch 'master' into nn_implement
josecarlosgomezt Apr 14, 2020
726a998
# test error
josecarlosgomezt Apr 14, 2020
0bb07ca
# test2
josecarlosgomezt Apr 14, 2020
8b7b883
# test3
josecarlosgomezt Apr 14, 2020
9763bfa
# graph folder to module
josecarlosgomezt Apr 14, 2020
c39d394
# test4
josecarlosgomezt Apr 14, 2020
a60dba1
# test5
josecarlosgomezt Apr 14, 2020
ef6cd44
# test 6
josecarlosgomezt Apr 14, 2020
d0a23b5
# test7
josecarlosgomezt Apr 14, 2020
9d722d0
# implemented my_keras to override keras model with custom design
josecarlosgomezt Apr 16, 2020
0df4c30
Merge branch 'master' into nn_implement
josecarlosgomezt Apr 28, 2020
d20aabe
changed environment name to flame_gpu
josecarlosgomezt Apr 29, 2020
d510690
Improved keras parameter default settings
josecarlosgomezt Apr 29, 2020
1d02d19
default parameters for keras estimator adjusted
josecarlosgomezt Apr 29, 2020
ed9c92a
Merge branch 'master' into nn_implement
josecarlosgomezt Jun 12, 2020
b77fc5f
Merge branch 'master' into nn_implement
josecarlosgomezt Jul 5, 2020
ff9d136
keras implementation cleaning
josecarlosgomezt Jul 14, 2020
80f8771
Keras update. Fixed model settings incompatibility.
josecarlosgomezt Jul 20, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ channels:
- 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
Expand Down
28 changes: 28 additions & 0 deletions environment_gpu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: flame_gpu
channels:
- default
- rdkit
- conda-forge
- chembl
- anaconda
dependencies:
- pip
- python=3.6
- setuptools=>38.4.0
- scikit-learn=>0.19.1
- requests
- rdkit
- matplotlib
- 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"
2 changes: 2 additions & 0 deletions flame/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,6 +58,7 @@ def __init__(self, parameters, conveyor):
('GNB', GNB),
('PLSR', PLSR),
('PLSDA', PLSDA),
('Keras', Keras_nn),
('median', median),
('mean', mean),
('majority', majority),
Expand Down
44 changes: 44 additions & 0 deletions flame/children/my_keras.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

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
53 changes: 53 additions & 0 deletions flame/children/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,59 @@ XGBOOST_optimize:
comments:
group: modeling

Keras_parameters:
advanced: advanced
object_type: dictionary
writable: false
options: null
value:
epochs:
object_type: list
writable: false
value: 100
options:
- 100
- 200
- 250
description: Number of epochs
batch_size:
object_type: float
writable: true
value: 36
options:
- 1
- 10
- 30
description: Batch size for model Keras batch learning
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:
- 50
- 100
- 150
description:
description: Keras optimize parameters
dependencies:
model: Keras
comments:
group: modeling

output_format:
advanced: regular
Expand Down
12 changes: 11 additions & 1 deletion flame/learn.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import os
import pickle
import numpy as np
import time

from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
Expand All @@ -36,6 +37,7 @@
from flame.stats.PLSDA import PLSDA
from flame.stats import feature_selection
from flame.stats.XGboost import XGBOOST
from flame.stats.Keras import Keras_nn
from flame.stats.combo import median, mean, majority, matrix
from flame.stats.imbalance import run_imbalance

Expand Down Expand Up @@ -65,6 +67,7 @@ def __init__(self, parameters, conveyor):
('GNB', GNB),
('PLSR', PLSR),
('PLSDA', PLSDA),
('Keras', Keras_nn),
('median', median),
('mean', mean),
('majority', majority),
Expand Down Expand Up @@ -397,6 +400,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')

Expand All @@ -411,5 +416,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
19 changes: 14 additions & 5 deletions flame/stats/Keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -209,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=20):
def create_model(self, dim=25):
# create model
model = Sequential()
model.add(Dense(10, 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
Expand All @@ -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
Expand Down
19 changes: 17 additions & 2 deletions flame/stats/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down Expand Up @@ -793,6 +802,7 @@ def quantitativeValidation(self):
return False, f'Error computing goodness of fit with exception: {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)
Expand Down Expand Up @@ -825,6 +835,8 @@ def quantitativeValidation(self):
results ['Y_pred'] = y_pred
return True, results



def qualitativeValidation(self):
''' performs validation for qualitative models '''

Expand Down Expand Up @@ -864,7 +876,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:
return False, f'Cross-validation failed with exception: {e}'

Expand Down Expand Up @@ -1147,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',
Expand Down Expand Up @@ -1180,4 +1195,4 @@ def load_model(self):
if self.estimator is None:
return False, 'No valid model estimator found. Try to rebuild the model'

return True, 'model loaded'
return True, 'model loaded'