{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "6826a9ff-3dbb-42f1-b5ea-b2cd0cbabf44", "metadata": {}, "outputs": [], "source": [ "from transformers import (\n", " AutoConfig,\n", " AutoModelForSequenceClassification,\n", " AutoTokenizer,\n", " DataCollatorWithPadding,\n", " EvalPrediction,\n", " HfArgumentParser,\n", " Trainer,\n", " TrainingArguments,\n", " default_data_collator,\n", " set_seed,\n", ")\n", "import json\n", "from tqdm import tqdm" ] }, { "cell_type": "code", "execution_count": 4, "id": "477afce3-a672-4154-b5f4-b5cee162a6f7", "metadata": {}, "outputs": [], "source": [ "model = AutoModelForSequenceClassification.from_pretrained(\"sentiment-malay-deberta/checkpoint-88000\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "d3d5a606-859e-4b26-a892-937296299ca8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'label': 'negative', 'score': 0.6645314693450928}, {'label': 'negative', 'score': 0.9972461462020874}, {'label': 'positive', 'score': 0.9917457699775696}, {'label': 'positive', 'score': 0.9973276853561401}, {'label': 'negative', 'score': 0.9989587068557739}, {'label': 'negative', 'score': 0.9864065051078796}]\n" ] } ], "source": [ "from transformers import pipeline\n", "sentiment_analysis = pipeline(\"sentiment-analysis\",model=\"sentiment-malay-deberta/checkpoint-88000\")\n", "print(sentiment_analysis([\"teloq hang\",\"tak prihatin fikir kita ni apa\",\"bossku\",\"macam la dia tu hebat\",\"eh salah\",\"belagak pandai\"]))" ] }, { "cell_type": "code", "execution_count": 11, "id": "d8069cbf-8e5b-43a0-83b1-b00bf00e8526", "metadata": {}, "outputs": [], "source": [ "data = []\n", "\n", "with open('combined-data-sentiment.jsonl') as f:\n", " for x in f:\n", " result = json.loads(x)\n", " if result['split'] != 'train':\n", " data.append(result)" ] }, { "cell_type": "code", "execution_count": 26, "id": "bcdce6cf-34ab-4bdc-a317-48f1d9c187cf", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████████████████████████████████████████████████████████████████████████████████████| 10857/10857 [15:37<00:00, 11.58it/s]\n" ] } ], "source": [ "batch_size = 3\n", "predicted_labels = []\n", "for i in tqdm(range(0, len(data), batch_size)):\n", " batch = data[i:i + batch_size]\n", " batch_predictions = sentiment_analysis([x['text'] for x in batch])\n", "\n", " for predictions in batch_predictions:\n", " max_score_label = predictions['label']\n", "\n", " predicted_labels.append(max_score_label)" ] }, { "cell_type": "code", "execution_count": 27, "id": "0ee8f722-4e53-4245-b27a-59224be2edf1", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/aisyah/.local/lib/python3.8/site-packages/sklearn/metrics/_classification.py:1471: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n", " _warn_prf(average, modifier, msg_start, len(result))\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " precision recall f1-score support\n", "\n", " negative 0.94533 0.97434 0.95962 20108\n", " neutral 0.00000 0.00000 0.00000 699\n", " positive 0.92857 0.93505 0.93180 11762\n", "\n", " accuracy 0.93924 32569\n", " macro avg 0.62463 0.63646 0.63047 32569\n", "weighted avg 0.91899 0.93924 0.92897 32569\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/home/aisyah/.local/lib/python3.8/site-packages/sklearn/metrics/_classification.py:1471: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n", " _warn_prf(average, modifier, msg_start, len(result))\n", "/home/aisyah/.local/lib/python3.8/site-packages/sklearn/metrics/_classification.py:1471: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n", " _warn_prf(average, modifier, msg_start, len(result))\n" ] } ], "source": [ "from sklearn.metrics import classification_report\n", "\n", "print(classification_report([x['sentiment'] for x in data],predicted_labels, digits = 5))" ] }, { "cell_type": "code", "execution_count": 5, "id": "59487536-1153-4ba7-80fc-e46a8080bfd8", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/aisyah/.local/lib/python3.8/site-packages/transformers/utils/hub.py:671: UserWarning: The `organization` argument is deprecated and will be removed in v5 of Transformers. Set your organization directly in the `repo_id` passed instead (`repo_id={organization}/{model_id}`).\n", " warnings.warn(\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "5d8b05ef5fd444df96cb959d9829509b", "version_major": 2, "version_minor": 0 }, "text/plain": [ "model.safetensors: 0%| | 0.00/283M [00:00