-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
queries.py
1243 lines (1114 loc) · 51.7 KB
/
queries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from libra.query.nlp_queries import (image_caption_query,
generate_caption, classify_text,
text_classification_query, get_summary,
summarization_query, generate_text, get_ner)
from libra.query.classification_models import (k_means_clustering,
train_svm, nearest_neighbors,
decision_tree, train_xgboost)
from libra.query.supplementaries import tune_helper, get_model_data, get_operators, get_accuracy, get_losses, \
get_target, get_plots, get_vocab
from libra.query.feedforward_nn import (regression_ann,
classification_ann,
convolutional)
from libra.data_generation.grammartree import get_value_instruction
from libra.data_generation.dataset_labelmatcher import (get_similar_column,
get_similar_model)
from libra.query.generative_models import dcgan
from libra.plotting.generate_plots import analyze
from libra.query.recommender_systems import ContentBasedRecommender
from libra.dashboard.auto_eda import edaDashboard
from colorama import Fore, Style
import pandas as pd
from pandas.core.common import SettingWithCopyWarning
import warnings
import os
import nltk
import ssl
import numpy as np
from tkinter import filedialog
from tkinter import *
from tensorflow.keras.preprocessing.image import img_to_array
import tensorflow as tf
from matplotlib import pyplot as plt
# suppressing warnings for cleaner dialogue box
warnings.simplefilter(action='error', category=FutureWarning)
warnings.simplefilter(action="ignore", category=SettingWithCopyWarning)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
# function imports from other files
currLog = ""
counter = 0
# clears log when needed - currently not being used
def clearLog():
global currLog
global counter
currLog = ""
counter = 0
print("")
def logger(instruction, found=""):
'''
logging function that creates hierarchial display of the processes of
different functions. Copied into different python files to maintain
global variables.
:param instruction: what you want to be displayed
:param found: if you want to display something found like target column
'''
global counter
if counter == 0:
print((" " * 2 * counter) + str(instruction) + str(found))
elif instruction == "->":
counter = counter - 1
print(Fore.BLUE + (" " * 2 * counter) +
str(instruction) + str(found) + (Style.RESET_ALL))
else:
print((" " * 2 * counter) + "|- " + str(instruction) + str(found))
if instruction == "done...":
print("\n" + "\n")
counter += 1
def get_folder_dir(self):
dir_path = filedialog.askdirectory()
return dir_path
def get_file():
filename = filedialog.askopenfilename()
if os.path.isfile(filename):
return filename
else:
print('No file chosen')
class client:
'''
class to store all query information. Currently, old_models is not being used.
'''
def __init__(self, data):
'''
initializer for the client class, reads in dataset and records by calling logger function
:param data: represents the dataset that you're trying to read
:return: a completely initialized client class
'''
self.required_installations()
logger("Creating client object")
self.dataset = data
logger("Reading in dataset")
print("")
self.models = {}
self.latest_model = None
clearLog()
def required_installations(self):
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
nltk.download('punkt', quiet=True)
nltk.download('averaged_perceptron_tagger', quiet=True)
nltk.download('stopwords', quiet=True)
# param model_requested: string representation of the name of the model user seeks to retrieve
# returns models with a specific string - currently deprecated, should not be used.
def get_models(self, model_requested):
'''
returns models with a specific string - currently deprecated, should not be used.
:param model_requested: represents the name of the model from which you want to retrieve
:return: the model dictionary for your specific model
'''
logger("Getting model...")
return get_similar_model(model_requested, self.models.keys())
clearLog()
# recommend items based on search criteria(for recommender systems only)
def recommend(self, search_term):
if self.latest_model == 'content_recommender':
model = self.models[self.latest_model]
return model.recommend(search_term)
else:
pass
# param modelKey: string representation of the model to make prediction
# param data: dataframe version of desired prediction set
def predict(self, data, model=None):
'''
Uses a model from the self.models dictionary to make a prediction. Also fits it based on the operator stored in the models dictionary.
:param data: is the data that you want to predict for using model
:param model: is the specific model you want to use to predict
:return: a prediction, most likely an array
'''
if model is None:
model = self.latest_model
if model == 'text_classification':
map_func = np.vectorize(lambda x: self.classify_text(x))
predictions = map_func(data)
return predictions
else:
modeldict = self.models[model]
if modeldict.get('preprocessor'):
data = modeldict['preprocessor'].transform(data)
predictions = modeldict['model'].predict(data)
clearLog()
return self.interpret(model, predictions)
def interpret(self, model, predictions):
'''
Function to interpret predictions from a neural network for the creation for graphs / user understanding.
:param model: is the model in the self.models dictionary that you want to use to interpret
:param predictions: the predictions that come out of the model
:return: a prediction, most likely an array
'''
modeldict = self.models[model]
if modeldict.get('interpreter'):
predictions = modeldict['interpreter'].inverse_transform(
predictions)
clearLog()
return predictions
# determines type of solution based of type of problem posed by query using a feed-forward neural network
# instruction should be the value of a column
def neural_network_query(self,
instruction,
callback=False,
text=[],
ca_threshold=None,
drop=None,
preprocess=True,
test_size=0.2,
random_state=49,
epochs=50,
generate_plots=True,
callback_mode='min',
maximizer="val_loss",
save_model=False,
save_path=os.getcwd(),
add_layer={}):
'''
Detects to see if it's a regression/classification problem and then calls the correct query.
:param hyperparameters: all of these are hyperparameters that're passed to the algorithm
:return: a model, plots, accuracy information all stored in the self.models dictionary
'''
data = pd.read_csv(self.dataset)
if preprocess:
remove = get_similar_column(
get_value_instruction(instruction), data)
if len(data) < 50:
raise Exception(
"Only datasets larger then 50 rows are supported for neural networks")
if len(data[remove].value_counts()) <= 50:
callback_mode = 'max'
maximizer = "val_accuracy"
self.classification_query_ann(
instruction,
text=text,
callback=callback,
ca_threshold=ca_threshold,
preprocess=preprocess,
test_size=test_size,
random_state=random_state,
epochs=epochs,
generate_plots=generate_plots,
callback_mode=callback_mode,
maximizer=maximizer,
save_model=save_model,
save_path=save_path,
add_layer=add_layer)
else:
self.regression_query_ann(
instruction,
callback=callback,
text=text,
ca_threshold=ca_threshold,
preprocess=preprocess,
test_size=test_size,
random_state=random_state,
epochs=epochs,
generate_plots=generate_plots,
callback_mode=callback_mode,
maximizer=maximizer,
drop=drop,
save_model=save_model,
save_path=save_path,
add_layer=add_layer)
clearLog()
# single regression query using a feed-forward neural network
# instruction should be the value of a column
def regression_query_ann(
self,
instruction,
callback=False,
text=[],
drop=None,
ca_threshold=None,
preprocess=True,
test_size=0.2,
random_state=49,
epochs=50,
generate_plots=True,
callback_mode='min',
maximizer="val_loss",
save_model=True,
save_path=os.getcwd(),
add_layer={}):
'''
Calls the body of the regression_query__ code in the supplementaries.py file. Used for a regression feed forward neural network.
:param instruction: The objective that you want to model (str).
:param callback: Applying a set of functions/actions at various stages of training (bool).
:param ca_threshold: Threshold for multiple correspondence analysis (float).
:param dataset: The dataset being used in the regression feed forward neural network (str).
:param text: A list of columns to perform text embedding on.
:param drop: A list of the dataset's columns to drop.
:param preprocess: Preprocess the data (bool).
:param test_size: Size of the testing set (float).
:param random_state: Initialize a pseudo-random number generator (int).
:param epochs: Number of epochs (int).
:param generate_plots: Generate plots for the model (bool).
:param callback_mode: The type of callback (str).
:param maximizer: The accuracy/loss type to optimize (str).
:param save_model: Save the model (bool).
:param save_path: Filepath of where to save the model (str).
:return: a model and information to along with it stored in the self.models dictionary.
'''
self.models['regression_ANN'] = regression_ann(
instruction=instruction,
callback=False,
ca_threshold=.25 if ca_threshold is None else ca_threshold,
dataset=self.dataset,
text=text,
drop=drop,
preprocess=preprocess,
test_size=test_size,
random_state=random_state,
epochs=epochs,
generate_plots=generate_plots,
callback_mode=callback_mode,
maximizer=maximizer,
save_model=save_model,
save_path=save_path,
add_layer=add_layer)
self.latest_model = 'regression_ANN'
clearLog()
# query for multilabel classification query, does not work for
# binaryclassification, fits to feed-forward neural network
def classification_query_ann(
self,
instruction,
callback=False,
text=[],
ca_threshold=None,
preprocess=True,
callback_mode='min',
drop=None,
random_state=49,
test_size=0.2,
epochs=50,
generate_plots=True,
maximizer="val_loss",
save_model=False,
save_path=os.getcwd(),
add_layer={}):
'''
Calls the body of the classification code in the supplementaries.py file. Used for a classification feed forward neural network.
:param instruction: The objective that you want to model (str).
:param callback: Applying a set of functions/actions at various stages of training (bool).
:param dataset: The dataset being used in the classification feed forward neural network (str).
:param text: A list of columns to perform text embedding on.
:param ca_threshold: Threshold for multiple correspondence analysis (float).
:param drop: A list of the dataset's columns to drop.
:param preprocess: Preprocess the data (bool).
:param test_size: Size of the testing set (float).
:param random_state: Initialize a pseudo-random number generator (int).
:param epochs: Number of epochs (int).
:param generate_plots: Generate plots for the model (bool).
:param callback_mode: The type of callback (str).
:param maximizer: The accuracy/loss type to optimize (str).
:param save_model: Save the model (bool).
:param save_path: Filepath of where to save the model (str).
:return: a model and information to along with it stored in the self.models dictionary.
'''
self.models['classification_ANN'] = classification_ann(
instruction=instruction,
callback=callback,
dataset=self.dataset,
text=text,
ca_threshold=.25 if ca_threshold is None else ca_threshold,
drop=drop,
preprocess=preprocess,
test_size=test_size,
random_state=random_state,
epochs=epochs,
generate_plots=generate_plots,
callback_mode=callback_mode,
maximizer=maximizer,
save_model=save_model,
save_path=save_path,
add_layer=add_layer)
self.latest_model = 'classification_ANN'
clearLog()
# query to perform k-means clustering
def kmeans_clustering_query(self,
preprocess=True,
scatters=[],
generate_plots=True,
drop=None,
clusters=None,
base_clusters=2,
verbose=0,
n_init=10,
max_iter=300,
random_state=42,
text=[]
):
'''
Calls the body of the kmeans_clustering code in the supplementaries.py file. Can be used without any preprocessing and/or parameters.
:param dataset: The dataset being used in the k-means clustering algorithm (str).
:param scatters: A list of various types of scatter plots.
:param preprocess: Preprocess the data (bool).
:param generate_plots: Generate plots for the model (bool).
:param drop: A list of the dataset's columns to drop.
:param base_clusters: Number of clusters to generate (int).
:param verbose: Printing the logging information (int).
:param n_init: Number of times the function will run with different seeds (int).
:param max_iter: Maximum number of iterations the function will run (int).
:param random_state: Initialize a pseudo-random number generator (int).
:param text: A list of columns to perform text embedding on.
:return: a model and information to along with it stored in the self.models dictionary.
'''
self.models['k_means_clustering'] = k_means_clustering(
dataset=self.dataset,
scatters=scatters,
clusters=clusters,
preprocess=preprocess,
generate_plots=generate_plots,
drop=drop,
base_clusters=base_clusters,
verbose=verbose,
n_init=n_init,
max_iter=max_iter,
random_state=random_state,
text=text
)
self.latest_model = 'k_means_clustering'
clearLog()
# query to create a support vector machine
def svm_query(self,
instruction,
test_size=0.2,
text=[],
random_state=49,
kernel='linear',
preprocess=True,
drop=None,
cross_val_size=0.3,
degree=3,
gamma='scale',
coef0=0.0,
max_iter=-1
):
'''
Calls the body of the svm query code in the supplementaries.py file. Used to create a classification support vector machine.
:param dataset: The dataset being used in the classification support vector machine (str).
:param text: A list of columns to perform text embedding on.
:param random_state: Initialize a pseudo-random number generator (int).
:param test_size: Size of the testing set (float).
:param kernel: The type of kernel to be used (str).
:param preprocess: Preprocess the data (bool).
:param drop: A list of the dataset's columns to drop.
:param cross_val_size: Cross-Validation score (float).
:param degree: Degree of the polynomial kernel function (int).
:param gamma: Kernel coefficient (int).
:param coef0: Significant term in 'poly' and 'sigmoid' kernel functions (float).
:param max_iter: Maximum number of iterations the function will run (int).
:return: a model and information to go along with it stored in the self.models dictionary.
'''
self.models['svm'] = train_svm(instruction,
dataset=self.dataset,
text=text,
random_state=random_state,
test_size=test_size,
kernel=kernel,
preprocess=preprocess,
drop=drop,
cross_val_size=cross_val_size,
degree=degree,
gamma=gamma,
coef0=coef0,
max_iter=max_iter
)
self.latest_model = 'svm'
clearLog()
# query to create a nearest neighbors model
def nearest_neighbor_query(
self,
instruction=None,
text=[],
random_state=49,
test_size=0.2,
preprocess=True,
drop=None,
min_neighbors=3,
max_neighbors=10,
leaf_size=30,
p=2,
algorithm='auto'
):
'''
Calls the body of the nearest neighbor code in the supplementaries.py file. Used to create a nearest neighbor algorithm.
:param instruction: The objective that you want to model (str).
:param text: A list of columns to perform text embedding on.
:param random_state: Initialize a pseudo-random number generator (int).
:param test_size: Size of the testing set (float).
:param dataset: The dataset being used in the nearest neighbor algorithm (str).
:param preprocess: Preprocess the data (bool).
:param drop: A list of the dataset's columns to drop.
:param min_neighbors: Minimum number of neighbors (int).
:param max_neighbors: Maximum number of neighbors (int).
:param leaf_size: Leaf size passed to BallTree or KDTree (int).
:param p: Power parameter for the Minkowski metric (int).
:param algorithm: Algorithm used to compute the nearest neighbors (str).
:return: a model and information to along with it stored in the self.models dictionary.
'''
self.models['nearest_neighbor'] = nearest_neighbors(
instruction=instruction,
text=text,
random_state=random_state,
test_size=test_size,
dataset=self.dataset,
preprocess=preprocess,
drop=drop,
min_neighbors=min_neighbors,
max_neighbors=max_neighbors,
leaf_size=leaf_size,
p=p,
algorithm=algorithm
)
self.latest_model = 'nearest_neighbor'
clearLog()
# query to create a decision tree model
def decision_tree_query(
self,
instruction,
preprocess=True,
test_size=0.2,
text=[],
drop=None,
criterion='gini',
splitter='best',
max_depth=None,
min_samples_split=2,
min_samples_leaf=1,
min_weight_fraction_leaf=0.0,
max_leaf_nodes=None,
min_impurity_decrease=0.0,
ccp_alpha=0.0):
'''
Calls the body of the decision tree code in the classification_models.py file. Used to create a decision tree algorithm.
:param instruction: The objective that you want to model (str).
:param text: A list of columns to perform text embedding on.
:param dataset: The dataset being used in the decision tree algorithm (str).
:param preprocess: Preprocess the data (bool).
:param test_size: Size of the testing set (float).
:param drop: A list of the dataset's columns to drop.
:param criterion: The function to measure the quality of a split (str).
:param splitter: The technique used to choose each node's split (str).
:param max_depth: The maximum depth of the tree (int).
:param min_samples_split: The minimum number of samples a node must have to split (int).
:param min_samples_leaf: The minimum number of samples a leaf node must have (int).
:param min_weight_fraction_leaf: The fraction of the input samples required to be at a leaf node (float).
:param max_leaf_nodes: Maximum number of leaf nodes (int).
:param min_impurity_decrease: A node will be split if this split induces a decrease of the impurity greater than or equal
to this value (float).
:param ccp_alpha: Complexity parameter used for Minimal Cost-Complexity Pruning (float).
:return: a model and information to along with it stored in the self.models dictionary.
'''
self.models['decision_tree'] = decision_tree(
instruction=instruction,
text=text,
dataset=self.dataset,
preprocess=preprocess,
test_size=test_size,
drop=drop,
criterion=criterion,
splitter=splitter,
max_depth=max_depth,
min_samples_split=min_samples_split,
min_samples_leaf=min_samples_leaf,
min_weight_fraction_leaf=min_weight_fraction_leaf,
max_leaf_nodes=max_leaf_nodes,
min_impurity_decrease=min_impurity_decrease,
ccp_alpha=ccp_alpha)
self.latest_model = 'decision_tree'
clearLog()
def content_recommender_query(self, feature_names=[], n_recommendations=10, indexer='title'):
self.models['content_recommender'] = ContentBasedRecommender(
data=self.dataset,
feature_names=feature_names,
indexer=indexer)
self.latest_model = 'content_recommender'
clearLog()
# query to create a xgboost model
def xgboost_query(self,
instruction,
text=[],
preprocess=True,
test_size=0.2,
drop=None,
random_state=49,
learning_rate=0.1,
n_estimators=1000,
max_depth=6,
min_child_weight=1,
gamma=0,
subsample=0.8,
colsample_bytree=0.8,
verbosity=0,
objective='binary:logistic'):
'''
Calls the body of the xgboost code in the classification_models.py file. Used to create a xgboost algorithm.
:param instruction: The objective that you want to model (str).
:param text: A list of columns to perform text embedding on.
:param dataset: The dataset being used in the xgboost algorithm (str).
:param preprocess: Preprocess the data (bool).
:param test_size: Size of the testing set (float).
:param drop: A list of the dataset's columns to drop.
:param random_seed: Initialize a pseudo-random number generator (int).
:param learning_rate: Boosting learning rate(float).
:param n_estimators: Number of gradient boosted trees. Equivalent to number of boosting rounds(in ).
:param max_depth: Maximum tree depth for base learners(int).
:param min_child_weight: Minimum sum of instance weight(hessian) needed in a child(int).
:param gamma: Minimum loss reduction required to make a further partition on a leaf node of the tree(int).
:param subsample: Subsample ratio of the training instance(float).
:param colsample_bytree: Subsample ratio of columns when constructing each tree(float).
:param objective: Specify the learning task and the corresponding learning objective or a custom
objective function to be used (string or callable).
:param scale_pos_weight: Balancing of positive and negative weights(float).
:param verbose: Verbosity of printing messages. Valid values are 0 (silent), 1 (warning), 2 (info), 3 (debug).
:return: a model and information to along with it stored in the self.models dictionary.
'''
self.models['xgboost'] = train_xgboost(instruction,
dataset=self.dataset,
text=[],
random_state=random_state,
preprocess=preprocess,
drop=drop,
learning_rate=learning_rate,
n_estimators=n_estimators,
max_depth=max_depth,
min_child_weight=min_child_weight,
gamma=gamma,
subsample=subsample,
verbosity=verbosity,
colsample_bytree=colsample_bytree,
objective=objective)
self.latest_model = 'xgboost'
clearLog()
# tunes a specific neural network based on the input model_to_tune
def tune(self,
model_to_tune=None,
max_layers=10,
min_layers=2,
min_dense=32,
max_dense=512,
executions_per_trial=3,
max_trials=1,
generate_plots=True,
activation='relu',
loss='categorical_crossentropy',
metrics='accuracy',
patience=1,
epochs=10,
objective='val_accuracy',
seed=42,
directory='my_dir',
verbose=0,
test_size=0.2
):
'''
Calls the body of the tune identifier which is located in the supplementaries.py which then calls the appropriate tuner depending on the model
:param model_to_tune: The model to tune.
:param patience: Number of epochs with no improvement after which training will be stopped (int).
:param dataset: The dataset being used in the tuner (str).
:param models: The model dictionary (dict).
:param generate_plots: Generate plots for the model (bool).
:param max_layers: Maximum number of layers (int).
:param min_layers: Minimum number of layers (int).
:param min_dense: Minimum kernel density (int).
:param max_dense: Maximum kernel density (int).
:param executions_per_trial: Number of executions per trial (int).
:param max_trials: Maximum number of trials
:param activation: Activation Function (str).
:param loss: Loss Function (str).
:param metrics: Type of metrics function (str).
:param epochs: Number of epochs (int).
:param objective: Name of model metric to maximize/minimize (str).
:param seed: Random seed (int).
:param directory: Path to the directory (str).
:param verbose: Printing the logging information (int).
:param test_size: Size of the testing set (float).
:return: an updated model and history stored in the models dictionary
'''
if model_to_tune is None:
model_to_tune = self.latest_model
self.models = tune_helper(
model_to_tune=model_to_tune,
patience=patience,
dataset=self.dataset,
models=self.models,
generate_plots=generate_plots,
max_layers=max_layers,
min_layers=min_layers,
min_dense=min_dense,
max_dense=max_dense,
executions_per_trial=executions_per_trial,
max_trials=max_trials,
activation=activation,
loss=loss,
metrics=metrics,
epochs=epochs,
objective=objective,
seed=seed,
directory=directory,
verbose=verbose,
test_size=test_size
)
clearLog()
# query to build a convolutional neural network
def convolutional_query(self,
instruction=None,
read_mode=None,
verbose=0,
preprocess=True,
data_path=None,
new_folders=True,
image_column=None,
test_size=0.2,
fine_tune=None,
augmentation=True,
custom_arch=None,
pretrained=None,
epochs=10,
height=None,
width=None,
show_feature_map=False,
save_as_tfjs=None,
save_as_tflite=None,
generate_plots=None):
'''
Calls the body of the convolutional neural network query which is located in the feedforward.py file
:param instruction: The objective that you want to model (str).
:param read_mode: The type of dataset (str).
:param verbose: Printing the logging information (int).
:param preprocess: Preprocess the data (bool).
:param data_path: Path to the dataset (str).
:param new_folders: Create new folders for the image during preprocessing (bool).
:param image_column: The column in the csv file where the filepaths for the images exist (str).
:param test_size: Ratio of dataset allotted to the testing data (float).
:param augmentation: Perform image data augmentation (bool).
:param epochs: Number of epochs (int).
:param height: Height of the input image (int).
:param width: Width of the input image (int).
:param show_feature_map: Displays feature map graphic (bool).
:return: an updated model and history stored in the models dictionary
'''
# storing values in the model dictionary
self.models["convolutional_NN"] = convolutional(
instruction=instruction,
read_mode=read_mode,
verbose=verbose,
preprocess=preprocess,
data_path=self.dataset,
new_folders=new_folders,
image_column=image_column,
training_ratio=1 - test_size,
fine_tune=fine_tune,
augmentation=augmentation,
custom_arch=custom_arch,
pretrained=pretrained,
epochs=epochs,
height=height,
width=width,
save_as_tfjs=save_as_tfjs,
save_as_tflite=save_as_tflite,
generate_plots=generate_plots)
if show_feature_map:
model = self.models["convolutional_NN"]["model"]
X_test = self.models["convolutional_NN"]["data"]["test"]
# Get first image in test images and format it
img = X_test[0][0]
img /= 255
successive_outputs = [layer.output for layer in model.layers[1:]]
visualization_model = tf.keras.models.Model(inputs=model.input, outputs=successive_outputs)
successive_feature_maps = visualization_model.predict(img)
# Add main title to figure
firstPlot = True
# Include names of layers in plot
layer_names = [layer.name for layer in model.layers]
for layer_name, feature_map in zip(layer_names, successive_feature_maps):
if len(feature_map.shape) == 4:
# Plot Feature maps for the conv / maxpool layers, not the fully-connected layers
n_features = feature_map.shape[-1] # number of features in the feature map
height = feature_map.shape[1] # feature map shape (1, size, size, n_features)
width = feature_map.shape[2]
display_grid = np.zeros((height, width * n_features))
# Format features appropriately
for i in range(n_features):
img = feature_map[0, :, :, i]
img -= img.mean()
img /= img.std()
img *= 64
img += 128
img = np.clip(img, 0, 255).astype('uint8')
# Tile each filter into a horizontal grid
display_grid[:, i * width: (i + 1) * width] = img
# Display the grid
scale = 20. / n_features
plt.figure(figsize=(scale * n_features, scale))
if firstPlot:
plt.title(f'Network Visualization\n\n{layer_name}')
firstPlot = False
else:
plt.title(layer_name)
plt.grid(False)
plt.imshow(display_grid, aspect='auto', cmap='viridis')
plt.show()
self.latest_model = 'convolutional_NN'
clearLog()
def gan_query(self,
instruction=None,
type='dcgan',
num_images=3,
preprocess=True,
data_path=None,
verbose=0,
epochs=10,
height=None,
width=None,
output_path=None):
if type == 'dcgan':
self.models["DCGAN"] = dcgan(instruction=instruction,
num_images=num_images,
preprocess=preprocess,
data_path=self.dataset,
verbose=verbose,
epochs=epochs,
height=height,
width=width,
output_path=output_path
)
self.latest_model = 'DCGAN'
clearLog()
# sentiment analysis prediction wrapper
def classify_text(self, text):
"""
Calls the body of the text_classification neural network query which is located in the nlp_queries.py file. This can only be called
if text_classification_query has been called previously.
:param text: The new text that you want to classify (str).
:return: a classification of text that you've provided
"""
clearLog()
return classify_text(self=self, text=text)
# sentiment analysis query
def text_classification_query(self, instruction, label_column=None, drop=None,
preprocess=True,
test_size=0.2,
random_state=49,
learning_rate=1e-2,
epochs=5,
monitor="val_loss",
batch_size=32,
max_text_length=20,
generate_plots=True,
save_model=False,
save_path=os.getcwd()):
'''
Calls the body of the text_classification query which is located in the nlp_queries.py file
:param instruction: The objective that you want to model (str).
:param drop: A list of the dataset's columns to drop.
:param preprocess: Preprocess the data (bool).
:param test_size: Size of the testing set (float).
:param random_state: Initialize a pseudo-random number generator (int).
:param learning_rate: The learning rate of the model (float).
:param epochs: Number of epochs (int).
:param batch_size: The batch size for the dataset (int).
:param generate_plots: Generate plots for the model (bool).
:param save_model: Save the model (bool).
:param save_path: Filepath of where to save the model (str).
:return: an updated model and history stored in the models dictionary
'''
# storing values the model dictionary
self.models["text_classification"] = text_classification_query(
self=self, instruction=instruction, label_column=label_column, drop=drop,
preprocess=preprocess,
test_size=test_size,
random_state=random_state,
learning_rate=learning_rate,
monitor=monitor,
epochs=epochs,
batch_size=batch_size,
max_text_length=max_text_length,
generate_plots=generate_plots,
save_model=save_model,
save_path=save_path)
self.latest_model = 'text_classification'
clearLog()
# summarization predict wrapper
def get_summary(self, text, num_beams=4, no_repeat_ngram_size=2, num_return_sequences=1,
early_stopping=True):
'''
Calls the body of the summarizer which is located in the nlp_queries.py file
:param text: set of text that you want to summarize (str).
:param max_summary_length: Max generated summary length (int).
:param early_stopping: Sets early stopping (bool).
:param num_return_sequences: Sets the number of likely possibilities to output (int).
:param no_repeat_ngram_size: Sets the number of unrepeated consecutive n-grams (int).
:param num_beams: Sets number of possibilities to explore in beam search (int).
:return: a summary of text inputted in the text field.
'''
clearLog()
return get_summary(self=self, text=text, num_beams=num_beams, no_repeat_ngram_size=no_repeat_ngram_size
, num_return_sequences=num_return_sequences, early_stopping=early_stopping)
# summarization query
def summarization_query(self, instruction, label_column=None, preprocess=True,
drop=None,
epochs=5,
batch_size=32,
learning_rate=3e-5,
max_text_length=512,
test_size=0.2,
gpu=False,
random_state=49,
generate_plots=True,
save_model=False,
save_path=os.getcwd()):
'''
Calls the body of the summarization query which is located in the nlp_queries.py file
:param instruction: The objective that you want to model (str).
:param preprocess: Preprocess the data (bool).
:param drop: A list of the dataset's columns to drop.
:param epochs: Number of epochs (int).
:param batch_size: The batch size for the dataset (int).
:param learning_rate: The learning rate of the model (float).
:param max_text_length: The maximum length of the string of text (int).
:param test_size: Size of the testing set (float).
:param gpu: Use gpu for accelerated training (bool).
:param random_state: Initialize a pseudo-random number generator (int).
:param generate_plots: Generate plots for the model (bool).
:param save_model: Save the model (bool).
:param save_path: Filepath of where to save the model (str).
:return: an updated model and history stored in the models dictionary
'''
self.models["summarization"] = summarization_query(
self=self, instruction=instruction, preprocess=preprocess, label_column=label_column,
drop=drop,
epochs=epochs,
batch_size=batch_size,
learning_rate=learning_rate,
max_text_length=max_text_length,