From e1fbb3687f5e424a3a7163adea8d4520434db14f Mon Sep 17 00:00:00 2001 From: luc Date: Mon, 31 Aug 2026 17:24:33 +0200 Subject: [PATCH] sentiment-analysis/training/aclimdb-sentiment-training.ipynb: update --- .../training/aclimdb-sentiment-training.ipynb | 82 ++++++++++++++----- 1 file changed, 62 insertions(+), 20 deletions(-) diff --git a/sentiment-analysis/training/aclimdb-sentiment-training.ipynb b/sentiment-analysis/training/aclimdb-sentiment-training.ipynb index 31547a4..312e19c 100644 --- a/sentiment-analysis/training/aclimdb-sentiment-training.ipynb +++ b/sentiment-analysis/training/aclimdb-sentiment-training.ipynb @@ -27,8 +27,8 @@ "import os\n", "import pandas as pd\n", "\n", - "imdb_train_dataset = \"aclimdb/train\"\n", - "imdb_test_dataset = \"aclimdb/test\"\n", + "imdb_train_dataset = 'aclimdb/train'\n", + "imdb_test_dataset = 'aclimdb/test'\n", "\n", "train_reviews = []\n", "train_scores = []\n", @@ -118,7 +118,7 @@ "metadata": {}, "outputs": [], "source": [ - "print(f\"Length training set: {len(df_train)}\\nLength testing set: {len(df_test)}\")" + "print(f'Length training set: {len(df_train)}\\nLength testing set: {len(df_test)}')" ] }, { @@ -136,31 +136,67 @@ "outputs": [], "source": [ "import torch\n", - "from transformers import DistilBertTokenizerFast\n", - "from torch.utils.data import DataLoader\n", - "from transformers import DistilBertForSequenceClassification\n", + "from torch.utils.data import DataLoader, TensorDataset\n", + "from transformers import (\n", + " DistilBertTokenizerFast,\n", + " DistilBertForSequenceClassification\n", + ")\n", "\n", - "model_name = \"distilbert-base-uncased\"\n", + "# Set device cuda\n", + "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n", + "print('Using:', device)\n", + "\n", + "# Define model\n", + "model_name = 'distilbert-base-uncased'\n", "tokenizer = DistilBertTokenizerFast.from_pretrained(model_name)\n", "\n", "# Tokenize and encode the text data\n", - "train_encodings = tokenizer(df_train['text'].tolist(), truncation=True, padding=True, return_tensors='pt')\n", - "test_encodings = tokenizer(df_test['text'].tolist(), truncation=True, padding=True, return_tensors='pt')\n", + "train_encodings = tokenizer(\n", + " df_train['text'].tolist(), \n", + " truncation=True, \n", + " padding=True, \n", + " return_tensors='pt'\n", + ")\n", "\n", - "# Create data loaders\n", - "train_dataset = torch.utils.data.TensorDataset(\n", + "test_encodings = tokenizer(\n", + " df_test['text'].tolist(), \n", + " truncation=True, \n", + " padding=True, \n", + " return_tensors='pt'\n", + ")\n", + "\n", + "# Create tensor dataset\n", + "train_dataset = TensorDataset(\n", " train_encodings['input_ids'], \n", " train_encodings['attention_mask'], \n", " torch.tensor(df_train['s'].values, dtype=torch.float32)\n", ")\n", - "train_dataloader = DataLoader(train_dataset, batch_size=25, shuffle=True)\n", "\n", - "test_dataset = torch.utils.data.TensorDataset(\n", + "test_dataset = TensorDataset(\n", " test_encodings['input_ids'], \n", " test_encodings['attention_mask'], \n", " torch.tensor(df_test['s'].values, dtype=torch.float32)\n", ")\n", - "test_dataloader = DataLoader(test_dataset, batch_size=25, shuffle=False)" + "\n", + "# Create data loaders\n", + "if device == 'cuda':\n", + " en_pin_memory = True\n", + "else:\n", + " en_pin_memory = False\n", + "\n", + "train_dataloader = DataLoader(\n", + " train_dataset, \n", + " batch_size=10, \n", + " shuffle=True,\n", + " pin_memory=en_pin_memory\n", + ")\n", + "\n", + "test_dataloader = DataLoader(\n", + " test_dataset, \n", + " batch_size=10, \n", + " shuffle=False,\n", + " pin_memory=en_pin_memory\n", + ")" ] }, { @@ -177,7 +213,10 @@ "metadata": {}, "outputs": [], "source": [ - "model = DistilBertForSequenceClassification.from_pretrained(model_name, num_labels=1)" + "model = DistilBertForSequenceClassification.from_pretrained(\n", + " model_name, \n", + " num_labels=1\n", + ").to(device)" ] }, { @@ -235,7 +274,7 @@ " for batch in train_dataloader:\n", " global_step += 1\n", " num_batches += 1\n", - " input_ids, attention_mask, target_scores = batch\n", + " input_ids, attention_mask, target_scores = [x.to(device) for x in batch]\n", "\n", " # Forward pass\n", " output = model(input_ids=input_ids, attention_mask=attention_mask)\n", @@ -270,7 +309,7 @@ "\n", " for batch in test_dataloader:\n", " with torch.no_grad():\n", - " input_ids, attention_mask, target_scores = batch\n", + " input_ids, attention_mask, target_scores = [x.to(device) for x in batch]\n", "\n", " # Obtaining the scores\n", " output = model(input_ids=input_ids, attention_mask=attention_mask)\n", @@ -290,7 +329,7 @@ " print(f\"Epoch {epoch + 1}/{num_epochs}, Validation loss: {average_loss:.4f}\\n\")\n", "\n", " # Saving the model\n", - " torch.save(model, f'bert-aclimdb/{epoch + 1}.pth')\n", + " torch.save(model.state_dict(), f'bert-aclimdb/{epoch + 1}.pth')\n", "\n", " # Early stopping check\n", " if average_loss < best_validation_loss:\n", @@ -300,6 +339,7 @@ " no_improvement_counter += 1\n", "\n", " if no_improvement_counter >= early_stop_patience:\n", + " print('Early stopping triggered')\n", " break\n", "\n", "writer.close()" @@ -319,7 +359,9 @@ "metadata": {}, "outputs": [], "source": [ - "model = torch.load('bert-aclimdb/4.pth')" + "model = DistilBertForSequenceClassification.from_pretrained(model_name, num_labels=1)\n", + "model.load_state_dict(torch.load('...', map_location=device))\n", + "model.to(device)" ] }, { @@ -344,7 +386,7 @@ "\n", "for batch in test_dataloader:\n", " with torch.no_grad():\n", - " input_ids, attention_mask, scores = batch\n", + " input_ids, attention_mask, scores = [x.to(device) for x in batch]\n", "\n", " # Obtaining the scores\n", " output = model(input_ids=input_ids, attention_mask=attention_mask)\n",