vickasa commited on
Commit
c62e581
·
verified ·
1 Parent(s): 97ba43a

full fine tuning code

Browse files
Fully Fine Tuning BERT for QA.ipynb ADDED
@@ -0,0 +1,497 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "66bc0f06",
6
+ "metadata": {},
7
+ "source": [
8
+ "## Install Dependencies"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": 1,
14
+ "id": "c214dba3-9553-4668-8582-b5edb7c13492",
15
+ "metadata": {},
16
+ "outputs": [
17
+ {
18
+ "name": "stdout",
19
+ "output_type": "stream",
20
+ "text": [
21
+ "Collecting transformers\n",
22
+ " Downloading transformers-4.49.0-py3-none-any.whl.metadata (44 kB)\n",
23
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m44.0/44.0 kB\u001b[0m \u001b[31m968.5 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
24
+ "\u001b[?25hCollecting datasets\n",
25
+ " Downloading datasets-3.3.2-py3-none-any.whl.metadata (19 kB)\n",
26
+ "Collecting peft\n",
27
+ " Downloading peft-0.14.0-py3-none-any.whl.metadata (13 kB)\n",
28
+ "Collecting accelerate\n",
29
+ " Downloading accelerate-1.4.0-py3-none-any.whl.metadata (19 kB)\n",
30
+ "Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (2.1.0+cu118)\n",
31
+ "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from transformers) (3.9.0)\n",
32
+ "Collecting huggingface-hub<1.0,>=0.26.0 (from transformers)\n",
33
+ " Downloading huggingface_hub-0.29.1-py3-none-any.whl.metadata (13 kB)\n",
34
+ "Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from transformers) (1.24.1)\n",
35
+ "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from transformers) (23.2)\n",
36
+ "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from transformers) (6.0.1)\n",
37
+ "Collecting regex!=2019.12.17 (from transformers)\n",
38
+ " Downloading regex-2024.11.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (40 kB)\n",
39
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m40.5/40.5 kB\u001b[0m \u001b[31m4.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
40
+ "\u001b[?25hRequirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from transformers) (2.31.0)\n",
41
+ "Collecting tokenizers<0.22,>=0.21 (from transformers)\n",
42
+ " Downloading tokenizers-0.21.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.7 kB)\n",
43
+ "Collecting safetensors>=0.4.1 (from transformers)\n",
44
+ " Downloading safetensors-0.5.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (3.8 kB)\n",
45
+ "Collecting tqdm>=4.27 (from transformers)\n",
46
+ " Downloading tqdm-4.67.1-py3-none-any.whl.metadata (57 kB)\n",
47
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m57.7/57.7 kB\u001b[0m \u001b[31m4.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
48
+ "\u001b[?25hCollecting pyarrow>=15.0.0 (from datasets)\n",
49
+ " Downloading pyarrow-19.0.1-cp310-cp310-manylinux_2_28_x86_64.whl.metadata (3.3 kB)\n",
50
+ "Collecting dill<0.3.9,>=0.3.0 (from datasets)\n",
51
+ " Downloading dill-0.3.8-py3-none-any.whl.metadata (10 kB)\n",
52
+ "Collecting pandas (from datasets)\n",
53
+ " Downloading pandas-2.2.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (89 kB)\n",
54
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m89.9/89.9 kB\u001b[0m \u001b[31m6.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
55
+ "\u001b[?25hCollecting requests (from transformers)\n",
56
+ " Downloading requests-2.32.3-py3-none-any.whl.metadata (4.6 kB)\n",
57
+ "Collecting xxhash (from datasets)\n",
58
+ " Downloading xxhash-3.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (12 kB)\n",
59
+ "Collecting multiprocess<0.70.17 (from datasets)\n",
60
+ " Downloading multiprocess-0.70.16-py310-none-any.whl.metadata (7.2 kB)\n",
61
+ "Requirement already satisfied: fsspec<=2024.12.0,>=2023.1.0 in /usr/local/lib/python3.10/dist-packages (from fsspec[http]<=2024.12.0,>=2023.1.0->datasets) (2023.4.0)\n",
62
+ "Collecting aiohttp (from datasets)\n",
63
+ " Downloading aiohttp-3.11.13-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (7.7 kB)\n",
64
+ "Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from peft) (5.9.6)\n",
65
+ "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.10/dist-packages (from torch) (4.4.0)\n",
66
+ "Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch) (1.12)\n",
67
+ "Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch) (3.0)\n",
68
+ "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch) (3.1.2)\n",
69
+ "Requirement already satisfied: triton==2.1.0 in /usr/local/lib/python3.10/dist-packages (from torch) (2.1.0)\n",
70
+ "Collecting aiohappyeyeballs>=2.3.0 (from aiohttp->datasets)\n",
71
+ " Downloading aiohappyeyeballs-2.4.6-py3-none-any.whl.metadata (5.9 kB)\n",
72
+ "Collecting aiosignal>=1.1.2 (from aiohttp->datasets)\n",
73
+ " Downloading aiosignal-1.3.2-py2.py3-none-any.whl.metadata (3.8 kB)\n",
74
+ "Collecting async-timeout<6.0,>=4.0 (from aiohttp->datasets)\n",
75
+ " Downloading async_timeout-5.0.1-py3-none-any.whl.metadata (5.1 kB)\n",
76
+ "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (23.1.0)\n",
77
+ "Collecting frozenlist>=1.1.1 (from aiohttp->datasets)\n",
78
+ " Downloading frozenlist-1.5.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (13 kB)\n",
79
+ "Collecting multidict<7.0,>=4.5 (from aiohttp->datasets)\n",
80
+ " Downloading multidict-6.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (5.0 kB)\n",
81
+ "Collecting propcache>=0.2.0 (from aiohttp->datasets)\n",
82
+ " Downloading propcache-0.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (10 kB)\n",
83
+ "Collecting yarl<2.0,>=1.17.0 (from aiohttp->datasets)\n",
84
+ " Downloading yarl-1.18.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (69 kB)\n",
85
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m69.2/69.2 kB\u001b[0m \u001b[31m10.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
86
+ "\u001b[?25hINFO: pip is looking at multiple versions of huggingface-hub to determine which version is compatible with other requirements. This could take a while.\n",
87
+ "Collecting huggingface-hub<1.0,>=0.26.0 (from transformers)\n",
88
+ " Downloading huggingface_hub-0.29.0-py3-none-any.whl.metadata (13 kB)\n",
89
+ " Downloading huggingface_hub-0.28.1-py3-none-any.whl.metadata (13 kB)\n",
90
+ " Downloading huggingface_hub-0.28.0-py3-none-any.whl.metadata (13 kB)\n",
91
+ " Downloading huggingface_hub-0.27.1-py3-none-any.whl.metadata (13 kB)\n",
92
+ " Downloading huggingface_hub-0.27.0-py3-none-any.whl.metadata (13 kB)\n",
93
+ " Downloading huggingface_hub-0.26.5-py3-none-any.whl.metadata (13 kB)\n",
94
+ " Downloading huggingface_hub-0.26.3-py3-none-any.whl.metadata (13 kB)\n",
95
+ "INFO: pip is still looking at multiple versions of huggingface-hub to determine which version is compatible with other requirements. This could take a while.\n",
96
+ " Downloading huggingface_hub-0.26.2-py3-none-any.whl.metadata (13 kB)\n",
97
+ " Downloading huggingface_hub-0.26.1-py3-none-any.whl.metadata (13 kB)\n",
98
+ " Downloading huggingface_hub-0.26.0-py3-none-any.whl.metadata (13 kB)\n",
99
+ "Collecting fsspec[http]<=2024.12.0,>=2023.1.0 (from datasets)\n",
100
+ " Downloading fsspec-2024.12.0-py3-none-any.whl.metadata (11 kB)\n",
101
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (2.1.1)\n",
102
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (3.4)\n",
103
+ "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (1.26.13)\n",
104
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (2022.12.7)\n",
105
+ "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch) (2.1.2)\n",
106
+ "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2.8.2)\n",
107
+ "Collecting pytz>=2020.1 (from pandas->datasets)\n",
108
+ " Downloading pytz-2025.1-py2.py3-none-any.whl.metadata (22 kB)\n",
109
+ "Collecting tzdata>=2022.7 (from pandas->datasets)\n",
110
+ " Downloading tzdata-2025.1-py2.py3-none-any.whl.metadata (1.4 kB)\n",
111
+ "Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch) (1.3.0)\n",
112
+ "Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.8.2->pandas->datasets) (1.16.0)\n",
113
+ "Downloading transformers-4.49.0-py3-none-any.whl (10.0 MB)\n",
114
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m10.0/10.0 MB\u001b[0m \u001b[31m46.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m \u001b[36m0:00:01\u001b[0m\n",
115
+ "\u001b[?25hDownloading datasets-3.3.2-py3-none-any.whl (485 kB)\n",
116
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m485.4/485.4 kB\u001b[0m \u001b[31m157.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
117
+ "\u001b[?25hDownloading peft-0.14.0-py3-none-any.whl (374 kB)\n",
118
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m374.8/374.8 kB\u001b[0m \u001b[31m128.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
119
+ "\u001b[?25hDownloading accelerate-1.4.0-py3-none-any.whl (342 kB)\n",
120
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m342.1/342.1 kB\u001b[0m \u001b[31m123.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
121
+ "\u001b[?25hDownloading dill-0.3.8-py3-none-any.whl (116 kB)\n",
122
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m116.3/116.3 kB\u001b[0m \u001b[31m54.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
123
+ "\u001b[?25hDownloading fsspec-2024.12.0-py3-none-any.whl (183 kB)\n",
124
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m183.9/183.9 kB\u001b[0m \u001b[31m79.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
125
+ "\u001b[?25hDownloading huggingface_hub-0.29.1-py3-none-any.whl (468 kB)\n",
126
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m468.0/468.0 kB\u001b[0m \u001b[31m156.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
127
+ "\u001b[?25hDownloading aiohttp-3.11.13-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB)\n",
128
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m251.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
129
+ "\u001b[?25hDownloading multiprocess-0.70.16-py310-none-any.whl (134 kB)\n",
130
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m134.8/134.8 kB\u001b[0m \u001b[31m65.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
131
+ "\u001b[?25hDownloading pyarrow-19.0.1-cp310-cp310-manylinux_2_28_x86_64.whl (42.1 MB)\n",
132
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m42.1/42.1 MB\u001b[0m \u001b[31m83.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0mta \u001b[36m0:00:01\u001b[0m\n",
133
+ "\u001b[?25hDownloading regex-2024.11.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (781 kB)\n",
134
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m781.7/781.7 kB\u001b[0m \u001b[31m179.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
135
+ "\u001b[?25hDownloading requests-2.32.3-py3-none-any.whl (64 kB)\n",
136
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m64.9/64.9 kB\u001b[0m \u001b[31m31.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
137
+ "\u001b[?25hDownloading safetensors-0.5.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (471 kB)\n",
138
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m471.6/471.6 kB\u001b[0m \u001b[31m164.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
139
+ "\u001b[?25hDownloading tokenizers-0.21.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB)\n",
140
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.0/3.0 MB\u001b[0m \u001b[31m225.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
141
+ "\u001b[?25hDownloading tqdm-4.67.1-py3-none-any.whl (78 kB)\n",
142
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m78.5/78.5 kB\u001b[0m \u001b[31m38.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
143
+ "\u001b[?25hDownloading pandas-2.2.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.1 MB)\n",
144
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.1/13.1 MB\u001b[0m \u001b[31m72.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
145
+ "\u001b[?25hDownloading xxhash-3.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (194 kB)\n",
146
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m194.1/194.1 kB\u001b[0m \u001b[31m90.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
147
+ "\u001b[?25hDownloading aiohappyeyeballs-2.4.6-py3-none-any.whl (14 kB)\n",
148
+ "Downloading aiosignal-1.3.2-py2.py3-none-any.whl (7.6 kB)\n",
149
+ "Downloading async_timeout-5.0.1-py3-none-any.whl (6.2 kB)\n",
150
+ "Downloading frozenlist-1.5.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (241 kB)\n",
151
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m241.9/241.9 kB\u001b[0m \u001b[31m89.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
152
+ "\u001b[?25hDownloading multidict-6.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (124 kB)\n",
153
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m124.6/124.6 kB\u001b[0m \u001b[31m61.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
154
+ "\u001b[?25hDownloading propcache-0.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (205 kB)\n",
155
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m205.4/205.4 kB\u001b[0m \u001b[31m92.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
156
+ "\u001b[?25hDownloading pytz-2025.1-py2.py3-none-any.whl (507 kB)\n",
157
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m507.9/507.9 kB\u001b[0m \u001b[31m168.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
158
+ "\u001b[?25hDownloading tzdata-2025.1-py2.py3-none-any.whl (346 kB)\n",
159
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m346.8/346.8 kB\u001b[0m \u001b[31m130.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
160
+ "\u001b[?25hDownloading yarl-1.18.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (319 kB)\n",
161
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m319.7/319.7 kB\u001b[0m \u001b[31m106.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
162
+ "\u001b[?25hInstalling collected packages: pytz, xxhash, tzdata, tqdm, safetensors, requests, regex, pyarrow, propcache, multidict, fsspec, frozenlist, dill, async-timeout, aiohappyeyeballs, yarl, pandas, multiprocess, huggingface-hub, aiosignal, tokenizers, aiohttp, accelerate, transformers, peft, datasets\n",
163
+ " Attempting uninstall: requests\n",
164
+ " Found existing installation: requests 2.31.0\n",
165
+ " Uninstalling requests-2.31.0:\n",
166
+ " Successfully uninstalled requests-2.31.0\n",
167
+ " Attempting uninstall: fsspec\n",
168
+ " Found existing installation: fsspec 2023.4.0\n",
169
+ " Uninstalling fsspec-2023.4.0:\n",
170
+ " Successfully uninstalled fsspec-2023.4.0\n",
171
+ "Successfully installed accelerate-1.4.0 aiohappyeyeballs-2.4.6 aiohttp-3.11.13 aiosignal-1.3.2 async-timeout-5.0.1 datasets-3.3.2 dill-0.3.8 frozenlist-1.5.0 fsspec-2024.12.0 huggingface-hub-0.29.1 multidict-6.1.0 multiprocess-0.70.16 pandas-2.2.3 peft-0.14.0 propcache-0.3.0 pyarrow-19.0.1 pytz-2025.1 regex-2024.11.6 requests-2.32.3 safetensors-0.5.3 tokenizers-0.21.0 tqdm-4.67.1 transformers-4.49.0 tzdata-2025.1 xxhash-3.5.0 yarl-1.18.3\n",
172
+ "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
173
+ "\u001b[0m\n",
174
+ "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.0.1\u001b[0m\n",
175
+ "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython -m pip install --upgrade pip\u001b[0m\n"
176
+ ]
177
+ }
178
+ ],
179
+ "source": [
180
+ "%%capture\n",
181
+ "\n",
182
+ "%pip install transformers datasets peft accelerate torch"
183
+ ]
184
+ },
185
+ {
186
+ "cell_type": "markdown",
187
+ "id": "367aeaca",
188
+ "metadata": {},
189
+ "source": [
190
+ "## Import Modules"
191
+ ]
192
+ },
193
+ {
194
+ "cell_type": "code",
195
+ "execution_count": null,
196
+ "id": "7df95981",
197
+ "metadata": {},
198
+ "outputs": [],
199
+ "source": [
200
+ "import json\n",
201
+ "import torch\n",
202
+ "from transformers import BertTokenizerFast, BertForQuestionAnswering, Trainer, TrainingArguments, pipeline"
203
+ ]
204
+ },
205
+ {
206
+ "cell_type": "markdown",
207
+ "id": "1e991901",
208
+ "metadata": {},
209
+ "source": [
210
+ "## Pre-Process The Data"
211
+ ]
212
+ },
213
+ {
214
+ "cell_type": "code",
215
+ "execution_count": 3,
216
+ "id": "282f340e-f1ba-4933-af49-0642863c01e1",
217
+ "metadata": {},
218
+ "outputs": [
219
+ {
220
+ "name": "stderr",
221
+ "output_type": "stream",
222
+ "text": [
223
+ "Some weights of BertForQuestionAnswering were not initialized from the model checkpoint at bert-base-uncased and are newly initialized: ['qa_outputs.bias', 'qa_outputs.weight']\n",
224
+ "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n",
225
+ "/usr/local/lib/python3.10/dist-packages/transformers/training_args.py:1594: FutureWarning: `evaluation_strategy` is deprecated and will be removed in version 4.46 of 🤗 Transformers. Use `eval_strategy` instead\n",
226
+ " warnings.warn(\n",
227
+ "/tmp/ipykernel_373/3878564307.py:83: FutureWarning: `tokenizer` is deprecated and will be removed in version 5.0.0 for `Trainer.__init__`. Use `processing_class` instead.\n",
228
+ " trainer = Trainer(\n"
229
+ ]
230
+ },
231
+ {
232
+ "data": {
233
+ "text/html": [
234
+ "\n",
235
+ " <div>\n",
236
+ " \n",
237
+ " <progress value='585' max='585' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
238
+ " [585/585 02:00, Epoch 3/3]\n",
239
+ " </div>\n",
240
+ " <table border=\"1\" class=\"dataframe\">\n",
241
+ " <thead>\n",
242
+ " <tr style=\"text-align: left;\">\n",
243
+ " <th>Step</th>\n",
244
+ " <th>Training Loss</th>\n",
245
+ " </tr>\n",
246
+ " </thead>\n",
247
+ " <tbody>\n",
248
+ " <tr>\n",
249
+ " <td>500</td>\n",
250
+ " <td>0.309300</td>\n",
251
+ " </tr>\n",
252
+ " </tbody>\n",
253
+ "</table><p>"
254
+ ],
255
+ "text/plain": [
256
+ "<IPython.core.display.HTML object>"
257
+ ]
258
+ },
259
+ "metadata": {},
260
+ "output_type": "display_data"
261
+ },
262
+ {
263
+ "data": {
264
+ "text/plain": [
265
+ "TrainOutput(global_step=585, training_loss=0.26500008636050754, metrics={'train_runtime': 120.4137, 'train_samples_per_second': 77.632, 'train_steps_per_second': 4.858, 'total_flos': 2442602081968128.0, 'train_loss': 0.26500008636050754, 'epoch': 3.0})"
266
+ ]
267
+ },
268
+ "execution_count": 3,
269
+ "metadata": {},
270
+ "output_type": "execute_result"
271
+ }
272
+ ],
273
+ "source": [
274
+ "# Initialize the tokenizer\n",
275
+ "tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased')\n",
276
+ "\n",
277
+ "def preprocess_data(data):\n",
278
+ " tokenized_data = []\n",
279
+ " for item in data:\n",
280
+ " # Tokenize the question and context together with offset mapping\n",
281
+ " inputs = tokenizer(\n",
282
+ " item['question'],\n",
283
+ " item['context'],\n",
284
+ " max_length=512,\n",
285
+ " truncation=True,\n",
286
+ " padding='max_length',\n",
287
+ " return_offsets_mapping=True, # This is crucial\n",
288
+ " return_tensors='pt'\n",
289
+ " )\n",
290
+ "\n",
291
+ " offset_mapping = inputs.pop('offset_mapping') # Extract offset mapping\n",
292
+ " input_ids = inputs['input_ids'].squeeze() # Remove batch dimension\n",
293
+ "\n",
294
+ " # Convert character indices to token indices for the answer\n",
295
+ " start_char = item['answer_start_index']\n",
296
+ " end_char = item['answer_end_index']\n",
297
+ "\n",
298
+ " start_token_idx, end_token_idx = None, None\n",
299
+ "\n",
300
+ " for i, (start, end) in enumerate(offset_mapping.squeeze().tolist()):\n",
301
+ " if start_char >= start and start_char < end:\n",
302
+ " start_token_idx = i\n",
303
+ " if end_char > start and end_char <= end:\n",
304
+ " end_token_idx = i\n",
305
+ " break # Stop once the end position is found\n",
306
+ "\n",
307
+ " # Ensure valid token indices\n",
308
+ " if start_token_idx is None or end_token_idx is None:\n",
309
+ " continue # Skip this example if indices are not found\n",
310
+ "\n",
311
+ " tokenized_data.append({\n",
312
+ " 'input_ids': input_ids,\n",
313
+ " 'attention_mask': inputs['attention_mask'].squeeze(),\n",
314
+ " 'token_type_ids': inputs['token_type_ids'].squeeze(),\n",
315
+ " 'start_positions': torch.tensor([start_token_idx]),\n",
316
+ " 'end_positions': torch.tensor([end_token_idx])\n",
317
+ " })\n",
318
+ " \n",
319
+ " return tokenized_data\n"
320
+ ]
321
+ },
322
+ {
323
+ "cell_type": "markdown",
324
+ "id": "bd7c66f6",
325
+ "metadata": {},
326
+ "source": [
327
+ "## Load The Dataset"
328
+ ]
329
+ },
330
+ {
331
+ "cell_type": "code",
332
+ "execution_count": null,
333
+ "id": "b1238434",
334
+ "metadata": {},
335
+ "outputs": [],
336
+ "source": [
337
+ "# Load dataset\n",
338
+ "def load_dataset(file_path):\n",
339
+ " with open(file_path, 'r', encoding='utf-8') as file:\n",
340
+ " data = json.load(file)\n",
341
+ " return data\n",
342
+ "\n",
343
+ "# Load your dataset\n",
344
+ "data = load_dataset('qa_london_data.json')\n",
345
+ "\n",
346
+ "# Preprocess the data\n",
347
+ "tokenized_datasets = preprocess_data(data)"
348
+ ]
349
+ },
350
+ {
351
+ "cell_type": "markdown",
352
+ "id": "9a0359a5",
353
+ "metadata": {},
354
+ "source": [
355
+ "## Train the Model"
356
+ ]
357
+ },
358
+ {
359
+ "cell_type": "code",
360
+ "execution_count": null,
361
+ "id": "cceb106c",
362
+ "metadata": {},
363
+ "outputs": [],
364
+ "source": [
365
+ "# Prepare model\n",
366
+ "model = BertForQuestionAnswering.from_pretrained('bert-base-uncased')\n",
367
+ "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
368
+ "model.to(device)\n",
369
+ "\n",
370
+ "# Training arguments\n",
371
+ "training_args = TrainingArguments(\n",
372
+ " output_dir=\"./results\",\n",
373
+ " evaluation_strategy=\"no\", # Disable evaluation\n",
374
+ " learning_rate=2e-5,\n",
375
+ " per_device_train_batch_size=16,\n",
376
+ " per_device_eval_batch_size=16,\n",
377
+ " num_train_epochs=3,\n",
378
+ " weight_decay=0.01,\n",
379
+ ")\n",
380
+ "\n",
381
+ "trainer = Trainer(\n",
382
+ " model=model,\n",
383
+ " args=training_args,\n",
384
+ " train_dataset=tokenized_datasets, # Only training dataset\n",
385
+ " tokenizer=tokenizer,\n",
386
+ ")\n",
387
+ "\n",
388
+ "trainer.train()\n"
389
+ ]
390
+ },
391
+ {
392
+ "cell_type": "markdown",
393
+ "id": "91b95765",
394
+ "metadata": {},
395
+ "source": [
396
+ "## Save The Model"
397
+ ]
398
+ },
399
+ {
400
+ "cell_type": "code",
401
+ "execution_count": 4,
402
+ "id": "49a99e11-a3de-42a7-a245-a352f1e70bab",
403
+ "metadata": {},
404
+ "outputs": [
405
+ {
406
+ "data": {
407
+ "text/plain": [
408
+ "('./fine_tuned_bert/tokenizer_config.json',\n",
409
+ " './fine_tuned_bert/special_tokens_map.json',\n",
410
+ " './fine_tuned_bert/vocab.txt',\n",
411
+ " './fine_tuned_bert/added_tokens.json',\n",
412
+ " './fine_tuned_bert/tokenizer.json')"
413
+ ]
414
+ },
415
+ "execution_count": 4,
416
+ "metadata": {},
417
+ "output_type": "execute_result"
418
+ }
419
+ ],
420
+ "source": [
421
+ "model.save_pretrained(\"./fine_tuned_bert\")\n",
422
+ "tokenizer.save_pretrained(\"./fine_tuned_bert\")"
423
+ ]
424
+ },
425
+ {
426
+ "cell_type": "markdown",
427
+ "id": "79a2aeca",
428
+ "metadata": {},
429
+ "source": [
430
+ "## Test The Model"
431
+ ]
432
+ },
433
+ {
434
+ "cell_type": "code",
435
+ "execution_count": 5,
436
+ "id": "aa2ebe37-fbc6-4093-bbdb-497b8ac50b89",
437
+ "metadata": {},
438
+ "outputs": [
439
+ {
440
+ "name": "stderr",
441
+ "output_type": "stream",
442
+ "text": [
443
+ "Device set to use cuda:0\n"
444
+ ]
445
+ },
446
+ {
447
+ "name": "stdout",
448
+ "output_type": "stream",
449
+ "text": [
450
+ "{'score': 0.9995142817497253, 'start': 67, 'end': 78, 'answer': 'guided tour'}\n"
451
+ ]
452
+ },
453
+ {
454
+ "name": "stderr",
455
+ "output_type": "stream",
456
+ "text": [
457
+ "/usr/local/lib/python3.10/dist-packages/transformers/pipelines/question_answering.py:391: FutureWarning: Passing a list of SQuAD examples to the pipeline is deprecated and will be removed in v5. Inputs should be passed using the `question` and `context` keyword arguments instead.\n",
458
+ " warnings.warn(\n"
459
+ ]
460
+ }
461
+ ],
462
+ "source": [
463
+ "# Load the fine-tuned model\n",
464
+ "qa_pipeline = pipeline(\"question-answering\", model=\"./fine_tuned_bert\", tokenizer=\"./fine_tuned_bert\")\n",
465
+ "\n",
466
+ "# Test on a sample question\n",
467
+ "result = qa_pipeline({\n",
468
+ " \"question\": \"To which category does the Christmas Lights by Night Open-Top Bus Tour belong?\",\n",
469
+ " \"context\": \"Christmas Lights by Night Open-Top Bus Tour is an activity of type guided tour. It lasts 1.5 hours...\"\n",
470
+ "})\n",
471
+ "\n",
472
+ "print(result)\n"
473
+ ]
474
+ }
475
+ ],
476
+ "metadata": {
477
+ "kernelspec": {
478
+ "display_name": "Python 3 (ipykernel)",
479
+ "language": "python",
480
+ "name": "python3"
481
+ },
482
+ "language_info": {
483
+ "codemirror_mode": {
484
+ "name": "ipython",
485
+ "version": 3
486
+ },
487
+ "file_extension": ".py",
488
+ "mimetype": "text/x-python",
489
+ "name": "python",
490
+ "nbconvert_exporter": "python",
491
+ "pygments_lexer": "ipython3",
492
+ "version": "3.10.12"
493
+ }
494
+ },
495
+ "nbformat": 4,
496
+ "nbformat_minor": 5
497
+ }
qa_london_data.json ADDED
The diff for this file is too large to render. See raw diff