{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "ac15a924", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "=== Treniranje i evaluacija za trening skup: train_combined ===\n", "\n", "--- Fine-tuning model: classla/bcms-bertic ---\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Some weights of ElectraForSequenceClassification were not initialized from the model checkpoint at classla/bcms-bertic and are newly initialized: ['classifier.dense.bias', 'classifier.dense.weight', 'classifier.out_proj.bias', 'classifier.out_proj.weight']\n", "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "aed062bba63e49b5a657521638acc86c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/7577 [00:00\n", " \n", " \n", " [1422/1422 45:18, Epoch 3/3]\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
StepTraining Loss
500.929600
1000.843100
1500.744100
2000.645000
2500.633000
3000.641400
3500.618200
4000.594200
4500.578800
5000.484900
5500.436400
6000.485900
6500.484800
7000.509100
7500.437300
8000.518500
8500.512200
9000.410600
9500.471700
10000.401200
10500.374100
11000.397300
11500.363700
12000.325300
12500.291000
13000.335900
13500.395900
14000.331800

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/torch/utils/data/dataloader.py:683: UserWarning: 'pin_memory' argument is set as true but not supported on MPS now, then device pinned memory won't be used.\n", " warnings.warn(warn_msg)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Evaluacija na test skupu test-1\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "fb87532e026a4d0b8aa72d5cbfc4fb2a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/653 [00:00" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Evaluacija: {'eval_loss': 0.8313503265380859, 'eval_accuracy': 0.7136294027565084, 'eval_f1_macro': 0.624180014657386, 'eval_runtime': 16.74, 'eval_samples_per_second': 39.008, 'eval_steps_per_second': 1.254, 'epoch': 3.0}\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/torch/utils/data/dataloader.py:683: UserWarning: 'pin_memory' argument is set as true but not supported on MPS now, then device pinned memory won't be used.\n", " warnings.warn(warn_msg)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Confusion Matrix:\n", "[[109 48 8]\n", " [ 70 328 32]\n", " [ 4 25 29]]\n", "\n", "Classification Report:\n", " precision recall f1-score support\n", "\n", " negative 0.60 0.66 0.63 165\n", " neutral 0.82 0.76 0.79 430\n", " positive 0.42 0.50 0.46 58\n", "\n", " accuracy 0.71 653\n", " macro avg 0.61 0.64 0.62 653\n", "weighted avg 0.73 0.71 0.72 653\n", "\n", "Predikcije spremljene u results_train_combined/predictions_test_1.csv\n", "\n", "Evaluacija na test skupu test-2\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "109da4be6d1d4c0d826f1320e481d4e1", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/741 [00:00" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Evaluacija: {'eval_loss': 0.23835134506225586, 'eval_accuracy': 0.9257759784075573, 'eval_f1_macro': 0.907760132195386, 'eval_runtime': 19.6933, 'eval_samples_per_second': 37.627, 'eval_steps_per_second': 1.219, 'epoch': 3.0}\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/torch/utils/data/dataloader.py:683: UserWarning: 'pin_memory' argument is set as true but not supported on MPS now, then device pinned memory won't be used.\n", " warnings.warn(warn_msg)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Confusion Matrix:\n", "[[197 16 3]\n", " [ 14 410 7]\n", " [ 3 12 79]]\n", "\n", "Classification Report:\n", " precision recall f1-score support\n", "\n", " negative 0.92 0.91 0.92 216\n", " neutral 0.94 0.95 0.94 431\n", " positive 0.89 0.84 0.86 94\n", "\n", " accuracy 0.93 741\n", " macro avg 0.91 0.90 0.91 741\n", "weighted avg 0.93 0.93 0.93 741\n", "\n", "Predikcije spremljene u results_train_combined/predictions_test_2.csv\n", "\n", "Evaluacija na test skupu test-3\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3b91d6b9cfde4469a865dd69c79b4f34", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/793 [00:00" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Evaluacija: {'eval_loss': 0.8141497373580933, 'eval_accuracy': 0.7679697351828499, 'eval_f1_macro': 0.7678761268324849, 'eval_runtime': 20.857, 'eval_samples_per_second': 38.021, 'eval_steps_per_second': 1.199, 'epoch': 3.0}\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/torch/utils/data/dataloader.py:683: UserWarning: 'pin_memory' argument is set as true but not supported on MPS now, then device pinned memory won't be used.\n", " warnings.warn(warn_msg)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Confusion Matrix:\n", "[[212 51 4]\n", " [ 7 250 6]\n", " [ 6 110 147]]\n", "\n", "Classification Report:\n", " precision recall f1-score support\n", "\n", " negative 0.94 0.79 0.86 267\n", " neutral 0.61 0.95 0.74 263\n", " positive 0.94 0.56 0.70 263\n", "\n", " accuracy 0.77 793\n", " macro avg 0.83 0.77 0.77 793\n", "weighted avg 0.83 0.77 0.77 793\n", "\n", "Predikcije spremljene u results_train_combined/predictions_test_3.csv\n", "\n", "\n", "=== Treniranje i evaluacija za trening skup: train_2 ===\n", "\n", "--- Fine-tuning model: classla/bcms-bertic ---\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Some weights of ElectraForSequenceClassification were not initialized from the model checkpoint at classla/bcms-bertic and are newly initialized: ['classifier.dense.bias', 'classifier.dense.weight', 'classifier.out_proj.bias', 'classifier.out_proj.weight']\n", "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a7f4d6a07e7544b38dc869d92f60dbf0", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/2221 [00:00\n", " \n", " \n", " [417/417 09:50, Epoch 3/3]\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
StepTraining Loss
500.998700
1000.815300
1500.685500
2000.542600
2500.520300
3000.464000
3500.380800
4000.328300

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Evaluacija na test skupu test-1\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4816fe0de415465ca0d982ff0ddc9f1b", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/653 [00:00" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Evaluacija: {'eval_loss': 0.8404552340507507, 'eval_accuracy': 0.6906584992343032, 'eval_f1_macro': 0.5999228826553304, 'eval_runtime': 15.1161, 'eval_samples_per_second': 43.199, 'eval_steps_per_second': 1.389, 'epoch': 3.0}\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/torch/utils/data/dataloader.py:683: UserWarning: 'pin_memory' argument is set as true but not supported on MPS now, then device pinned memory won't be used.\n", " warnings.warn(warn_msg)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Confusion Matrix:\n", "[[116 42 7]\n", " [ 86 309 35]\n", " [ 7 25 26]]\n", "\n", "Classification Report:\n", " precision recall f1-score support\n", "\n", " negative 0.56 0.70 0.62 165\n", " neutral 0.82 0.72 0.77 430\n", " positive 0.38 0.45 0.41 58\n", "\n", " accuracy 0.69 653\n", " macro avg 0.59 0.62 0.60 653\n", "weighted avg 0.72 0.69 0.70 653\n", "\n", "Predikcije spremljene u results_train_2/predictions_test_1.csv\n", "\n", "Evaluacija na test skupu test-2\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6459394251ff4731938cb87fdba6c9bd", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/741 [00:00" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Evaluacija: {'eval_loss': 0.5182289481163025, 'eval_accuracy': 0.8083670715249662, 'eval_f1_macro': 0.7534545808339037, 'eval_runtime': 17.475, 'eval_samples_per_second': 42.403, 'eval_steps_per_second': 1.373, 'epoch': 3.0}\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/torch/utils/data/dataloader.py:683: UserWarning: 'pin_memory' argument is set as true but not supported on MPS now, then device pinned memory won't be used.\n", " warnings.warn(warn_msg)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Confusion Matrix:\n", "[[163 44 9]\n", " [ 32 381 18]\n", " [ 10 29 55]]\n", "\n", "Classification Report:\n", " precision recall f1-score support\n", "\n", " negative 0.80 0.75 0.77 216\n", " neutral 0.84 0.88 0.86 431\n", " positive 0.67 0.59 0.62 94\n", "\n", " accuracy 0.81 741\n", " macro avg 0.77 0.74 0.75 741\n", "weighted avg 0.80 0.81 0.81 741\n", "\n", "Predikcije spremljene u results_train_2/predictions_test_2.csv\n", "\n", "Evaluacija na test skupu test-3\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "aed4ce93c6614d97abd44fe642c0b3fa", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/793 [00:00" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Evaluacija: {'eval_loss': 0.9036539793014526, 'eval_accuracy': 0.7112232030264817, 'eval_f1_macro': 0.7055643128874013, 'eval_runtime': 18.7008, 'eval_samples_per_second': 42.405, 'eval_steps_per_second': 1.337, 'epoch': 3.0}\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/torch/utils/data/dataloader.py:683: UserWarning: 'pin_memory' argument is set as true but not supported on MPS now, then device pinned memory won't be used.\n", " warnings.warn(warn_msg)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Confusion Matrix:\n", "[[204 53 10]\n", " [ 17 239 7]\n", " [ 13 129 121]]\n", "\n", "Classification Report:\n", " precision recall f1-score support\n", "\n", " negative 0.87 0.76 0.81 267\n", " neutral 0.57 0.91 0.70 263\n", " positive 0.88 0.46 0.60 263\n", "\n", " accuracy 0.71 793\n", " macro avg 0.77 0.71 0.71 793\n", "weighted avg 0.77 0.71 0.71 793\n", "\n", "Predikcije spremljene u results_train_2/predictions_test_3.csv\n" ] } ], "source": [ "import pandas as pd\n", "import torch\n", "from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments\n", "from datasets import Dataset\n", "from sklearn.metrics import classification_report, confusion_matrix\n", "\n", "def load_and_prepare_data(train_path):\n", " df = pd.read_csv(train_path)\n", " df = df.rename(columns={\"Label\": \"label\"})\n", " return Dataset.from_pandas(df)\n", "\n", "def load_and_prepare_test_data(test_path):\n", " df = pd.read_csv(test_path)\n", " df = df.rename(columns={\"Label\": \"label\"})\n", " return Dataset.from_pandas(df), df\n", "\n", "def tokenize_dataset(dataset, tokenizer):\n", " def tokenize_function(examples):\n", " return tokenizer(examples['Sentence'], padding='max_length', truncation=True, max_length=128)\n", " tokenized = dataset.map(tokenize_function, batched=True)\n", " tokenized.set_format(type='torch', columns=['input_ids', 'attention_mask', 'label'])\n", " return tokenized\n", "\n", "def compute_metrics(eval_pred):\n", " logits, labels = eval_pred\n", " preds = torch.argmax(torch.tensor(logits), axis=1).numpy()\n", " report = classification_report(labels, preds, output_dict=True)\n", " acc = report['accuracy']\n", " f1 = report['macro avg']['f1-score']\n", " return {'accuracy': acc, 'f1_macro': f1}\n", "\n", "def train_and_evaluate(model_name, train_dataset, test_datasets, raw_test_dfs, output_base_dir):\n", " print(f\"\\n--- Fine-tuning model: {model_name} ---\")\n", "\n", " tokenizer = AutoTokenizer.from_pretrained(model_name)\n", " model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=3)\n", "\n", " tokenized_train = tokenize_dataset(train_dataset, tokenizer)\n", "\n", " training_args = TrainingArguments(\n", " output_dir=f\"{output_base_dir}/model\",\n", " learning_rate=2e-5,\n", " per_device_train_batch_size=16,\n", " per_device_eval_batch_size=32,\n", " num_train_epochs=3,\n", " weight_decay=0.01,\n", " load_best_model_at_end=False,\n", " logging_dir=f\"{output_base_dir}/logs\",\n", " logging_steps=50,\n", " save_total_limit=2,\n", " seed=42,\n", " )\n", "\n", " trainer = Trainer(\n", " model=model,\n", " args=training_args,\n", " train_dataset=tokenized_train,\n", " compute_metrics=compute_metrics,\n", " )\n", "\n", " # Treniraj model\n", " trainer.train()\n", "\n", " # Spremi model nakon treninga\n", " trainer.save_model()\n", "\n", " # Evaluiraj i predvidi na svakom test skupu\n", " for i, (test_dataset, raw_test_df) in enumerate(zip(test_datasets, raw_test_dfs), start=1):\n", " print(f\"\\nEvaluacija na test skupu test-{i}\")\n", "\n", " tokenized_test = tokenize_dataset(test_dataset, tokenizer)\n", " eval_results = trainer.evaluate(eval_dataset=tokenized_test)\n", " print(f\"Evaluacija: {eval_results}\")\n", "\n", " predictions_output = trainer.predict(tokenized_test)\n", " preds = torch.argmax(torch.tensor(predictions_output.predictions), axis=1).numpy()\n", " labels = predictions_output.label_ids\n", "\n", " print(\"Confusion Matrix:\")\n", " print(confusion_matrix(labels, preds))\n", "\n", " print(\"\\nClassification Report:\")\n", " print(classification_report(labels, preds, target_names=['negative', 'neutral', 'positive']))\n", "\n", " # Spremi predikcije u CSV\n", " output_df = raw_test_df.copy()\n", " output_df['predicted_label'] = preds\n", " output_df['correct'] = output_df['label'] == output_df['predicted_label']\n", " output_csv = f\"{output_base_dir}/predictions_test_{i}.csv\"\n", " output_df.to_csv(output_csv, index=False)\n", " print(f\"Predikcije spremljene u {output_csv}\")\n", "\n", "if __name__ == \"__main__\":\n", " # Učitaj trening skupove zasebno\n", " train_files = {\n", " \"train_combined\": \"TRAIN.csv\",\n", " \"train_2\": \"train-2.csv\"\n", " }\n", "\n", " # Učitaj test skupove\n", " test_files = [\"test-1.csv\", \"test-2.csv\", \"test-3.csv\"]\n", " test_datasets = []\n", " raw_test_dfs = []\n", " for f in test_files:\n", " ds, df = load_and_prepare_test_data(f)\n", " test_datasets.append(ds)\n", " raw_test_dfs.append(df)\n", "\n", " model_name = \"classla/bcms-bertic\"\n", "\n", " # Za svaki trening skup treniraj i evaluiraj model na sva tri testa\n", " for train_name, train_path in train_files.items():\n", " print(f\"\\n\\n=== Treniranje i evaluacija za trening skup: {train_name} ===\")\n", " train_dataset = load_and_prepare_data(train_path)\n", " output_dir = f\"results_{train_name}\"\n", " train_and_evaluate(model_name, train_dataset, test_datasets, raw_test_dfs, output_dir)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.3" } }, "nbformat": 4, "nbformat_minor": 5 }