-
Notifications
You must be signed in to change notification settings - Fork 269
/
utils.html
2609 lines (2428 loc) · 97.4 KB
/
utils.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<meta name="generator" content="pdoc 0.10.0" />
<title>ktrain.utils API documentation</title>
<meta name="description" content="" />
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity="sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin>
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity="sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin>
<link rel="stylesheet preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin>
<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity="sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin></script>
<script>window.addEventListener('DOMContentLoaded', () => hljs.initHighlighting())</script>
</head>
<body>
<main>
<article id="content">
<header>
<h1 class="title">Module <code>ktrain.utils</code></h1>
</header>
<section id="section-intro">
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">from .imports import *
# ------------------------------------------------------------------------------
# KTRAIN DEFAULTS
# ------------------------------------------------------------------------------
DEFAULT_WD = 0.01
def get_default_optimizer(lr=0.001, wd=DEFAULT_WD):
from .lroptimize.optimization import AdamWeightDecay
opt = AdamWeightDecay(
learning_rate=lr,
weight_decay_rate=wd,
beta_1=0.9,
beta_2=0.999,
epsilon=1e-6,
exclude_from_weight_decay=["layer_norm", "bias"],
)
return opt
# Use vanilla Adam as default unless weight decay is explicitly set by user
# in which case AdamWeightDecay is default optimizer.
# See core.Learner.set_weight_decay for more information
# dep_fix
if "tensorflow" in sys.modules:
DEFAULT_OPT = (
"adam"
if version.parse(tf.__version__) < version.parse("2.11")
else tf.keras.optimizers.legacy.Adam()
)
else:
DEFAULT_OPT = "adam"
DEFAULT_BS = 32
DEFAULT_ES = 5
DEFAULT_ROP = 2
# from .lroptimize.optimization import AdamWeightDecay
# DEFAULT_OPT = AdamWeightDecay(learning_rate=0.001,
# weight_decay_rate=0.01,
# beta_1=0.9,
# beta_2=0.999,
# epsilon=1e-6,
# exclude_from_weight_decay=['layer_norm', 'bias'])
DEFAULT_TRANSFORMER_LAYERS = [-2] # second-to-last hidden state
DEFAULT_TRANSFORMER_MAXLEN = 512
DEFAULT_TRANSFORMER_NUM_SPECIAL = 2
MODEL_BASENAME = "tf_model"
MODEL_NAME = MODEL_BASENAME + ".h5"
PREPROC_NAME = MODEL_BASENAME + ".preproc"
# ------------------------------------------------------------------------------
# DATA/MODEL INSPECTORS
# ------------------------------------------------------------------------------
def is_ktrain_dataset(data):
from .dataset import Dataset
return isinstance(data, Dataset)
def loss_fn_from_model(model):
# dep_fix
if version.parse(tf.__version__) < version.parse("2.2") or DISABLE_V2_BEHAVIOR:
return model.loss_functions[0].fn
else: # TF 2.2.0
return model.compiled_loss._get_loss_object(
model.compiled_loss._losses[0].name
).fn
def metrics_from_model(model):
msg = "Could not retrieve metrics list from compiled model"
# dep_fix
if version.parse(tf.__version__) < version.parse("2.2") or DISABLE_V2_BEHAVIOR:
return model._compile_metrics
# return [m.name for m in model.metrics] if is_tf_keras() else model.metrics
else: # TF >= 2.2.0
mlist = model.compiled_metrics._metrics
if isinstance(mlist, list) and isinstance(
mlist[0], str
): # metrics are strings prior to training
return mlist
elif isinstance(mlist, list) and isinstance(mlist[0], list):
try:
return [m.name for m in mlist[0]]
except:
warnings.warn(msg)
return []
elif isinstance(mlist, list) and hasattr(
mlist[0], "name"
): # tf.keras.metrics.AUC()
try:
return [m.name for m in mlist]
except:
warnings.warn(msg)
return []
else:
warnings.warn(msg)
return []
def is_classifier(model):
"""
checks for classification and mutlilabel from model
"""
is_classifier = False
is_multilabel = False
# get loss name
loss = model.loss
if callable(loss):
if hasattr(loss, "__name__"):
loss = loss.__name__
elif hasattr(loss, "name"):
loss = loss.name
else:
raise Exception("could not get loss name")
# check for classification
if loss in [
"categorical_crossentropy",
"sparse_categorical_crossentropy",
"binary_crossentropy",
]:
is_classifier = True
else:
mlist = metrics_from_model(model)
if isinstance(mlist, (list, np.ndarray)) and any(
["accuracy" in m for m in mlist]
):
is_classifier = True
elif isinstance(mlist, (list, np.ndarray)) and any(["auc" in m for m in mlist]):
is_classifier = True
# check for multilabel
if loss == "binary_crossentropy":
if is_huggingface(model=model):
is_multilabel = True
else:
last = model.layers[-1]
output_shape = last.output_shape
mult_output = (
True if len(output_shape) == 2 and output_shape[1] > 1 else False
)
if (
(
hasattr(last, "activation")
and isinstance(last.activation, type(keras.activations.sigmoid))
)
or isinstance(last, type(keras.activations.sigmoid))
) and mult_output:
is_multilabel = True
return (is_classifier, is_multilabel)
def is_tabular_from_data(data):
return type(data).__name__ in ["TabularDataset"]
def is_huggingface(model=None, data=None):
"""
check for hugging face transformer model
from model and/or data
"""
huggingface = False
if model is not None and is_huggingface_from_model(model):
huggingface = True
elif data is not None and is_huggingface_from_data(data):
huggingface = True
return huggingface
def is_huggingface_from_model(model):
# 20201202: support both transformers<4.0 and transformers>=4.0
return "transformers.modeling_tf" in str(
type(model)
) or "transformers.models" in str(type(model))
def is_huggingface_from_data(data):
return type(data).__name__ in ["TransformerDataset"]
def is_ner(model=None, data=None):
ner = False
if data is None:
warnings.warn("is_ner only detects CRF-based NER models when data is None")
if model is not None and is_crf(model):
ner = True
elif data is not None and is_ner_from_data(data):
ner = True
return ner
def is_crf(model):
"""
checks for CRF sequence tagger.
"""
# loss = model.loss
# if callable(loss):
# if hasattr(loss, '__name__'):
# loss = loss.__name__
# elif hasattr(loss, 'name'):
# loss = loss.name
# else:
# raise Exception('could not get loss name')
# return loss == 'crf_loss' or 'CRF.loss_function' in str(model.loss)
return type(model.layers[-1]).__name__ == "CRF"
# def is_ner_from_model(model):
# """
# checks for sequence tagger.
# Curently, only checks for a CRF-based sequence tagger
# """
# loss = model.loss
# if callable(loss):
# if hasattr(loss, '__name__'):
# loss = loss.__name__
# elif hasattr(loss, 'name'):
# loss = loss.name
# else:
# raise Exception('could not get loss name')
# return loss == 'crf_loss' or 'CRF.loss_function' in str(model.loss)
def is_ner_from_data(data):
return type(data).__name__ == "NERSequence"
def is_nodeclass(model=None, data=None):
result = False
if data is not None and type(data).__name__ == "NodeSequenceWrapper":
result = True
return result
def is_linkpred(model=None, data=None):
result = False
if data is not None and type(data).__name__ == "LinkSequenceWrapper":
result = True
return result
def is_imageclass_from_data(data):
return type(data).__name__ in [
"DirectoryIterator",
"DataFrameIterator",
"NumpyArrayIterator",
]
def is_regression_from_data(data):
"""
checks for regression task from data
"""
data_arg_check(val_data=data, val_required=True)
if is_ner(data=data):
return False # NERSequence
elif is_nodeclass(data=data):
return False # NodeSequenceWrapper
elif is_linkpred(data=data):
return False # LinkSequenceWrapper
Y = y_from_data(data)
if len(Y.shape) == 1 or (len(Y.shape) > 1 and Y.shape[1] == 1):
return True
return False
def is_multilabel(data):
"""
checks for multilabel from data
"""
data_arg_check(val_data=data, val_required=True)
if is_ner(data=data):
return False # NERSequence
elif is_nodeclass(data=data):
return False # NodeSequenceWrapper
elif is_linkpred(data=data):
return False # LinkSequenceWrapper
multilabel = False
Y = y_from_data(data)
if len(Y.shape) == 1 or (len(Y.shape) > 1 and Y.shape[1] == 1):
return False
for idx, y in enumerate(Y):
if idx >= 1024:
break
if np.issubdtype(type(y), np.integer) or np.issubdtype(type(y), np.floating):
return False
total_for_example = sum(y)
if total_for_example > 1:
multilabel = True
break
return multilabel
def shape_from_data(data):
err_msg = "could not determine shape from %s" % (type(data))
if is_iter(data):
if is_ktrain_dataset(data):
return data.xshape()
elif hasattr(data, "image_shape"):
return data.image_shape # DirectoryIterator/DataFrameIterator
elif hasattr(data, "x"): # NumpyIterator
return data.x.shape[1:]
else:
try:
return data[0][0].shape[1:]
except:
raise Exception(err_msg)
else:
try:
if type(data[0]) == list: # BERT-style tuple
return data[0][0].shape
else:
return data[0].shape # standard tuple
except:
raise Exception(err_msg)
def ondisk(data):
if hasattr(data, "ondisk"):
return data.ondisk()
ondisk = is_iter(data) and (type(data).__name__ not in ["NumpyArrayIterator"])
return ondisk
def nsamples_from_data(data):
err_msg = "could not determine number of samples from %s" % (type(data))
if is_iter(data):
if is_ktrain_dataset(data):
return data.nsamples()
elif hasattr(data, "samples"): # DirectoryIterator/DataFrameIterator
return data.samples
elif hasattr(data, "n"): # DirectoryIterator/DataFrameIterator/NumpyIterator
return data.n
else:
raise Exception(err_msg)
else:
try:
if type(data[0]) == list: # BERT-style tuple
return len(data[0][0])
else:
return len(data[0]) # standard tuple
except:
raise Exception(err_msg)
def nclasses_from_data(data):
if is_iter(data):
if is_ktrain_dataset(data):
return data.nclasses()
elif hasattr(data, "classes"): # DirectoryIterator
return len(set(data.classes))
else:
try:
return data[0][1].shape[1] # DataFrameIterator/NumpyIterator
except:
raise Exception(
"could not determine number of classes from %s" % (type(data))
)
else:
try:
return data[1].shape[1]
except:
raise Exception(
"could not determine number of classes from %s" % (type(data))
)
def y_from_data(data):
if is_iter(data):
if is_ktrain_dataset(data):
return data.get_y()
elif hasattr(data, "classes"): # DirectoryIterator
return keras.utils.to_categorical(data.classes)
elif hasattr(data, "labels"): # DataFrameIterator
return data.labels
elif hasattr(data, "y"): # NumpyArrayIterator
# return to_categorical(data.y)
return data.y
else:
raise Exception(
"could not determine number of classes from %s" % (type(data))
)
else:
try:
return data[1]
except:
raise Exception(
"could not determine number of classes from %s" % (type(data))
)
def is_iter(data, ignore=False):
if ignore:
return True
iter_classes = ["NumpyArrayIterator", "DirectoryIterator", "DataFrameIterator"]
return data.__class__.__name__ in iter_classes or is_ktrain_dataset(data)
def data_arg_check(
train_data=None,
val_data=None,
train_required=False,
val_required=False,
ndarray_only=False,
):
if train_required and train_data is None:
raise ValueError("train_data is required")
if val_required and val_data is None:
raise ValueError("val_data is required")
if train_data is not None and not is_iter(train_data, ndarray_only):
if bad_data_tuple(train_data):
err_msg = "data must be tuple of numpy.ndarrays"
if not ndarray_only:
err_msg += " or an instance of ktrain.Dataset"
raise ValueError(err_msg)
if val_data is not None and not is_iter(val_data, ndarray_only):
if bad_data_tuple(val_data):
err_msg = "data must be tuple of numpy.ndarrays or BERT-style tuple"
if not ndarray_only:
err_msg += " or an instance of Iterator"
raise ValueError(err_msg)
return
def bert_data_tuple(data):
"""
checks if data tuple is BERT-style format
"""
if is_iter(data):
return False
if (
type(data[0]) == list
and len(data[0]) == 2
and type(data[0][0]) is np.ndarray
and type(data[0][1]) is np.ndarray
and type(data[1]) is np.ndarray
and np.count_nonzero(data[0][1]) == 0
):
return True
else:
return False
def bad_data_tuple(data):
"""
Checks for standard tuple or BERT-style tuple
"""
if (
not isinstance(data, tuple)
or len(data) != 2
or type(data[0]) not in [np.ndarray, list]
or (type(data[0]) in [list] and type(data[0][0]) is not np.ndarray)
or type(data[1]) is not np.ndarray
):
return True
else:
return False
# ------------------------------------------------------------------------------
# PLOTTING UTILITIES
# ------------------------------------------------------------------------------
# plots images with labels within jupyter notebook
def plots(ims, figsize=(12, 6), rows=1, interp=False, titles=None):
# if type(ims[0]) is np.ndarray:
# ims = np.array(ims).astype(np.uint8)
# if (ims.shape[-1] != 3):
# ims = ims.transpose((0,2,3,1))
f = plt.figure(figsize=figsize)
cols = len(ims) // rows if len(ims) % 2 == 0 else len(ims) // rows + 1
for i in range(len(ims)):
sp = f.add_subplot(rows, cols, i + 1)
sp.axis("Off")
if titles is not None:
sp.set_title(titles[i], fontsize=16)
plt.imshow(ims[i], interpolation=None if interp else "none")
def plot_confusion_matrix(
cm, classes, normalize=False, title="Confusion matrix", cmap=plt.cm.Blues
):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(cm, interpolation="nearest", cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype("float") / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print("Confusion matrix, without normalization")
print(cm)
thresh = cm.max() / 2.0
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(
j,
i,
cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black",
)
plt.tight_layout()
plt.ylabel("True label")
plt.xlabel("Predicted label")
# ------------------------------------------------------------------------------
# DOWNLOAD UTILITIES
# ------------------------------------------------------------------------------
def download(url, filename):
with open(filename, "wb") as f:
response = requests.get(url, stream=True, verify=False)
total = response.headers.get("content-length")
if total is None:
f.write(response.content)
else:
downloaded = 0
total = int(total)
# print(total)
for data in response.iter_content(
chunk_size=max(int(total / 1000), 1024 * 1024)
):
downloaded += len(data)
f.write(data)
done = int(50 * downloaded / total)
sys.stdout.write("\r[{}{}]".format("█" * done, "." * (50 - done)))
sys.stdout.flush()
def get_ktrain_data():
home = os.path.expanduser("~")
ktrain_data = os.path.join(home, "ktrain_data")
if not os.path.isdir(ktrain_data):
os.mkdir(ktrain_data)
return ktrain_data
# ------------------------------------------------------------------------------
# MISC UTILITIES
# ------------------------------------------------------------------------------
from subprocess import Popen
def checkjava(path=None):
"""
Checks if a Java executable is available for Tika.
Args:
path(str): path to java executable
Returns:
True if Java is available, False otherwise
"""
# Get path to java executable if path not set
if not path:
path = os.getenv("TIKA_JAVA", "java")
# Check if java binary is available on path
try:
_ = Popen(path, stdout=open(os.devnull, "w"), stderr=open(os.devnull, "w"))
except:
return False
return True
def batchify(X, size):
"""
```
Splits X into separate batch sizes specified by size.
Args:
X(list): elements
size(int): batch size
Returns:
list of evenly sized batches with the last batch having the remaining elements
```
"""
return [X[x : x + size] for x in range(0, len(X), size)]
def list2chunks(a, n):
k, m = divmod(len(a), n)
return (a[i * k + min(i, m) : (i + 1) * k + min(i + 1, m)] for i in range(n))
def check_array(X, y=None, X_name="X", y_name="targets"):
if not isinstance(X, (list, np.ndarray)):
raise ValueError("%s must be a list or NumPy array" % X_name)
if y is not None and not isinstance(y, (list, np.ndarray)):
raise ValueError("%s must be a list or NumPy array" % y_name)
return
def is_tf_keras():
if keras.__name__ == "keras":
is_tf_keras = False
elif (
keras.__name__
in ["tensorflow.keras", "tensorflow.python.keras", "tensorflow_core.keras"]
or keras.__version__[-3:] == "-tf"
):
is_tf_keras = True
else:
raise KeyError("Cannot detect if using keras or tf.keras.")
return is_tf_keras
def vprint(s=None, verbose=1):
if not s:
s = "\n"
if verbose:
print(s)
def add_headers_to_df(fname_in, header_dict, fname_out=None):
df = pd.read_csv(fname_in, header=None)
df.rename(columns=header_dict, inplace=True)
if fname_out is None:
name, ext = os.path.splitext(fname_in)
name += "-headers"
fname_out = name + "." + ext
df.to_csv(fname_out, index=False) # save to new csv file
return
def get_random_colors(n, name="hsv", hex_format=True):
"""Returns a function that maps each index in 0, 1, ..., n-1 to a distinct
RGB color; the keyword argument name must be a standard mpl colormap name."""
cmap = plt.cm.get_cmap(name, n)
result = []
for i in range(n):
color = cmap(i)
if hex_format:
color = rgb2hex(color)
result.append(color)
return np.array(result)
def get_hf_model_name(model_id):
parts = model_id.split("/")
if len(parts) == 1:
model_id = parts[0]
else:
model_id = "/".join(parts[1:])
if model_id.startswith("xlm-roberta"):
model_name = "xlm-roberta"
else:
model_name = model_id.split("-")[0]
return model_name
# ------------------------------------------------------------------------------
# target-handling
# ------------------------------------------------------------------------------
class YTransform:
def __init__(self, class_names=[], label_encoder=None):
"""
```
Cheks and transforms array of targets. Targets are transformed in place.
Args:
class_names(list): labels associated with targets (e.g., ['negative', 'positive'])
Only used/required if:
1. targets are one/multi-hot-encoded
2. targets are integers and represent class IDs for classification task
Not required if:
1. targets are numeric and task is regression
2. targets are strings and task is classification (class_names are populated automatically)
label_encoder(LabelEncoder): a prior instance of LabelEncoder.
If None, will be created when train=True
```
"""
if type(class_names) != list:
if isinstance(class_names, (pd.Series, np.ndarray)):
class_names = class_names.tolist()
else:
raise ValueError("class_names must be list")
self.c = class_names
self.le = label_encoder
self.train_called = False
def get_classes(self):
return self.c
def set_classes(self, class_names):
self.c = (
class_names.tolist() if isinstance(class_names, np.ndarray) else class_names
)
def apply(self, targets, train=True):
if targets is None and train:
raise ValueError("targets is None")
elif targets is None and not train:
return
# validate labels against data
targets = np.array(targets) if type(targets) == list else targets
if len(targets.shape) > 1 and targets.shape[1] == 1:
targets = np.squeeze(targets, axis=1)
# handle numeric targets (regression)
if len(targets.shape) == 1 and not isinstance(targets[0], str):
# numeric targets
if not self.get_classes() and train:
warnings.warn(
"Task is being treated as REGRESSION because "
+ "either class_names argument was not supplied or is_regression=True. "
+ "If this is incorrect, change accordingly."
)
if not self.get_classes():
targets = np.array(targets, dtype=np.float32)
# string targets (classification)
elif len(targets.shape) == 1 and isinstance(targets[0], str):
if not train and self.le is None:
raise ValueError(
"LabelEncoder has not been trained. Call with train=True"
)
if train:
self.le = LabelEncoder()
self.le.fit(targets)
if self.get_classes():
warnings.warn(
"class_names argument was ignored, as they were extracted from string labels in dataset"
)
self.set_classes(self.le.classes_)
targets = self.le.transform(
targets
) # convert to numerical targets for classfication
# handle categorical targets (classification)
elif len(targets.shape) > 1:
if not self.get_classes():
raise ValueError(
"targets are 1-hot or multi-hot encoded but class_names is empty. "
+ "The classes argument should have been supplied."
)
else:
if train and len(self.get_classes()) != targets.shape[1]:
raise ValueError(
"training targets suggest %s classes, but class_names are %s"
% (targets.shape[1], self.get_classes())
)
# numeric targets (classification)
if len(targets.shape) == 1 and self.get_classes():
if np.issubdtype(type(max(targets)), np.floating):
warnings.warn(
"class_names implies classification but targets array contains float(s) instead of integers or strings"
)
if train and (len(set(targets)) != int(max(targets) + 1)):
raise ValueError(
"len(set(targets) is %s but max(targets)+1 is %s"
% (len(set(targets)), int(max(targets) + 1))
)
targets = keras.utils.to_categorical(
targets, num_classes=len(self.get_classes())
)
if train:
self.train_called = True
return targets
def apply_train(self, targets):
return self.apply(targets, train=True)
def apply_test(self, targets):
return self.apply(targets, train=False)
class YTransformDataFrame(YTransform):
def __init__(self, label_columns=[], is_regression=False):
"""
```
Checks and transforms label columns in DataFrame. DataFrame is modified in place
Args:
label_columns(list): list of columns storing labels
is_regression(bool): If True, task is regression and integer targets are treated as numeric dependent variable.
IF False, task is classification and integer targets are treated as class IDs.
```
"""
self.is_regression = is_regression
if isinstance(label_columns, str):
label_columns = [label_columns]
self.label_columns = label_columns
if not label_columns:
raise ValueError("label_columns is required")
self.label_columns = (
[self.label_columns]
if isinstance(self.label_columns, str)
else self.label_columns
)
# class_names = label_columns if len(label_columns) > 1 else []
super().__init__(class_names=[])
def get_label_columns(self, squeeze=True):
"""
Returns label columns of transformed DataFrame
"""
if not self.train_called:
raise Exception("apply_train should be called first")
if not self.is_regression:
new_lab_cols = self.c
else:
new_lab_cols = self.label_columns
return new_lab_cols[0] if len(new_lab_cols) == 1 and squeeze else new_lab_cols
def apply(self, df, train=True):
df = (
df.copy()
) # dep_fix: SettingWithCopy - prevent original DataFrame from losing old label columns
labels_exist = True
lst = self.label_columns[:]
if not all(x in df.columns.values for x in lst):
labels_exist = False
if train and not labels_exist:
raise ValueError(
"dataframe is missing label columns: %s" % (self.label_columns)
)
# extract targets
# todo: sort?
if len(self.label_columns) > 1:
if train and self.is_regression:
warnings.warn(
"is_regression=True was supplied but ignored because multiple label columns imply classification"
)
cols = df.columns.values
missing_cols = []
for l in self.label_columns:
if l not in df.columns.values:
missing_cols.append(l)
if len(missing_cols) > 0:
raise ValueError(
"These label_columns do not exist in df: %s" % (missing_cols)
)
# set targets
targets = (
df[self.label_columns].values
if labels_exist
else np.zeros((df.shape[0], len(self.label_columns)))
)
# set class names
if train:
self.set_classes(self.label_columns)
# single column
else:
# set targets
targets = (
df[self.label_columns[0]].values
if labels_exist
else np.zeros(df.shape[0], dtype=np.int_)
)
if self.is_regression and isinstance(targets[0], str):
warnings.warn(
"is_regression=True was supplied but targets are strings - casting to floats"
)
targets = targets.astype(np.float64)
# set class_names if classification task and targets with integer labels
if train and not self.is_regression and not isinstance(targets[0], str):
class_names = list(set(targets))
class_names.sort()
class_names = list(map(str, class_names))
if len(class_names) == 2:
class_names = [
"not_" + self.label_columns[0],
self.label_columns[0],
]
else:
class_names = [self.label_columns[0] + "_" + c for c in class_names]
self.set_classes(class_names)
# transform targets
targets = super().apply(
targets, train=train
) # self.c (new label_columns) may be modified here
targets = (
targets if len(targets.shape) > 1 else np.expand_dims(targets, 1)
) # since self.label_columns is list
# modify DataFrame
if labels_exist:
for l in self.label_columns:
del df[l] # delete old label columns
new_lab_cols = self.get_label_columns(squeeze=False)
if len(new_lab_cols) != targets.shape[1]:
raise ValueError(
"mismatch between target shape and number of labels - please open ktrain GitHub issue"
)
for i, col in enumerate(new_lab_cols):
df[col] = targets[:, i]
df[new_lab_cols] = targets
print(new_lab_cols)
print(df[new_lab_cols].head())
df[new_lab_cols] = df[new_lab_cols].astype("float32")
return df
def apply_train(self, df):
return self.apply(df, train=True)
def apply_test(self, df):
return self.apply(df, train=False)</code></pre>
</details>
</section>
<section>
</section>
<section>
</section>
<section>
<h2 class="section-title" id="header-functions">Functions</h2>
<dl>
<dt id="ktrain.utils.add_headers_to_df"><code class="name flex">
<span>def <span class="ident">add_headers_to_df</span></span>(<span>fname_in, header_dict, fname_out=None)</span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def add_headers_to_df(fname_in, header_dict, fname_out=None):
df = pd.read_csv(fname_in, header=None)
df.rename(columns=header_dict, inplace=True)
if fname_out is None:
name, ext = os.path.splitext(fname_in)
name += "-headers"
fname_out = name + "." + ext
df.to_csv(fname_out, index=False) # save to new csv file
return</code></pre>
</details>
</dd>
<dt id="ktrain.utils.bad_data_tuple"><code class="name flex">
<span>def <span class="ident">bad_data_tuple</span></span>(<span>data)</span>
</code></dt>
<dd>
<div class="desc"><p>Checks for standard tuple or BERT-style tuple</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def bad_data_tuple(data):
"""
Checks for standard tuple or BERT-style tuple
"""
if (
not isinstance(data, tuple)
or len(data) != 2