{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": [ "# Installing dependencies\n", "## Please make a copy of this notebook." ], "id": "13156d7ed48b282" }, { "metadata": {}, "cell_type": "markdown", "source": [ "# Huggingface login\n", "You will require your personal token." ], "id": "432a756039e6399" }, { "metadata": {}, "cell_type": "code", "source": "# !huggingface-cli login", "id": "2e73da09a7c6171e", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "markdown", "source": "# Part 1: Load Data", "id": "c731d9c1ebb477dc" }, { "metadata": {}, "cell_type": "markdown", "source": "## Downloading the train and test dataset", "id": "14070f20b547688f" }, { "metadata": {}, "cell_type": "markdown", "source": "", "id": "b8920847b7cc378d" }, { "metadata": {}, "cell_type": "code", "source": [ "from datasets import load_dataset\n", "\n", "dataset_train = load_dataset(\"CISProject/FOX_NBC\", split=\"train\")\n", "dataset_test = load_dataset(\"CISProject/FOX_NBC\", split=\"test\")\n", "# dataset_test = load_dataset(\"CISProject/FOX_NBC\", split=\"test_data_random_subset\")\n" ], "id": "877c90c978d62b7d", "outputs": [], "execution_count": 32 }, { "metadata": { "ExecuteTime": { "end_time": "2024-12-16T08:10:32.645990Z", "start_time": "2024-12-16T08:10:32.636717Z" } }, "cell_type": "code", "source": [ "import numpy as np\n", "import torch\n", "import re\n", "from transformers import BertTokenizer\n", "from transformers import RobertaTokenizer\n", "from sklearn.feature_extraction.text import CountVectorizer\n", "from gensim.models import KeyedVectors\n", "from sklearn.feature_extraction.text import TfidfVectorizer\n", "\n", "def preprocess_data(data,\n", " mode=\"train\",\n", " vectorizer=None,\n", " w2v_model=None,\n", " max_features=4096,\n", " max_seq_length=128,\n", " num_proc=4):\n", " if w2v_model is None:\n", " raise ValueError(\"w2v_model must be provided for Word2Vec embeddings.\")\n", "\n", " # tokenizer = BertTokenizer.from_pretrained(\"bert-base-uncased\")\n", " tokenizer = RobertaTokenizer.from_pretrained(\"roberta-base\")\n", " # 1. Clean text once\n", " def clean_text(examples):\n", " import re\n", " cleaned = []\n", " for text in examples[\"title\"]:\n", " text = text.lower()\n", " text = re.sub(r'[^\\w\\s]', '', text)\n", " text = text.strip()\n", " cleaned.append(text)\n", " return {\"clean_title\": cleaned}\n", "\n", " data = data.map(clean_text, batched=True, num_proc=num_proc)\n", "\n", " # 2. Fit CountVectorizer on training data if needed\n", " if mode == \"train\" and vectorizer is None:\n", " # Collect all cleaned titles to fit\n", " all_titles = data[\"clean_title\"]\n", " #vectorizer = CountVectorizer(max_features=max_features, ngram_range=(1,2))\n", " vectorizer = TfidfVectorizer(max_features=max_features)\n", " vectorizer.fit(all_titles)\n", " print(\"vectorizer fitted on training data.\")\n", "\n", " # 3. Transform titles with vectorizer once\n", " def vectorize_batch(examples):\n", " import numpy as np\n", " freq = vectorizer.transform(examples[\"clean_title\"]).toarray().astype(np.float32)\n", " return {\"freq_inputs\": freq}\n", "\n", " data = data.map(vectorize_batch, batched=True, num_proc=num_proc)\n", "\n", " # 4. Tokenize with BERT once\n", " def tokenize_batch(examples):\n", " tokenized = tokenizer(\n", " examples[\"title\"],\n", " padding=\"max_length\",\n", " truncation=True,\n", " max_length=max_seq_length\n", " )\n", " return {\n", " \"input_ids\": tokenized[\"input_ids\"],\n", " \"attention_mask\": tokenized[\"attention_mask\"]\n", " }\n", "\n", " data = data.map(tokenize_batch, batched=True, num_proc=num_proc)\n", "\n", " # 5. Convert titles into tokens for W2V\n", " def split_tokens(examples):\n", " tokens_list = [t.split() for t in examples[\"clean_title\"]]\n", " return {\"tokens\": tokens_list}\n", "\n", " data = data.map(split_tokens, batched=True, num_proc=num_proc)\n", "\n", " # Build an embedding dictionary for all unique tokens (do this once before embedding map)\n", " unique_tokens = set()\n", " for tokens in data[\"tokens\"]:\n", " unique_tokens.update(tokens)\n", "\n", " embedding_dim = w2v_model.vector_size\n", " embedding_dict = {}\n", " for tk in unique_tokens:\n", " if tk in w2v_model:\n", " embedding_dict[tk] = w2v_model[tk].astype(np.float32)\n", " else:\n", " embedding_dict[tk] = np.zeros((embedding_dim,), dtype=np.float32)\n", "\n", " def w2v_embedding_batch(examples):\n", " import numpy as np\n", " batch_w2v = []\n", " for tokens in examples[\"tokens\"]:\n", " vectors = [embedding_dict[tk] for tk in tokens[:max_seq_length]]\n", " if len(vectors) < max_seq_length:\n", " vectors += [np.zeros((embedding_dim,), dtype=np.float32)] * (max_seq_length - len(vectors))\n", " batch_w2v.append(vectors)\n", " return {\"pos_inputs\": batch_w2v}\n", "\n", "\n", " data = data.map(w2v_embedding_batch, batched=True, batch_size=32, num_proc=num_proc)\n", "\n", " # 7. Create labels\n", " def make_labels(examples):\n", " labels = [1.0 if agency == \"fox\" else 0.0 for agency in examples[\"news\"]]\n", " return {\"labels\": labels}\n", "\n", " data = data.map(make_labels, batched=True, num_proc=num_proc)\n", "\n", " # Convert freq_inputs and pos_inputs to torch tensors in a final map step\n", " def to_tensors(examples):\n", " import torch\n", "\n", " freq_inputs = torch.tensor(examples[\"freq_inputs\"], dtype=torch.float32)\n", " input_ids = torch.tensor(examples[\"input_ids\"])\n", " attention_mask = torch.tensor(examples[\"attention_mask\"])\n", " pos_inputs = torch.tensor(examples[\"pos_inputs\"], dtype=torch.float32)\n", " labels = torch.tensor(examples[\"labels\"],dtype=torch.long)\n", "\n", " # seq_inputs shape: (batch_size, 2, seq_len)\n", " seq_inputs = torch.stack([input_ids, attention_mask], dim=1)\n", "\n", " return {\n", " \"freq_inputs\": freq_inputs,\n", " \"seq_inputs\": seq_inputs,\n", " \"pos_inputs\": pos_inputs,\n", " \"labels\": labels\n", " }\n", "\n", " # Apply final conversion to tensor\n", " processed_data = data.map(to_tensors, batched=True, num_proc=num_proc)\n", "\n", " return processed_data, vectorizer\n" ], "id": "dc2ba675ce880d6d", "outputs": [], "execution_count": 33 }, { "metadata": { "ExecuteTime": { "end_time": "2024-12-16T08:11:25.586667Z", "start_time": "2024-12-16T08:10:32.651505Z" } }, "cell_type": "code", "source": [ "from gensim.models import KeyedVectors\n", "w2v_model = KeyedVectors.load_word2vec_format(\"./GoogleNews-vectors-negative300.bin\", binary=True)\n", "\n", "dataset_train,vectorizer = preprocess_data(\n", " data=dataset_train,\n", " mode=\"train\",\n", " w2v_model=w2v_model,\n", " max_features=8192,\n", " max_seq_length=128\n", ")\n", "\n", "dataset_test, _ = preprocess_data(\n", " data=dataset_test,\n", " mode=\"test\",\n", " vectorizer=vectorizer,\n", " w2v_model=w2v_model,\n", " max_features=8192,\n", " max_seq_length=128\n", ")" ], "id": "158b99950fb22d1", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "vectorizer fitted on training data.\n" ] }, { "data": { "text/plain": [ "Map (num_proc=4): 0%| | 0/3044 [00:00 0.5).int()\n", " # Backward pass\n", " self.optimizer.zero_grad()\n", " loss.backward()\n", " self.optimizer.step()\n", " _, preds = torch.max(preds, dim=1)\n", " # Metrics\n", " epoch_loss += loss.item()\n", " total += y_train.size(0)\n", " # print(preds.shape)\n", " correct += (preds == y_train).sum().item()\n", "\n", " # Log epoch metrics\n", " print(f\"Train Loss: {epoch_loss / len(self.train_loader):.4f}\")\n", " print(f\"Train Accuracy: {correct / total:.4f}\")\n", "\n", " # Validation and Save Checkpoints\n", " if (epoch + 1) % self.val_freq == 0:\n", " self.val()\n", " if (epoch + 1) % self.save_freq == 0:\n", " self.save_checkpoint(epoch + 1)\n", "\n", " # Update learning rate\n", " self.iter_step += 1\n", " self.update_learning_rate()\n", "\n", "\n", " def val(self):\n", " self.model.eval()\n", " val_loss = 0.0\n", " correct = 0\n", " total = 0\n", "\n", " with torch.no_grad():\n", " for batch_inputs, labels in tqdm(self.val_loader, desc=\"Validation\", leave=False):\n", " freq_inputs = batch_inputs[\"freq_inputs\"].to(self.device)\n", " seq_inputs = batch_inputs[\"seq_inputs\"].to(self.device)\n", " pos_inputs = batch_inputs[\"pos_inputs\"].to(self.device)\n", " y_val = labels.to(self.device)\n", "\n", " preds = self.model({\"freq_inputs\": freq_inputs, \"seq_inputs\": seq_inputs, \"pos_inputs\": pos_inputs})\n", " loss = self.criterion(preds, y_val)\n", " # preds = (torch.sigmoid(preds)>0.5).float()\n", " _, preds = torch.max(preds, dim=1)\n", " val_loss += loss.item()\n", " total += y_val.size(0)\n", " correct += (preds == y_val).sum().item()\n", " if self.val_loss is None or val_loss < self.val_loss:\n", " self.val_loss = val_loss\n", " self.save_checkpoint(\"best\")\n", " # Log validation metrics\n", " print(f\"Validation Loss: {val_loss / len(self.val_loader):.4f}\")\n", " print(f\"Validation Accuracy: {correct / total:.4f}\")\n", "\n", " def save_checkpoint(self, epoch):\n", " \"\"\"Save model in Hugging Face format.\"\"\"\n", " checkpoint_dir = os.path.join(self.save_path, f\"checkpoint_epoch_{epoch}\")\n", " if epoch ==\"best\":\n", " checkpoint_dir = os.path.join(self.save_path, \"best\")\n", " self.model.save_pretrained(checkpoint_dir)\n", " print(f\"Checkpoint saved at {checkpoint_dir}\")" ], "id": "7be377251b81a25d", "outputs": [], "execution_count": 38 }, { "metadata": { "ExecuteTime": { "end_time": "2024-12-16T08:23:59.280460Z", "start_time": "2024-12-16T08:11:25.911156Z" } }, "cell_type": "code", "source": [ "from torch.utils.data import DataLoader\n", "\n", "# Define a collate function to handle the batched data\n", "def collate_fn(batch):\n", " freq_inputs = torch.stack([torch.tensor(item[\"freq_inputs\"]) for item in batch])\n", " seq_inputs = torch.stack([torch.tensor(item[\"seq_inputs\"]) for item in batch])\n", " pos_inputs = torch.stack([torch.tensor(item[\"pos_inputs\"]) for item in batch])\n", " labels = torch.tensor([torch.tensor(item[\"labels\"],dtype=torch.long) for item in batch])\n", " return {\"freq_inputs\": freq_inputs, \"seq_inputs\": seq_inputs, \"pos_inputs\": pos_inputs}, labels\n", "\n", "train_loader = DataLoader(dataset_train, batch_size=config.train[\"batch_size\"], shuffle=True,collate_fn=collate_fn)\n", "test_loader = DataLoader(dataset_test, batch_size=config.train[\"batch_size\"], shuffle=False,collate_fn=collate_fn)\n", "trainer = Trainer(model, train_loader, test_loader, config)\n", "\n", "# Train the model\n", "trainer.train()\n", "# Save the final model in Hugging Face format\n", "final_save_path = os.path.join(config.base_exp_dir, \"checkpoints\")\n", "model.save_pretrained(final_save_path)\n", "print(f\"Final model saved at {final_save_path}\")\n" ], "id": "dd1749c306f148eb", "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Epoch 1/10: 100%|██████████| 96/96 [01:00<00:00, 1.60it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.6920\n", "Train Accuracy: 0.5217\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Checkpoint saved at ./exp/fox_nbc/checkpoints\\best\n", "Validation Loss: 0.6914\n", "Validation Accuracy: 0.5322\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Epoch 2/10: 100%|██████████| 96/96 [01:00<00:00, 1.59it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.6137\n", "Train Accuracy: 0.6390\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Checkpoint saved at ./exp/fox_nbc/checkpoints\\best\n", "Validation Loss: 0.4313\n", "Validation Accuracy: 0.7937\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Epoch 3/10: 100%|██████████| 96/96 [01:00<00:00, 1.59it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.3599\n", "Train Accuracy: 0.8466\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Validation Loss: 0.4837\n", "Validation Accuracy: 0.7911\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Epoch 4/10: 100%|██████████| 96/96 [01:00<00:00, 1.59it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.2002\n", "Train Accuracy: 0.9208\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Checkpoint saved at ./exp/fox_nbc/checkpoints\\best\n", "Validation Loss: 0.3466\n", "Validation Accuracy: 0.8647\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Epoch 5/10: 100%|██████████| 96/96 [01:00<00:00, 1.57it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0939\n", "Train Accuracy: 0.9655\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Validation Loss: 0.5384\n", "Validation Accuracy: 0.8371\n", "Checkpoint saved at ./exp/fox_nbc/checkpoints\\checkpoint_epoch_5\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Epoch 6/10: 100%|██████████| 96/96 [01:00<00:00, 1.58it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0555\n", "Train Accuracy: 0.9800\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Validation Loss: 0.4440\n", "Validation Accuracy: 0.8857\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Epoch 7/10: 100%|██████████| 96/96 [01:00<00:00, 1.58it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0422\n", "Train Accuracy: 0.9855\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Validation Loss: 0.5519\n", "Validation Accuracy: 0.8739\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Epoch 8/10: 100%|██████████| 96/96 [01:01<00:00, 1.56it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0163\n", "Train Accuracy: 0.9944\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Validation Loss: 0.6396\n", "Validation Accuracy: 0.8581\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Epoch 9/10: 100%|██████████| 96/96 [01:01<00:00, 1.57it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0125\n", "Train Accuracy: 0.9974\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Validation Loss: 0.4890\n", "Validation Accuracy: 0.8857\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Epoch 10/10: 100%|██████████| 96/96 [01:17<00:00, 1.24it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0039\n", "Train Accuracy: 0.9997\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Validation Loss: 0.5024\n", "Validation Accuracy: 0.8883\n", "Checkpoint saved at ./exp/fox_nbc/checkpoints\\checkpoint_epoch_10\n", "Final model saved at ./exp/fox_nbc/checkpoints\n" ] } ], "execution_count": 39 }, { "metadata": {}, "cell_type": "markdown", "source": "## Evaluate Model", "id": "4af000263dd99bca" }, { "metadata": { "ExecuteTime": { "end_time": "2024-12-16T08:24:40.129577Z", "start_time": "2024-12-16T08:24:26.080416Z" } }, "cell_type": "code", "source": [ "from transformers import AutoConfig, AutoModel\n", "from sklearn.metrics import accuracy_score, classification_report\n", "def load_last_checkpoint(checkpoint_dir):\n", " # Find all checkpoints in the directory\n", " checkpoints = [f for f in os.listdir(checkpoint_dir) if f.startswith(\"checkpoint_epoch_\")]\n", " if not checkpoints:\n", " raise FileNotFoundError(f\"No checkpoints found in {checkpoint_dir}!\")\n", " # Sort checkpoints by epoch number\n", " checkpoints.sort(key=lambda x: int(x.split(\"_\")[-1]))\n", "\n", " # Load the last checkpoint\n", " last_checkpoint = os.path.join(checkpoint_dir, checkpoints[-1])\n", " # print(f\"Loading checkpoint from {last_checkpoint}\")\n", " # Load the best checkpoint\n", " #if os.path.join(checkpoint_dir, \"best\") is not None:\n", " # last_checkpoint = os.path.join(checkpoint_dir, \"best\")\n", " print(f\"Loading checkpoint from {last_checkpoint}\")\n", " # Load model and config\n", " config = AutoConfig.from_pretrained(last_checkpoint)\n", " model = AutoModel.from_pretrained(last_checkpoint, config=config)\n", " return model\n", "\n", "# Step 1: Define paths and setup\n", "checkpoint_dir = os.path.join(config.base_exp_dir, \"checkpoints\") # Directory where checkpoints are stored\n", "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "model = load_last_checkpoint(checkpoint_dir)\n", "model.to(device)\n", "\n", "# criterion = torch.nn.BCEWithLogitsLoss()\n", "\n", "criterion = torch.nn.CrossEntropyLoss()\n", "\n", "def evaluate_model(model, val_loader, criterion, device=\"cuda\"):\n", " model.eval()\n", " val_loss = 0.0\n", " correct = 0\n", " total = 0\n", " all_preds = []\n", " all_labels = []\n", " with torch.no_grad():\n", " for batch_inputs, labels in tqdm(val_loader, desc=\"Testing\", leave=False):\n", " freq_inputs = batch_inputs[\"freq_inputs\"].to(device)\n", " seq_inputs = batch_inputs[\"seq_inputs\"].to(device)\n", " pos_inputs = batch_inputs[\"pos_inputs\"].to(device)\n", " labels = labels.to(device)\n", "\n", " preds= model({\"freq_inputs\": freq_inputs, \"seq_inputs\": seq_inputs, \"pos_inputs\": pos_inputs})\n", " loss = criterion(preds, labels)\n", " _, preds = torch.max(preds, dim=1)\n", " # preds = (torch.sigmoid(preds) > 0.5).float()\n", " val_loss += loss.item()\n", " total += labels.size(0)\n", " # preds = (torch.sigmoid(preds) > 0.5).int()\n", " correct += (preds == labels).sum().item()\n", " all_preds.extend(preds.cpu().numpy())\n", " all_labels.extend(labels.cpu().numpy())\n", "\n", " return accuracy_score(all_labels, all_preds), classification_report(all_labels, all_preds)\n", "\n", "\n", "accuracy, report = evaluate_model(model, test_loader, criterion)\n", "print(f\"Accuracy: {accuracy:.4f}\")\n", "print(report)\n" ], "id": "b75d2dc8a300cdf6", "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Some weights of RobertaModel were not initialized from the model checkpoint at roberta-base and are newly initialized: ['roberta.pooler.dense.bias', 'roberta.pooler.dense.weight']\n", "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n", "Some weights of the model checkpoint at ./exp/fox_nbc/checkpoints\\checkpoint_epoch_10 were not used when initializing CustomModel: ['freq.lin0.parametrizations.weight.original0', 'freq.lin0.parametrizations.weight.original1', 'freq.lin1.parametrizations.weight.original0', 'freq.lin1.parametrizations.weight.original1', 'freq.lin2.parametrizations.weight.original0', 'freq.lin2.parametrizations.weight.original1', 'pos.lin0.parametrizations.weight.original0', 'pos.lin0.parametrizations.weight.original1', 'pos.lin1.parametrizations.weight.original0', 'pos.lin1.parametrizations.weight.original1', 'pos.lin2.parametrizations.weight.original0', 'pos.lin2.parametrizations.weight.original1']\n", "- This IS expected if you are initializing CustomModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n", "- This IS NOT expected if you are initializing CustomModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Loading checkpoint from ./exp/fox_nbc/checkpoints\\checkpoint_epoch_10\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Some weights of CustomModel were not initialized from the model checkpoint at ./exp/fox_nbc/checkpoints\\checkpoint_epoch_10 and are newly initialized: ['freq.lin0.weight_g', 'freq.lin0.weight_v', 'freq.lin1.weight_g', 'freq.lin1.weight_v', 'freq.lin2.weight_g', 'freq.lin2.weight_v', 'pos.lin0.weight_g', 'pos.lin0.weight_v', 'pos.lin1.weight_g', 'pos.lin1.weight_v', 'pos.lin2.weight_g', 'pos.lin2.weight_v']\n", "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n", " " ] }, { "name": "stdout", "output_type": "stream", "text": [ "Accuracy: 0.8883\n", " precision recall f1-score support\n", "\n", " 0 0.86 0.91 0.88 356\n", " 1 0.92 0.87 0.89 405\n", "\n", " accuracy 0.89 761\n", " macro avg 0.89 0.89 0.89 761\n", "weighted avg 0.89 0.89 0.89 761\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\r" ] } ], "execution_count": 41 }, { "metadata": {}, "cell_type": "markdown", "source": "# Part 3. Pushing the Model to the Hugging Face", "id": "d2ffeb383ea00beb" }, { "metadata": { "ExecuteTime": { "end_time": "2024-12-16T08:25:19.716888Z", "start_time": "2024-12-16T08:24:56.833580Z" } }, "cell_type": "code", "source": "model.push_model(REPO_NAME)", "id": "f55c22b0a1b2a66b", "outputs": [ { "data": { "text/plain": [ "README.md: 0%| | 0.00/326 [00:00 0.5).float()\n", " val_loss += loss.item()\n", " total += labels.size(0)\n", " correct += (preds == labels).sum().item()\n", " all_preds.extend(preds.cpu().numpy())\n", " all_labels.extend(labels.cpu().numpy())\n", "\n", " return accuracy_score(all_labels, all_preds), classification_report(all_labels, all_preds)\n", "\n", "\n", "accuracy, report = evaluate_model(model, test_loader, criterion)\n", "print(f\"Accuracy: {accuracy:.4f}\")\n", "print(report)\n" ], "id": "cc313b4396f87690", "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " " ] }, { "name": "stdout", "output_type": "stream", "text": [ "Accuracy: 0.8883\n", " precision recall f1-score support\n", "\n", " 0 0.86 0.91 0.88 356\n", " 1 0.92 0.87 0.89 405\n", "\n", " accuracy 0.89 761\n", " macro avg 0.89 0.89 0.89 761\n", "weighted avg 0.89 0.89 0.89 761\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\r" ] } ], "execution_count": 44 } ], "metadata": { "kernelspec": { "name": "python3", "language": "python", "display_name": "Python 3 (ipykernel)" } }, "nbformat": 5, "nbformat_minor": 9 }