-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
81 lines (62 loc) · 2.09 KB
/
Copy pathtrain.py
File metadata and controls
81 lines (62 loc) · 2.09 KB
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
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix
from tensorflow.keras.callbacks import (EarlyStopping, ReduceLROnPlateau, ModelCheckpoint, CSVLogger)
from visualization import (save_training_history, save_confusion_matrix, save_classification_report)
from config import Config
class Trainer:
def __init__(self, model):
self.model = model
def train(self, x_train, y_train, x_val, y_val):
callbacks = [
EarlyStopping(
monitor="val_loss",
patience=5,
mode="min",
restore_best_weights=True,
),
ReduceLROnPlateau(
monitor="val_loss",
mode="min",
factor=0.5,
patience=3,
min_lr=0.000001
),
ModelCheckpoint(
Config.MODEL_PATH,
save_best_only=True,
mode="min",
monitor="val_loss"
),
CSVLogger('results/training_log.csv', append=True)
]
history = self.model.fit(
x_train,
y_train,
validation_data=(x_val, y_val),
epochs=Config.EPOCHS,
batch_size=Config.BATCH_SIZE,
callbacks=callbacks
)
save_training_history(history)
return history
def evaluate(self, x_test, y_test):
predictions = self.model.predict(
x_test,
verbose=0
)
y_pred = np.argmax(
predictions,
axis=1
)
print("\nClassification Report")
report = classification_report(
y_test,
y_pred,
target_names=["Negative", "Positive"],
zero_division=0
)
print(report)
save_classification_report(report)
print("\nConfusion Matrix")
print(confusion_matrix(y_test, y_pred))
save_confusion_matrix(y_test, y_pred, ["Negative", "Positive"])