Skip to content

Commit 12fc29f

Browse files
Merge pull request #191 from Thilakraj1998/main
Major Enhancement(Support Tensorflow Model)
2 parents f22d991 + 62c4389 commit 12fc29f

File tree

7 files changed

+153
-41
lines changed

7 files changed

+153
-41
lines changed

blobcity/code_gen/Generator.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,16 @@ def modeler(yml_data,key,with_doc,codes="",nb=None):
215215
216216
The function adds code syntax related to the Machine learning model initialization and training.
217217
"""
218-
param=SourceCode.parameters.replace("PARAM", str(yml_data['model']['parameters']))
219-
model=SourceCode.models_init.replace("MODELNAME", str(yml_data['model']['type']))
218+
if yml_data['model']['type'] not in ['TF','tf','Tensorflow']:
219+
param=SourceCode.parameters.replace("PARAM", str(yml_data['model']['parameters']))
220+
model=SourceCode.models_init.replace("MODELNAME", str(yml_data['model']['type']))
221+
else:param,model="\n",SourceCode.tf_load
222+
220223
imports,metaDesc=SourceCode.models[key][yml_data['model']['type']],PyComments.models[key][yml_data['model']['type']]
221224

222225
if nb!=None:
223226
nb['cells'][1]['source']=nb['cells'][1]['source']+imports
224-
if with_doc:
225-
nb['cells'].append(nbf.v4.new_markdown_cell(IpynbComments.models[key][yml_data['model']['type']]))
226-
227+
if with_doc:nb['cells'].append(nbf.v4.new_markdown_cell(IpynbComments.models[key][yml_data['model']['type']]))
227228
nb['cells'].append(nbf.v4.new_code_cell(param+model))
228229
return nb
229230
elif codes!="":
@@ -233,7 +234,7 @@ def modeler(yml_data,key,with_doc,codes="",nb=None):
233234
codes=codes+"# "+metaDesc
234235
return codes+param+model
235236

236-
def model_metrics(key,codes="",nb=None,with_doc=False):
237+
def model_metrics(yml_data,key,codes="",nb=None,with_doc=False):
237238
"""
238239
param1: dictionary : AutoAI steps data
239240
param2: string : Code syntaxs
@@ -246,11 +247,26 @@ def model_metrics(key,codes="",nb=None,with_doc=False):
246247
if with_doc:
247248
if nb!=None: nb['cells'].append(nbf.v4.new_markdown_cell(IpynbComments.procedure['metrics']))
248249
else: codes=codes+PyComments.procedure['metrics']
250+
249251
if nb!=None and codes=="":
250-
nb['cells'].append(nbf.v4.new_code_cell(SourceCode.metric[key]))
252+
if yml_data['model']['type'] not in ['TF','tf','Tensorflow']:
253+
nb['cells'].append(nbf.v4.new_code_cell(SourceCode.metric[key]))
254+
else:
255+
if key == 'Classification':
256+
tf_metric_type=SourceCode.tf_metric[key]['binary'] if yml_data['model']['classification_type']=='binary' else SourceCode.tf_metric[key]['multi']
257+
nb['cells'].append(nbf.v4.new_code_cell(tf_metric_type))
258+
else:nb['cells'].append(nbf.v4.new_code_cell(SourceCode.tf_metric[key]))
251259
return nb
252260
else:
253-
return codes+SourceCode.metric[key]
261+
if yml_data['model']['type'] not in ['TF','tf','Tensorflow']:
262+
return codes+SourceCode.metric[key]
263+
else:
264+
if key == 'Classification':
265+
tf_metric_type=SourceCode.tf_metric[key]['binary'] if yml_data['model']['classification_type']=='binary' else SourceCode.tf_metric[key]['multi']
266+
return codes+tf_metric_type
267+
else:
268+
return codes+SourceCode.tf_metric[key]
269+
254270

255271
def pycoder(yml_data,CGpath,doc=False):
256272
"""
@@ -270,7 +286,7 @@ def pycoder(yml_data,CGpath,doc=False):
270286
codes=add_corr_matrix(codes=codes,with_doc=doc)
271287
codes=splits(codes=codes,with_doc=doc)
272288
codes=modeler(yml_data,key,doc,codes=codes)
273-
codes=model_metrics(key,codes=codes,with_doc=doc)
289+
codes=model_metrics(yml_data,key,codes=codes,with_doc=doc)
274290
write_pycode(CGpath,codes)
275291

276292
def ipynbcoder(yml_data,CGpath,doc=True):
@@ -292,7 +308,7 @@ def ipynbcoder(yml_data,CGpath,doc=True):
292308
nb=add_corr_matrix(nb=nb,with_doc=doc)
293309
nb=splits(nb=nb,with_doc=doc)
294310
nb=modeler(yml_data,key,doc,nb=nb)
295-
nb=model_metrics(key,nb=nb,with_doc=doc)
311+
nb=model_metrics(yml_data,key,nb=nb,with_doc=doc)
296312
write_ipynbcode(CGpath,nb)
297313

298314
def code_generator(data,filepath,doc=None):

blobcity/code_gen/IpynbMeta.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
class IpynbComments:
2020
models={
2121
'Classification':{
22+
'TF':"""### Neural Network/Deep Learning Model \nDeep learning is a subset of machine learning, which is essentially a neural network with three or more layers. These neural networks attempt to simulate the behavior of the human brain-albeit far from matching its ability-allowing it to 'learn' from large amounts of data. While a neural network with a single layer can still make approximate predictions, additional hidden layers can help to optimize and refine for accuracy.""",
2223
'LinearDiscriminantAnalysis':"""### Model\n A classifier with a linear decision boundary, generated by fitting class conditional densities to the data and using Bayes’ rule.\n The model fits a Gaussian density to each class, assuming that all classes share the same covariance matrix.\n The fitted model can also be used to reduce the dimensionality of the input by projecting it to the most discriminative directions, using the transform method.
2324
2425
1. solver: Solver to use, possible values: {'svd', 'lsqr', 'eigen'}
@@ -337,6 +338,7 @@ class IpynbComments:
337338
7. **n_iter_no_change** -> Number of iterations with no improvement to wait before early stopping."""
338339
},
339340
'Regression':{
341+
'TF':"""### Neural Network/Deep Learning Model \nDeep learning is a subset of machine learning, which is essentially a neural network with three or more layers. These neural networks attempt to simulate the behavior of the human brain-albeit far from matching its ability-allowing it to 'learn' from large amounts of data. While a neural network with a single layer can still make approximate predictions, additional hidden layers can help to optimize and refine for accuracy.""",
340342
'OrthogonalMatchingPursuit':"""### Model \nOrthogonalMatchingPursuit and orthogonal_mp implements the OMP algorithm for approximating the fit of a linear model with constraints imposed on the number of non-zero coefficients \n
341343
OMP is based on a greedy algorithm that includes at each step the atom most highly correlated with the current residual. It is similar to the simpler matching pursuit (MP) method, but better in that at each iteration, the residual is recomputed using an orthogonal projection on the space of the previously chosen dictionary elements.
342344
@@ -765,7 +767,6 @@ class IpynbComments:
765767
2. **tol** -> Stopping criterion.
766768
767769
3. **max_iter** -> The maximal number of iterations for the solver.""",
768-
'TF':'Neural Network Model Description'
769770
}
770771
}
771772

blobcity/code_gen/PyMeta.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class PyComments:
4242
'LGBMClassifier':'LightGBM is a gradient boosting framework that uses tree based learning algorithms.\n# It is designed to be distributed and efficiency\r\n',
4343
'PassiveAggressiveClassifier':'The passive-aggressive algorithms are a family of algorithms for large-scale learning.\n# They are similar to the Perceptron in that they do not require a learning rate. However, contrary to the Perceptron,\n# they include a regularization parameter C.\r\n',
4444
'LinearDiscriminantAnalysis':'A classifier with a linear decision boundary, generated by fitting class conditional densities to the data and using Bayes’ rule.\n #The model fits a Gaussian density to each class, assuming that all classes share the same covariance matrix.\n# The fitted model can also be used to reduce the dimensionality of the input by projecting it to the most discriminative directions, using the transform method.\r\n',
45+
'TF':"Deep learning is a subset of machine learning, which is essentially a neural network with three or more layers.\n# These neural networks attempt to simulate the behavior of the human brain—albeit far from matching its ability—allowing it to “learn” from large amounts of data.\n# While a neural network with a single layer can still make approximate predictions,\n# additional hidden layers can help to optimize and refine for accuracy."
4546
},
4647
'Regression':{
4748
'OrthogonalMatchingPursuit':"OrthogonalMatchingPursuit and orthogonal_mp implements the OMP algorithm for approximating\n# the fit of a linear model with constraints imposed on the number of non-zero coefficients\n# OMP is based on a greedy algorithm that includes at each step the atom most highly\n# correlated with the current residual. It is similar to the simpler matching pursuit (MP) method, but better in that at each iteration, the residual is recomputed using an orthogonal projection on the space of the previously chosen dictionary elements.",
@@ -72,6 +73,7 @@ class PyComments:
7273
'HuberRegressor':"Linear regression model that is robust to outliers.\n# The Huber Regressor optimizes the squared loss for the samples\n# where |(y - X'w) / sigma| < epsilon and the absolute loss for the samples where |(y - X'w) / sigma| > epsilon, where w and sigma are parameters to be optimized.\n# The parameter sigma makes sure that if y is scaled up or down by a certain factor, one does not need to rescale epsilon to achieve the same robustness.\n# Note that this does not take into account the fact that the different features of X may be of different scales.\n# This makes sure that the loss function is not heavily influenced by the outliers while not completely ignoring their effect.\r\n",
7374
'ElasticNet':'Elastic Net first emerged as a result of critique on Lasso, whose variable selection can be too dependent on data and thus unstable.\n# The solution is to combine the penalties of Ridge regression and Lasso to get the best of both worlds.\r\n',
7475
'PoissonRegressor':"Poisson regression is a generalized linear model form of regression used to model count data and contingency tables.\n# It assumes the response variable or target variable Y has a Poisson distribution, and assumes the logarithm of its expected value can be modeled by a linear combination of unknown parameters.\n# It is sometimes known as a log-linear model, especially when used to model contingency tables.\r\n",
76+
'TF':"Deep learning is a subset of machine learning, which is essentially a neural network with three or more layers.\n# These neural networks attempt to simulate the behavior of the human brain—albeit far from matching its ability—allowing it to “learn” from large amounts of data.\n# While a neural network with a single layer can still make approximate predictions,\n# additional hidden layers can help to optimize and refine for accuracy."
7577
}
7678
}
7779

blobcity/code_gen/SourceCodes.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ class SourceCode:
8787
}
8888

8989
cor_matrix="f,ax = plt.subplots(figsize=(18, 18))\rmatrix = np.triu(X.corr())\rse.heatmap(X.corr(), annot=True, linewidths=.5, fmt= '.1f',ax=ax, mask=matrix)\rplt.show()\n"
90-
tf_model_load="model = tf.keras.models.load_model(./autoaimodel.h5)"
91-
tf_model_metric={
90+
tf_load="model = bc.load('PICKLE FILE PATH')\r#summary\rnn=model.model\rnn.summary()\nnn.fit(X_train,Y_train,epochs=10)"
91+
tf_metric={
9292
'Classification':{
93-
'binary':"y_pred=model.predict(Y_test)\ry_pred=np.round(y_pred)# Classification Report\rprint(classification_report(Y_test,y_pred))\r\n",
94-
'multi':"y_pred=model.predict(Y_test)\ry_pred=np.argmax(y_pred,axis=1)# Classification Report\rprint(classification_report(Y_test,y_pred))\r\n"
93+
'binary':"\ry_pred=nn.predict(X_test)\ry_pred=np.round(y_pred)# Classification Report\rprint(classification_report(Y_test,y_pred))\r\n",
94+
'multi':"\rnn=model.model\ry_pred=nn.predict(test_df)\ry_pred=np.argmax(y_pred,axis=1)# Classification Report\rprint(classification_report(Y_test,y_pred))\r\n"
9595
},
96-
'Regression':"# Metrics\r\ny_pred=model.predict(X_test)\rprint('R2 Score: {:.2f}'.format(r2_score(Y_test,y_pred)))\r"+\
96+
'Regression':"# Metrics\r\ntest_df = pd.DataFrame(X_test,columns = X.columns.to_list())\nnn=model.model\ry_pred=nn.predict(test_df)\rprint('R2 Score: {:.2f}'.format(r2_score(Y_test,y_pred)))\r"+\
9797
"print('Mean Absolute Error {:.2f}'.format(mean_absolute_error(Y_test,y_pred)))\r"+\
9898
"print('Mean Squared Error {:.2f}'.format(mean_squared_error(Y_test,y_pred)))"
9999
}
@@ -121,6 +121,7 @@ class SourceCode:
121121
'LinearDiscriminantAnalysis':'from sklearn.discriminant_analysis import LinearDiscriminantAnalysis\r\n',
122122
'PassiveAggressiveClassifier':'from sklearn.linear_model import PassiveAggressiveClassifier\r\n',
123123
'LGBMClassifier':'from lightgbm import LGBMClassifier\r\n',
124+
'TF':'import blobcity as bc\r\n'
124125
},
125126
'Regression':{
126127
'OrthogonalMatchingPursuit':'from sklearn.linear_model import OrthogonalMatchingPursuit\r\n',
@@ -151,6 +152,6 @@ class SourceCode:
151152
'HuberRegressor':'from sklearn.linear_model import HuberRegressor\r\n',
152153
'ElasticNet':'from sklearn.linear_model import ElasticNet\r\n',
153154
'PoissonRegressor':'from sklearn.linear_model import PoissonRegressor\r\n',
154-
'TF':'import tensorflow as tf\r\n'
155+
'TF':'import blobcity as bc\r\n'
155156
}
156157
}

blobcity/main/driver.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ def train(file=None, df=None, target=None,features=None,use_neural=False,accurac
3333
3434
param3: string: target/dependent column name.
3535
36-
param4: float: range[0.1,1.0]
36+
param4: boolean: whether to train tensorflow models
37+
38+
param5: float: range[0.1,1.0]
3739
3840
return: Model Class Object
3941
Performs a model search on the data proivded. A yaml file is generated once the best fit model configuration

0 commit comments

Comments
 (0)