{ "cells": [ { "cell_type": "code", "id": "initial_id", "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2026-07-05T15:05:59.049967188Z", "start_time": "2026-07-05T15:05:56.833387637Z" } }, "source": [ "import os\n", "os.environ[\"HF_ENDPOINT\"] = \"https://hf-mirror.com\"\n", "import torch\n", "from torch.utils.data import Dataset, DataLoader\n", "import pandas as pd\n", "import transformers\n", "from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments\n", "from tqdm import tqdm" ], "outputs": [], "execution_count": 2 }, { "metadata": { "ExecuteTime": { "end_time": "2026-07-05T15:05:59.150289671Z", "start_time": "2026-07-05T15:05:59.051997256Z" } }, "cell_type": "code", "source": [ "train_df = pd.read_csv('./train.csv')\n", "test_df = pd.read_csv('./test.csv')\n", "from sklearn.model_selection import train_test_split\n", "train_df, val_df = train_test_split(train_df, test_size=0.1, random_state=42, stratify=train_df['label'])\n", "print(f\"Train: {len(train_df)}, Val: {len(val_df)}, Test: {len(test_df)}\")" ], "id": "f32065752f1597fb", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Train: 10908, Val: 1212, Test: 5195\n" ] } ], "execution_count": 3 }, { "metadata": { "ExecuteTime": { "end_time": "2026-07-05T15:05:59.184794043Z", "start_time": "2026-07-05T15:05:59.156968452Z" } }, "cell_type": "code", "source": "train_df.head()", "id": "460c6fffc81814df", "outputs": [ { "data": { "text/plain": [ " id premise \\\n", "9989 6417481c8d यद्यपि हम आज के समय में अल कायदा के साथ केएसएए... \n", "3880 5c5ca34cf6 'Upload him into his body? What body?' \n", "8559 1d3c28ecff yeah and then about every five years you have ... \n", "6316 22e2a4903d θέλω να πω ότι υπήρχε είχα, είχα το ρολόι μου ... \n", "762 307016c21f Yet, in the mouths of the white townsfolk of S... \n", "\n", " hypothesis lang_abv language \\\n", "9989 हर व्यक्ति केएसएम को हमेशा अल कायदा के बराबर म... hi Hindi \n", "3880 I don't think he has a body at all. en English \n", "8559 You have to dig them up every five years, thro... en English \n", "6316 Ήταν ενοχλητικό όταν κάλυψε τα παπούτσια μου. el Greek \n", "762 White townsfolk in Salisbury, N.C. are easily ... en English \n", "\n", " label \n", "9989 2 \n", "3880 1 \n", "8559 1 \n", "6316 1 \n", "762 1 " ], "text/html": [ "
\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", "
idpremisehypothesislang_abvlanguagelabel
99896417481c8dयद्यपि हम आज के समय में अल कायदा के साथ केएसएए...हर व्यक्ति केएसएम को हमेशा अल कायदा के बराबर म...hiHindi2
38805c5ca34cf6'Upload him into his body? What body?'I don't think he has a body at all.enEnglish1
85591d3c28ecffyeah and then about every five years you have ...You have to dig them up every five years, thro...enEnglish1
631622e2a4903dθέλω να πω ότι υπήρχε είχα, είχα το ρολόι μου ...Ήταν ενοχλητικό όταν κάλυψε τα παπούτσια μου.elGreek1
762307016c21fYet, in the mouths of the white townsfolk of S...White townsfolk in Salisbury, N.C. are easily ...enEnglish1
\n", "
" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 4 }, { "metadata": { "ExecuteTime": { "end_time": "2026-07-02T09:54:33.329021843Z", "start_time": "2026-07-02T09:54:22.292806435Z" } }, "cell_type": "code", "source": [ "model_name = \"xlm-roberta-base\"\n", "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", "model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=3)" ], "id": "3d828c6bd763c919", "outputs": [ { "data": { "text/plain": [ "Loading weights: 0%| | 0/197 [00:00 best_val_acc:\n", " best_val_acc = val_acc\n", " torch.save(model.state_dict(), 'best_model.pt')\n", " print(\"Best model saved!\")" ], "id": "9d75896a5b9cf921", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "code", "source": [ "# 加载最佳模型权重\n", "model.load_state_dict(torch.load('best_model.pt'))\n", "model.eval()\n", "\n", "# 构建测试集 Dataset 和 DataLoader(注意测试集没有 label)\n", "class TestDataset(Dataset):\n", " def __init__(self, df, tokenizer, max_length=128):\n", " self.df = df.reset_index(drop=True)\n", " self.tokenizer = tokenizer\n", " self.max_length = max_length\n", "\n", " def __len__(self):\n", " return len(self.df)\n", "\n", " def __getitem__(self, idx):\n", " row = self.df.iloc[idx]\n", " premise = str(row['premise'])\n", " hypothesis = str(row['hypothesis'])\n", " encoding = self.tokenizer(\n", " premise,\n", " hypothesis,\n", " truncation=True,\n", " padding='max_length',\n", " max_length=self.max_length,\n", " return_tensors='pt'\n", " )\n", " return {\n", " 'input_ids': encoding['input_ids'].squeeze(0),\n", " 'attention_mask': encoding['attention_mask'].squeeze(0)\n", " }\n", "\n", "test_dataset = TestDataset(test_df, tokenizer, max_length)\n", "test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)\n", "\n", "# 预测\n", "all_preds = []\n", "with torch.no_grad():\n", " for batch in tqdm(test_loader, desc=\"Predicting\"):\n", " input_ids = batch['input_ids'].to(device)\n", " attention_mask = batch['attention_mask'].to(device)\n", " outputs = model(input_ids, attention_mask=attention_mask)\n", " logits = outputs.logits\n", " preds = torch.argmax(logits, dim=1)\n", " all_preds.extend(preds.cpu().numpy())\n", "\n", "# 生成提交文件\n", "submission = pd.DataFrame({\n", " 'id': test_df['id'],\n", " 'label': all_preds\n", "})\n", "submission.to_csv('submission.csv', index=False)\n", "print(\"Submission saved!\")" ], "id": "df17fe10b2f36fc5", "outputs": [], "execution_count": null } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }