jaynopponep commited on
Commit
ad2ba33
·
1 Parent(s): 7fa7266

Evaluate model

Browse files
Files changed (3) hide show
  1. .idea/discord.xml +1 -1
  2. evaluate.py +62 -0
  3. train.py +1 -0
.idea/discord.xml CHANGED
@@ -1,7 +1,7 @@
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <project version="4">
3
  <component name="DiscordProjectSettings">
4
- <option name="show" value="ASK" />
5
  <option name="description" value="" />
6
  </component>
7
  </project>
 
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <project version="4">
3
  <component name="DiscordProjectSettings">
4
+ <option name="show" value="PROJECT_FILES" />
5
  <option name="description" value="" />
6
  </component>
7
  </project>
evaluate.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ from datasets import Dataset
4
+ from sklearn.metrics import accuracy_score, precision_recall_fscore_support
5
+ from transformers import BertTokenizer, BertForSequenceClassification, Trainer, TrainingArguments
6
+
7
+ # Load the saved model and tokenizer
8
+ def load_model_and_tokenizer(model_path):
9
+ model = BertForSequenceClassification.from_pretrained(model_path, num_labels=2)
10
+ tokenizer = BertTokenizer.from_pretrained(model_path)
11
+ return model, tokenizer
12
+
13
+ # Function to tokenize the evaluation dataset
14
+ def tokenize_function(examples, tokenizer):
15
+ return tokenizer(examples["text"], padding="max_length", truncation=True, max_length=512)
16
+
17
+ # Load and prepare the evaluation dataset
18
+ def load_evaluation_data(file_path, tokenizer):
19
+ df = pd.read_csv(file_path)
20
+ eval_dataset = Dataset.from_pandas(df)
21
+ eval_dataset = eval_dataset.map(lambda examples: tokenize_function(examples, tokenizer), batched=True)
22
+ eval_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
23
+ return eval_dataset
24
+
25
+ # Define the compute_metrics function to be used by the Trainer
26
+ def compute_metrics(pred):
27
+ labels = pred.label_ids
28
+ preds = np.argmax(pred.predictions, axis=-1)
29
+ precision, recall, f1, _ = precision_recall_fscore_support(labels, preds, average='binary')
30
+ acc = accuracy_score(labels, preds)
31
+ return {
32
+ 'accuracy': acc,
33
+ 'f1': f1,
34
+ 'precision': precision,
35
+ 'recall': recall
36
+ }
37
+
38
+ # Evaluation function using Hugging Face's Trainer
39
+ def evaluate_model(model, tokenizer, eval_dataset):
40
+ training_args = TrainingArguments(
41
+ output_dir="./results",
42
+ per_device_eval_batch_size=8
43
+ )
44
+ trainer = Trainer(
45
+ model=model,
46
+ args=training_args,
47
+ eval_dataset=eval_dataset,
48
+ compute_metrics=compute_metrics
49
+ )
50
+ results = trainer.evaluate()
51
+ return results
52
+
53
+ # Main function to run the evaluation
54
+ if __name__ == "__main__":
55
+ model_path = "./trained_model" # Path where the model and tokenizer are saved
56
+ eval_data_path = "path_to_evaluation_data.csv" # Path to your evaluation dataset CSV file
57
+
58
+ model, tokenizer = load_model_and_tokenizer(model_path)
59
+ eval_dataset = load_evaluation_data(eval_data_path, tokenizer)
60
+ evaluation_results = evaluate_model(model, tokenizer, eval_dataset)
61
+
62
+ print("Evaluation Results:", evaluation_results)
train.py CHANGED
@@ -13,6 +13,7 @@ train_df, eval_df = train_test_split(df, test_size=0.2)
13
  # Tokenizer
14
  tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
15
 
 
16
  def tokenize_function(examples):
17
  return tokenizer(examples["text"], padding="max_length", truncation=True, max_length=512)
18
 
 
13
  # Tokenizer
14
  tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
15
 
16
+
17
  def tokenize_function(examples):
18
  return tokenizer(examples["text"], padding="max_length", truncation=True, max_length=512)
19