-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainer.py
More file actions
48 lines (35 loc) · 1.74 KB
/
Copy pathtrainer.py
File metadata and controls
48 lines (35 loc) · 1.74 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
import tensorflow as tf
from transformers import TFBertForSequenceClassification
from sklearn.metrics import classification_report
class Trainer:
def __init__(self, options):
self.opt = options
# Load the BERT model for sequence classification
self.model = TFBertForSequenceClassification.from_pretrained(self.opt.model_path, num_labels=3)
# Compile the model
optimizer = tf.keras.optimizers.Adam(learning_rate=self.opt.learning_rate, epsilon=self.opt.epsilon)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
self.model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
# Print the model summary
self.model.summary()
def train(self, X_train_encoded, X_test_encoded, y_train_encoded, y_test_encoded):
# Train the model
history = self.model.fit(
{'input_ids': X_train_encoded['input_ids'], 'attention_mask': X_train_encoded['attention_mask']},
y_train_encoded,
validation_data=(
{'input_ids': X_test_encoded['input_ids'], 'attention_mask': X_test_encoded['attention_mask']},
y_test_encoded
),
epochs=self.opt.num_epochs,
batch_size=self.opt.batch_size,
)
def save_model(self):
# Save the entire model
self.model.save(self.opt.model_output)
def evaluate(self, X_test_encoded, y_test_encoded):
# Predict on the test set
y_pred_prob = self.model.predict([X_test_encoded['input_ids'], X_test_encoded['attention_mask']])
y_pred_class = y_pred_prob.logits.argmax(axis=-1)
# Generate a classification report
print(classification_report(y_test_encoded, y_pred_class))