S-MurilloG commited on
Commit
86b9d1c
1 Parent(s): 72d556e

Fine-tuning model

Browse files
Files changed (1) hide show
  1. CARSE_02_FineTuning.ipynb +193 -4
CARSE_02_FineTuning.ipynb CHANGED
@@ -50,20 +50,209 @@
50
  "cell_type": "markdown",
51
  "metadata": {},
52
  "source": [
53
- "### Subimos nuestro primer file de entrenamiento"
54
  ]
55
  },
56
  {
57
  "cell_type": "code",
58
- "execution_count": null,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  "metadata": {},
60
  "outputs": [],
61
  "source": [
62
- "#upload_file_1 = client.files.create(\n",
63
- " file=open(\"Training_Data/Training_Prompts_1.jsonl\", \"rb\"),\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  " purpose=\"fine-tune\"\n",
65
  ")"
66
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  }
68
  ],
69
  "metadata": {
 
50
  "cell_type": "markdown",
51
  "metadata": {},
52
  "source": [
53
+ "### Separamos en Training y Validation cada file"
54
  ]
55
  },
56
  {
57
  "cell_type": "code",
58
+ "execution_count": 16,
59
+ "metadata": {},
60
+ "outputs": [],
61
+ "source": [
62
+ "# Función para separar la data\n",
63
+ "def dividir_training_validation(ruta_archivo, proporcion_training=0.8):\n",
64
+ " # Leer todas las líneas del archivo\n",
65
+ " with open(ruta_archivo, 'r', encoding='utf-8') as file:\n",
66
+ " lineas = file.readlines()\n",
67
+ "\n",
68
+ " # Calcular el punto de corte para el conjunto de entrenamiento\n",
69
+ " corte = int(len(lineas) * proporcion_training)\n",
70
+ "\n",
71
+ " # Dividir las líneas en conjuntos de entrenamiento y validación\n",
72
+ " lineas_training = lineas[:corte]\n",
73
+ " lineas_validation = lineas[corte:]\n",
74
+ "\n",
75
+ " # Crear archivos para training y validation\n",
76
+ " ruta_archivo_base = ruta_archivo.replace('.jsonl', '')\n",
77
+ " archivo_training = f'{ruta_archivo_base}_train.jsonl'\n",
78
+ " archivo_validation = f'{ruta_archivo_base}_val.jsonl'\n",
79
+ "\n",
80
+ " # Escribir el conjunto de entrenamiento\n",
81
+ " with open(archivo_training, 'w', encoding='utf-8') as file:\n",
82
+ " file.writelines(lineas_training)\n",
83
+ "\n",
84
+ " # Escribir el conjunto de validación\n",
85
+ " with open(archivo_validation, 'w', encoding='utf-8') as file:\n",
86
+ " file.writelines(lineas_validation)"
87
+ ]
88
+ },
89
+ {
90
+ "cell_type": "code",
91
+ "execution_count": 17,
92
  "metadata": {},
93
  "outputs": [],
94
  "source": [
95
+ "# Llamadas a la función para crear la separación\n",
96
+ "\"\"\"\n",
97
+ "dividir_training_validation('Training_Data/Training_Prompts_1.jsonl')\n",
98
+ "dividir_training_validation('Training_Data/Training_Prompts_2.jsonl')\n",
99
+ "dividir_training_validation('Training_Data/Training_Prompts_3.jsonl')\n",
100
+ "\"\"\""
101
+ ]
102
+ },
103
+ {
104
+ "cell_type": "code",
105
+ "execution_count": 18,
106
+ "metadata": {},
107
+ "outputs": [],
108
+ "source": [
109
+ "# Eliminamos los archivos \"originales\"\n",
110
+ "\"\"\"\n",
111
+ "os.remove('Training_Data/Training_Prompts_1.jsonl')\n",
112
+ "os.remove('Training_Data/Training_Prompts_2.jsonl')\n",
113
+ "os.remove('Training_Data/Training_Prompts_3.jsonl')\n",
114
+ "\"\"\""
115
+ ]
116
+ },
117
+ {
118
+ "cell_type": "markdown",
119
+ "metadata": {},
120
+ "source": [
121
+ "### Subimos files de entrenamiento y validación"
122
+ ]
123
+ },
124
+ {
125
+ "cell_type": "code",
126
+ "execution_count": 29,
127
+ "metadata": {},
128
+ "outputs": [],
129
+ "source": [
130
+ "# Para Training\n",
131
+ "upload_train_response = client.files.create(\n",
132
+ " file=open(\"Training_Data/Training_Prompts_1_train.jsonl\", \"rb\"),\n",
133
+ " purpose=\"fine-tune\"\n",
134
+ ")\n",
135
+ "\n",
136
+ "# Para Validation\n",
137
+ "upload_val_response = client.files.create(\n",
138
+ " file=open(\"Training_Data/Training_Prompts_1_val.jsonl\", \"rb\"),\n",
139
  " purpose=\"fine-tune\"\n",
140
  ")"
141
  ]
142
+ },
143
+ {
144
+ "cell_type": "code",
145
+ "execution_count": 30,
146
+ "metadata": {},
147
+ "outputs": [
148
+ {
149
+ "name": "stdout",
150
+ "output_type": "stream",
151
+ "text": [
152
+ "Training file id:\tfile-eQOE8MxF51oFiGSVT48x0vLw\n",
153
+ "Validation file id:\tfile-3LSttIrULCZUz5a4pXc3Fsk4\n"
154
+ ]
155
+ }
156
+ ],
157
+ "source": [
158
+ "train_file_id = upload_train_response.id\n",
159
+ "val_file_id = upload_val_response.id\n",
160
+ "\n",
161
+ "print(f'Training file id:\\t{train_file_id}')\n",
162
+ "print(f'Validation file id:\\t{val_file_id}')"
163
+ ]
164
+ },
165
+ {
166
+ "cell_type": "markdown",
167
+ "metadata": {},
168
+ "source": [
169
+ "# Trabajo de fine-tuning"
170
+ ]
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "execution_count": 32,
175
+ "metadata": {},
176
+ "outputs": [],
177
+ "source": [
178
+ "fine_tune_response = client.fine_tuning.jobs.create(\n",
179
+ " training_file=\"file-eQOE8MxF51oFiGSVT48x0vLw\", \n",
180
+ " validation_file=\"file-3LSttIrULCZUz5a4pXc3Fsk4\",\n",
181
+ " model=\"gpt-3.5-turbo-1106\", \n",
182
+ " suffix=\"CARSE\",\n",
183
+ " hyperparameters={\n",
184
+ " \"n_epochs\":5\n",
185
+ " }\n",
186
+ ")"
187
+ ]
188
+ },
189
+ {
190
+ "cell_type": "code",
191
+ "execution_count": 33,
192
+ "metadata": {},
193
+ "outputs": [
194
+ {
195
+ "name": "stdout",
196
+ "output_type": "stream",
197
+ "text": [
198
+ "Fine-tune id:\tftjob-oKdrQdLt4j9ijBvKuQWXl9C9\n"
199
+ ]
200
+ }
201
+ ],
202
+ "source": [
203
+ "fine_tune_id = fine_tune_response.id\n",
204
+ "\n",
205
+ "print(f'Fine-tune id:\\t{fine_tune_id}')"
206
+ ]
207
+ },
208
+ {
209
+ "cell_type": "code",
210
+ "execution_count": 35,
211
+ "metadata": {},
212
+ "outputs": [
213
+ {
214
+ "data": {
215
+ "text/plain": [
216
+ "FineTuningJob(id='ftjob-oKdrQdLt4j9ijBvKuQWXl9C9', created_at=1702185828, error=None, fine_tuned_model=None, finished_at=None, hyperparameters=Hyperparameters(n_epochs=5, batch_size=1, learning_rate_multiplier=2), model='gpt-3.5-turbo-1106', object='fine_tuning.job', organization_id='org-IXFDgE8ZZcQzb9yKJmEuFxvC', result_files=[], status='running', trained_tokens=None, training_file='file-eQOE8MxF51oFiGSVT48x0vLw', validation_file='file-3LSttIrULCZUz5a4pXc3Fsk4')"
217
+ ]
218
+ },
219
+ "execution_count": 35,
220
+ "metadata": {},
221
+ "output_type": "execute_result"
222
+ }
223
+ ],
224
+ "source": [
225
+ "# Retrieve the state of a fine-tune\n",
226
+ "client.fine_tuning.jobs.retrieve(fine_tune_id)"
227
+ ]
228
+ },
229
+ {
230
+ "cell_type": "code",
231
+ "execution_count": 36,
232
+ "metadata": {},
233
+ "outputs": [
234
+ {
235
+ "data": {
236
+ "text/plain": [
237
+ "SyncCursorPage[FineTuningJobEvent](data=[FineTuningJobEvent(id='ftevent-HJmomlGcLNxB8tYxEn2fZYge', created_at=1702186772, level='info', message='Step 471/890: training loss=1.55, validation loss=2.50', object='fine_tuning.job.event', data={'step': 471, 'train_loss': 1.5508146286010742, 'valid_loss': 2.498373487721319, 'train_mean_token_accuracy': 0.75, 'valid_mean_token_accuracy': 0.43478260869565216}, type='metrics'), FineTuningJobEvent(id='ftevent-CYGX0JMLoG4Ofjs69dz8MPk9', created_at=1702186756, level='info', message='Step 461/890: training loss=1.16, validation loss=1.79', object='fine_tuning.job.event', data={'step': 461, 'train_loss': 1.156284213066101, 'valid_loss': 1.792006492614746, 'train_mean_token_accuracy': 0.7692307829856873, 'valid_mean_token_accuracy': 0.4}, type='metrics'), FineTuningJobEvent(id='ftevent-mPG7xYX7opqQr9nlHUnn0gOM', created_at=1702186738, level='info', message='Step 451/890: training loss=1.81, validation loss=3.31', object='fine_tuning.job.event', data={'step': 451, 'train_loss': 1.806351900100708, 'valid_loss': 3.3079380989074707, 'train_mean_token_accuracy': 0.5545454621315002, 'valid_mean_token_accuracy': 0.16666666666666666}, type='metrics'), FineTuningJobEvent(id='ftevent-SZnQTTUL8QV1vD3wmIkQ9NQE', created_at=1702186720, level='info', message='Step 441/890: training loss=2.13, validation loss=1.30', object='fine_tuning.job.event', data={'step': 441, 'train_loss': 2.1261494159698486, 'valid_loss': 1.3008832931518555, 'train_mean_token_accuracy': 0.5555555820465088, 'valid_mean_token_accuracy': 0.35}, type='metrics'), FineTuningJobEvent(id='ftevent-Z70r5u6Tv1cpKIjNoz7uxnaB', created_at=1702186702, level='info', message='Step 431/890: training loss=2.31, validation loss=1.63', object='fine_tuning.job.event', data={'step': 431, 'train_loss': 2.3078231811523438, 'valid_loss': 1.6347110454852765, 'train_mean_token_accuracy': 0.5, 'valid_mean_token_accuracy': 0.23076923076923078}, type='metrics'), FineTuningJobEvent(id='ftevent-3FOJQqO26ejvt9jGJv5YtTLS', created_at=1702186686, level='info', message='Step 421/890: training loss=1.73, validation loss=2.02', object='fine_tuning.job.event', data={'step': 421, 'train_loss': 1.7264701128005981, 'valid_loss': 2.0240015983581543, 'train_mean_token_accuracy': 0.6315789222717285, 'valid_mean_token_accuracy': 0.42857142857142855}, type='metrics'), FineTuningJobEvent(id='ftevent-ErMp6gvVT4GBdGf3yFGcAScn', created_at=1702186668, level='info', message='Step 411/890: training loss=1.16, validation loss=1.91', object='fine_tuning.job.event', data={'step': 411, 'train_loss': 1.1630134582519531, 'valid_loss': 1.9137598673502605, 'train_mean_token_accuracy': 0.7058823704719543, 'valid_mean_token_accuracy': 0.5277777777777778}, type='metrics'), FineTuningJobEvent(id='ftevent-OvYkLbxmyf0IDr7RNdSZv5Qr', created_at=1702186652, level='info', message='Step 401/890: training loss=0.76, validation loss=2.10', object='fine_tuning.job.event', data={'step': 401, 'train_loss': 0.7578617334365845, 'valid_loss': 2.0966723442077635, 'train_mean_token_accuracy': 0.7272727489471436, 'valid_mean_token_accuracy': 0.25}, type='metrics'), FineTuningJobEvent(id='ftevent-BJ0JMEcNFGTfCzyXK1R5Uets', created_at=1702186634, level='info', message='Step 391/890: training loss=1.46, validation loss=2.74', object='fine_tuning.job.event', data={'step': 391, 'train_loss': 1.4621750116348267, 'valid_loss': 2.7422271728515626, 'train_mean_token_accuracy': 0.699999988079071, 'valid_mean_token_accuracy': 0.4}, type='metrics'), FineTuningJobEvent(id='ftevent-HuADV2SZtgOS5cBnyN12JnMO', created_at=1702186618, level='info', message='Step 381/890: training loss=1.10, validation loss=2.81', object='fine_tuning.job.event', data={'step': 381, 'train_loss': 1.1008758544921875, 'valid_loss': 2.8111038208007812, 'train_mean_token_accuracy': 0.875, 'valid_mean_token_accuracy': 0.1935483870967742}, type='metrics')], object='list', has_more=True)"
238
+ ]
239
+ },
240
+ "execution_count": 36,
241
+ "metadata": {},
242
+ "output_type": "execute_result"
243
+ }
244
+ ],
245
+ "source": [
246
+ "# List up to 10 events from a fine-tuning job\n",
247
+ "client.fine_tuning.jobs.list_events(fine_tuning_job_id=fine_tune_id, limit=10)"
248
+ ]
249
+ },
250
+ {
251
+ "cell_type": "code",
252
+ "execution_count": null,
253
+ "metadata": {},
254
+ "outputs": [],
255
+ "source": []
256
  }
257
  ],
258
  "metadata": {