RenatoBarreira commited on
Commit
578c4bb
·
verified ·
1 Parent(s): 50da99e

Upload BERTEVIB7BERTLARGE8DATAAUG.ipynb

Browse files
Files changed (1) hide show
  1. BERTEVIB7BERTLARGE8DATAAUG.ipynb +835 -0
BERTEVIB7BERTLARGE8DATAAUG.ipynb ADDED
@@ -0,0 +1,835 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "aa95efb4-71eb-4f3d-8799-c12831385900",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import os\n",
11
+ "import pandas as pd\n",
12
+ "import torch\n",
13
+ "from torch import nn, optim\n",
14
+ "from torch.utils.data import DataLoader, Dataset\n",
15
+ "from transformers import AutoTokenizer, AutoModel, get_linear_schedule_with_warmup\n",
16
+ "from sklearn.metrics import accuracy_score, f1_score, classification_report\n",
17
+ "from sklearn.model_selection import StratifiedKFold, train_test_split\n",
18
+ "import random\n",
19
+ "import numpy as np\n",
20
+ "import statistics"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 2,
26
+ "id": "3e6b7a75-f6d2-4fd7-968c-b16f9832d1ca",
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "#Mudanças principais:\n",
31
+ "#1400\n",
32
+ "#Modelo Bertimbau Large: Alterado o model_name para 'neuralmind/bert-large-portuguese-cased'.\n",
33
+ "\n",
34
+ "#LR= 3e-5.\n",
35
+ "\n",
36
+ "#Descongelamento das camadas: Parametrizamos o número de camadas finais do BERT a descongelar, via unfreeze_layers. Por exemplo, se definirmos unfreeze_layers=8, descongelamos as últimas 8 camadas.\n",
37
+ "\n",
38
+ "#Outros otimizadores e LR Schedulers: Mantemos o AdamW como otimizador principal, mas agora adicionamos um scheduler (get_linear_schedule_with_warmup do transformers) para ajustar a taxa de aprendizado durante o treino. Caso queira testar outro otimizador, basta substituir a linha do optimizador. Também deixamos comentado outro exemplo (SGD) para referência.\n",
39
+ "#Para testar diferentes taxas de aprendizado, basta alterar learning_rate no código.\n",
40
+ "#Para testar diferentes números de camadas a descongelar, altere unfreeze_layers.\n",
41
+ "\n",
42
+ "#4\n",
43
+ "#processo de treinamento e avaliação várias vezes (uma para cada fold).\n",
44
+ "#diminuindo épocas ou early stopping, se necessário.\n",
45
+ "#O early stopping agora é feito com base no conjunto de validação interno a cada fold.\n",
46
+ "#Esse processo é mais demorado, pois treinaremos o modelo K vezes.\n",
47
+ "#Ajuste parâmetros (como número de épocas, taxa de aprendizado, etc.) conforme necessário."
48
+ ]
49
+ },
50
+ {
51
+ "cell_type": "code",
52
+ "execution_count": 3,
53
+ "id": "e375e916-07a1-44f1-9675-8fb7eb8045f1",
54
+ "metadata": {},
55
+ "outputs": [],
56
+ "source": [
57
+ "# Semente para reprodutibilidade\n",
58
+ "seed = 42\n",
59
+ "random.seed(seed)\n",
60
+ "np.random.seed(seed)\n",
61
+ "torch.manual_seed(seed)\n",
62
+ "if torch.cuda.is_available():\n",
63
+ " torch.cuda.manual_seed_all(seed)"
64
+ ]
65
+ },
66
+ {
67
+ "cell_type": "code",
68
+ "execution_count": 4,
69
+ "id": "1b511a6a-4c56-4335-8c15-660d3d146399",
70
+ "metadata": {},
71
+ "outputs": [
72
+ {
73
+ "name": "stdout",
74
+ "output_type": "stream",
75
+ "text": [
76
+ "Usando dispositivo: cuda\n"
77
+ ]
78
+ }
79
+ ],
80
+ "source": [
81
+ "# Configurações gerais\n",
82
+ "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
83
+ "print(f'Usando dispositivo: {device}')\n",
84
+ "\n",
85
+ "model_name = 'neuralmind/bert-large-portuguese-cased' \n",
86
+ "learning_rate = 3e-5\n",
87
+ "unfreeze_layers = 4\n",
88
+ "nclasses = 2\n",
89
+ "nepochs = 5\n",
90
+ "batch_size = 8\n",
91
+ "batch_status = 32\n",
92
+ "early_stop = 2\n",
93
+ "max_length = 360\n",
94
+ "write_path = 'C:/model_xlm_roberta_cv8'\n",
95
+ "\n",
96
+ "if not os.path.exists(write_path):\n",
97
+ " os.makedirs(write_path)"
98
+ ]
99
+ },
100
+ {
101
+ "cell_type": "code",
102
+ "execution_count": 5,
103
+ "id": "3a0a71c0-667a-421f-b324-fa857999aa6f",
104
+ "metadata": {},
105
+ "outputs": [],
106
+ "source": [
107
+ "# Carregar os dados\n",
108
+ "data = pd.read_csv(\"DATAFRAME1400_augmented.csv\")"
109
+ ]
110
+ },
111
+ {
112
+ "cell_type": "code",
113
+ "execution_count": 6,
114
+ "id": "ff7f8d91-5750-45f3-9a75-1240e5401757",
115
+ "metadata": {},
116
+ "outputs": [],
117
+ "source": [
118
+ "# Dataset Customizado\n",
119
+ "class CustomDataset(Dataset):\n",
120
+ " def __init__(self, data, tokenizer, max_length):\n",
121
+ " self.data = data.reset_index(drop=True)\n",
122
+ " self.tokenizer = tokenizer\n",
123
+ " self.max_length = max_length\n",
124
+ "\n",
125
+ " def __len__(self):\n",
126
+ " return len(self.data)\n",
127
+ "\n",
128
+ " def __getitem__(self, idx):\n",
129
+ " text = self.data.iloc[idx]['text']\n",
130
+ " label = self.data.iloc[idx]['contra']\n",
131
+ " inputs = self.tokenizer(text, return_tensors='pt',\n",
132
+ " padding='max_length', truncation=True,\n",
133
+ " max_length=self.max_length)\n",
134
+ " return {key: val.squeeze(0) for key, val in inputs.items()}, torch.tensor(label)\n",
135
+ "\n",
136
+ "\n"
137
+ ]
138
+ },
139
+ {
140
+ "cell_type": "code",
141
+ "execution_count": 7,
142
+ "id": "ac08faef-e9c9-496f-b850-6295f892c4e6",
143
+ "metadata": {},
144
+ "outputs": [
145
+ {
146
+ "name": "stdout",
147
+ "output_type": "stream",
148
+ "text": [
149
+ "\n",
150
+ "=== Fold 1/5 ===\n",
151
+ "Epoch: 0 [32/539]\tLoss: 0.806810\n",
152
+ "Epoch: 0 [64/539]\tLoss: 0.893960\n",
153
+ "Epoch: 0 [96/539]\tLoss: 0.640361\n",
154
+ "Epoch: 0 [128/539]\tLoss: 0.615714\n",
155
+ "Epoch: 0 [160/539]\tLoss: 0.570503\n",
156
+ "Epoch: 0 [192/539]\tLoss: 0.630810\n",
157
+ "Epoch: 0 [224/539]\tLoss: 0.643570\n",
158
+ "Epoch: 0 [256/539]\tLoss: 0.768140\n",
159
+ "Epoch: 0 [288/539]\tLoss: 1.057127\n",
160
+ "Epoch: 0 [320/539]\tLoss: 0.569394\n",
161
+ "Epoch: 0 [352/539]\tLoss: 0.381507\n",
162
+ "Epoch: 0 [384/539]\tLoss: 0.306017\n",
163
+ "Epoch: 0 [416/539]\tLoss: 0.685640\n",
164
+ "Epoch: 0 [448/539]\tLoss: 0.419818\n",
165
+ "Epoch: 0 [480/539]\tLoss: 0.364408\n",
166
+ "Epoch: 0 [512/539]\tLoss: 0.748105\n",
167
+ "Epoch 0 - Val F1: 0.8243, Val Accuracy: 0.8246\n",
168
+ "Novo melhor modelo salvo.\n",
169
+ "Epoch: 1 [32/539]\tLoss: 0.233392\n",
170
+ "Epoch: 1 [64/539]\tLoss: 0.336354\n",
171
+ "Epoch: 1 [96/539]\tLoss: 0.421595\n",
172
+ "Epoch: 1 [128/539]\tLoss: 0.099084\n",
173
+ "Epoch: 1 [160/539]\tLoss: 0.365469\n",
174
+ "Epoch: 1 [192/539]\tLoss: 0.614397\n",
175
+ "Epoch: 1 [224/539]\tLoss: 0.651654\n",
176
+ "Epoch: 1 [256/539]\tLoss: 0.123179\n",
177
+ "Epoch: 1 [288/539]\tLoss: 0.272575\n",
178
+ "Epoch: 1 [320/539]\tLoss: 0.270919\n",
179
+ "Epoch: 1 [352/539]\tLoss: 0.199035\n",
180
+ "Epoch: 1 [384/539]\tLoss: 0.386721\n",
181
+ "Epoch: 1 [416/539]\tLoss: 0.824670\n",
182
+ "Epoch: 1 [448/539]\tLoss: 0.282997\n",
183
+ "Epoch: 1 [480/539]\tLoss: 0.184333\n",
184
+ "Epoch: 1 [512/539]\tLoss: 0.038649\n",
185
+ "Epoch 1 - Val F1: 0.8559, Val Accuracy: 0.8559\n",
186
+ "Novo melhor modelo salvo.\n",
187
+ "Epoch: 2 [32/539]\tLoss: 0.168970\n",
188
+ "Epoch: 2 [64/539]\tLoss: 0.351753\n",
189
+ "Epoch: 2 [96/539]\tLoss: 0.034803\n",
190
+ "Epoch: 2 [128/539]\tLoss: 0.011378\n",
191
+ "Epoch: 2 [160/539]\tLoss: 0.508281\n",
192
+ "Epoch: 2 [192/539]\tLoss: 0.069164\n",
193
+ "Epoch: 2 [224/539]\tLoss: 0.061568\n",
194
+ "Epoch: 2 [256/539]\tLoss: 0.309656\n",
195
+ "Epoch: 2 [288/539]\tLoss: 0.024734\n",
196
+ "Epoch: 2 [320/539]\tLoss: 0.132665\n",
197
+ "Epoch: 2 [352/539]\tLoss: 0.025687\n",
198
+ "Epoch: 2 [384/539]\tLoss: 0.571691\n",
199
+ "Epoch: 2 [416/539]\tLoss: 0.420970\n",
200
+ "Epoch: 2 [448/539]\tLoss: 0.036176\n",
201
+ "Epoch: 2 [480/539]\tLoss: 0.161503\n",
202
+ "Epoch: 2 [512/539]\tLoss: 0.021420\n",
203
+ "Epoch 2 - Val F1: 0.9186, Val Accuracy: 0.9186\n",
204
+ "Novo melhor modelo salvo.\n",
205
+ "Epoch: 3 [32/539]\tLoss: 0.060905\n",
206
+ "Epoch: 3 [64/539]\tLoss: 0.033918\n",
207
+ "Epoch: 3 [96/539]\tLoss: 0.047984\n",
208
+ "Epoch: 3 [128/539]\tLoss: 0.008567\n",
209
+ "Epoch: 3 [160/539]\tLoss: 0.057391\n",
210
+ "Epoch: 3 [192/539]\tLoss: 0.028192\n",
211
+ "Epoch: 3 [224/539]\tLoss: 0.087906\n",
212
+ "Epoch: 3 [256/539]\tLoss: 0.048106\n",
213
+ "Epoch: 3 [288/539]\tLoss: 0.020358\n",
214
+ "Epoch: 3 [320/539]\tLoss: 0.018573\n",
215
+ "Epoch: 3 [352/539]\tLoss: 0.004970\n",
216
+ "Epoch: 3 [384/539]\tLoss: 0.144033\n",
217
+ "Epoch: 3 [416/539]\tLoss: 0.081381\n",
218
+ "Epoch: 3 [448/539]\tLoss: 0.008459\n",
219
+ "Epoch: 3 [480/539]\tLoss: 0.298734\n",
220
+ "Epoch: 3 [512/539]\tLoss: 0.032544\n",
221
+ "Epoch 3 - Val F1: 0.9207, Val Accuracy: 0.9207\n",
222
+ "Novo melhor modelo salvo.\n",
223
+ "Epoch: 4 [32/539]\tLoss: 0.011373\n",
224
+ "Epoch: 4 [64/539]\tLoss: 0.010223\n",
225
+ "Epoch: 4 [96/539]\tLoss: 0.008893\n",
226
+ "Epoch: 4 [128/539]\tLoss: 0.051655\n",
227
+ "Epoch: 4 [160/539]\tLoss: 0.060906\n",
228
+ "Epoch: 4 [192/539]\tLoss: 0.005525\n",
229
+ "Epoch: 4 [224/539]\tLoss: 0.105270\n",
230
+ "Epoch: 4 [256/539]\tLoss: 0.003194\n",
231
+ "Epoch: 4 [288/539]\tLoss: 0.011297\n",
232
+ "Epoch: 4 [320/539]\tLoss: 0.430329\n",
233
+ "Epoch: 4 [352/539]\tLoss: 0.007904\n",
234
+ "Epoch: 4 [384/539]\tLoss: 0.029006\n",
235
+ "Epoch: 4 [416/539]\tLoss: 0.005053\n",
236
+ "Epoch: 4 [448/539]\tLoss: 0.016824\n",
237
+ "Epoch: 4 [480/539]\tLoss: 0.021264\n",
238
+ "Epoch: 4 [512/539]\tLoss: 0.079554\n",
239
+ "Epoch 4 - Val F1: 0.9332, Val Accuracy: 0.9332\n",
240
+ "Novo melhor modelo salvo.\n",
241
+ "Desempenho no conjunto de teste desta dobra:\n",
242
+ " precision recall f1-score support\n",
243
+ "\n",
244
+ " 0 0.92 0.95 0.93 627\n",
245
+ " 1 0.94 0.91 0.92 570\n",
246
+ "\n",
247
+ " accuracy 0.93 1197\n",
248
+ " macro avg 0.93 0.93 0.93 1197\n",
249
+ "weighted avg 0.93 0.93 0.93 1197\n",
250
+ "\n",
251
+ "F1 (teste): 0.9289, Accuracy (teste): 0.9290\n",
252
+ "\n",
253
+ "=== Fold 2/5 ===\n",
254
+ "Epoch: 0 [32/539]\tLoss: 0.698321\n",
255
+ "Epoch: 0 [64/539]\tLoss: 0.653913\n",
256
+ "Epoch: 0 [96/539]\tLoss: 0.659919\n",
257
+ "Epoch: 0 [128/539]\tLoss: 0.683509\n",
258
+ "Epoch: 0 [160/539]\tLoss: 0.634293\n",
259
+ "Epoch: 0 [192/539]\tLoss: 0.671241\n",
260
+ "Epoch: 0 [224/539]\tLoss: 0.625201\n",
261
+ "Epoch: 0 [256/539]\tLoss: 0.452264\n",
262
+ "Epoch: 0 [288/539]\tLoss: 0.487493\n",
263
+ "Epoch: 0 [320/539]\tLoss: 0.263387\n",
264
+ "Epoch: 0 [352/539]\tLoss: 0.548454\n",
265
+ "Epoch: 0 [384/539]\tLoss: 0.539689\n",
266
+ "Epoch: 0 [416/539]\tLoss: 0.267489\n",
267
+ "Epoch: 0 [448/539]\tLoss: 0.373991\n",
268
+ "Epoch: 0 [480/539]\tLoss: 0.328955\n",
269
+ "Epoch: 0 [512/539]\tLoss: 0.192182\n",
270
+ "Epoch 0 - Val F1: 0.8142, Val Accuracy: 0.8142\n",
271
+ "Novo melhor modelo salvo.\n",
272
+ "Epoch: 1 [32/539]\tLoss: 0.304935\n",
273
+ "Epoch: 1 [64/539]\tLoss: 0.417642\n",
274
+ "Epoch: 1 [96/539]\tLoss: 0.266215\n",
275
+ "Epoch: 1 [128/539]\tLoss: 0.528966\n",
276
+ "Epoch: 1 [160/539]\tLoss: 0.147393\n",
277
+ "Epoch: 1 [192/539]\tLoss: 0.070752\n",
278
+ "Epoch: 1 [224/539]\tLoss: 0.301449\n",
279
+ "Epoch: 1 [256/539]\tLoss: 0.427771\n",
280
+ "Epoch: 1 [288/539]\tLoss: 0.160852\n",
281
+ "Epoch: 1 [320/539]\tLoss: 0.351679\n",
282
+ "Epoch: 1 [352/539]\tLoss: 0.058495\n",
283
+ "Epoch: 1 [384/539]\tLoss: 0.198829\n",
284
+ "Epoch: 1 [416/539]\tLoss: 0.036262\n",
285
+ "Epoch: 1 [448/539]\tLoss: 0.270694\n",
286
+ "Epoch: 1 [480/539]\tLoss: 0.055876\n",
287
+ "Epoch: 1 [512/539]\tLoss: 0.377879\n",
288
+ "Epoch 1 - Val F1: 0.9101, Val Accuracy: 0.9102\n",
289
+ "Novo melhor modelo salvo.\n",
290
+ "Epoch: 2 [32/539]\tLoss: 0.088281\n",
291
+ "Epoch: 2 [64/539]\tLoss: 0.170918\n",
292
+ "Epoch: 2 [96/539]\tLoss: 0.131975\n",
293
+ "Epoch: 2 [128/539]\tLoss: 0.112684\n",
294
+ "Epoch: 2 [160/539]\tLoss: 0.087198\n",
295
+ "Epoch: 2 [192/539]\tLoss: 0.022119\n",
296
+ "Epoch: 2 [224/539]\tLoss: 0.111156\n",
297
+ "Epoch: 2 [256/539]\tLoss: 0.039095\n",
298
+ "Epoch: 2 [288/539]\tLoss: 0.035981\n",
299
+ "Epoch: 2 [320/539]\tLoss: 0.063993\n",
300
+ "Epoch: 2 [352/539]\tLoss: 0.289066\n",
301
+ "Epoch: 2 [384/539]\tLoss: 0.007462\n",
302
+ "Epoch: 2 [416/539]\tLoss: 0.038028\n",
303
+ "Epoch: 2 [448/539]\tLoss: 0.245104\n",
304
+ "Epoch: 2 [480/539]\tLoss: 0.161102\n",
305
+ "Epoch: 2 [512/539]\tLoss: 0.359799\n",
306
+ "Epoch 2 - Val F1: 0.9352, Val Accuracy: 0.9353\n",
307
+ "Novo melhor modelo salvo.\n",
308
+ "Epoch: 3 [32/539]\tLoss: 0.017929\n",
309
+ "Epoch: 3 [64/539]\tLoss: 0.236215\n",
310
+ "Epoch: 3 [96/539]\tLoss: 0.008162\n",
311
+ "Epoch: 3 [128/539]\tLoss: 0.002611\n",
312
+ "Epoch: 3 [160/539]\tLoss: 0.014606\n",
313
+ "Epoch: 3 [192/539]\tLoss: 0.010964\n",
314
+ "Epoch: 3 [224/539]\tLoss: 0.009766\n",
315
+ "Epoch: 3 [256/539]\tLoss: 0.229709\n",
316
+ "Epoch: 3 [288/539]\tLoss: 0.002626\n",
317
+ "Epoch: 3 [320/539]\tLoss: 0.079086\n",
318
+ "Epoch: 3 [352/539]\tLoss: 0.008535\n",
319
+ "Epoch: 3 [384/539]\tLoss: 0.038777\n",
320
+ "Epoch: 3 [416/539]\tLoss: 0.023151\n",
321
+ "Epoch: 3 [448/539]\tLoss: 0.011855\n",
322
+ "Epoch: 3 [480/539]\tLoss: 0.003287\n",
323
+ "Epoch: 3 [512/539]\tLoss: 0.050024\n",
324
+ "Epoch 3 - Val F1: 0.9309, Val Accuracy: 0.9311\n",
325
+ "Epoch: 4 [32/539]\tLoss: 0.009518\n",
326
+ "Epoch: 4 [64/539]\tLoss: 0.005575\n",
327
+ "Epoch: 4 [96/539]\tLoss: 0.023808\n",
328
+ "Epoch: 4 [128/539]\tLoss: 0.006816\n",
329
+ "Epoch: 4 [160/539]\tLoss: 0.001851\n",
330
+ "Epoch: 4 [192/539]\tLoss: 0.006231\n",
331
+ "Epoch: 4 [224/539]\tLoss: 0.028360\n",
332
+ "Epoch: 4 [256/539]\tLoss: 0.002173\n",
333
+ "Epoch: 4 [288/539]\tLoss: 0.155585\n",
334
+ "Epoch: 4 [320/539]\tLoss: 0.001898\n",
335
+ "Epoch: 4 [352/539]\tLoss: 0.026873\n",
336
+ "Epoch: 4 [384/539]\tLoss: 0.001487\n",
337
+ "Epoch: 4 [416/539]\tLoss: 0.005737\n",
338
+ "Epoch: 4 [448/539]\tLoss: 0.001628\n",
339
+ "Epoch: 4 [480/539]\tLoss: 0.012242\n",
340
+ "Epoch: 4 [512/539]\tLoss: 0.002932\n",
341
+ "Epoch 4 - Val F1: 0.9415, Val Accuracy: 0.9415\n",
342
+ "Novo melhor modelo salvo.\n",
343
+ "Desempenho no conjunto de teste desta dobra:\n",
344
+ " precision recall f1-score support\n",
345
+ "\n",
346
+ " 0 0.93 0.97 0.95 627\n",
347
+ " 1 0.96 0.92 0.94 570\n",
348
+ "\n",
349
+ " accuracy 0.95 1197\n",
350
+ " macro avg 0.95 0.94 0.95 1197\n",
351
+ "weighted avg 0.95 0.95 0.95 1197\n",
352
+ "\n",
353
+ "F1 (teste): 0.9456, Accuracy (teste): 0.9457\n",
354
+ "\n",
355
+ "=== Fold 3/5 ===\n",
356
+ "Epoch: 0 [32/539]\tLoss: 0.735917\n",
357
+ "Epoch: 0 [64/539]\tLoss: 0.592529\n",
358
+ "Epoch: 0 [96/539]\tLoss: 0.643685\n",
359
+ "Epoch: 0 [128/539]\tLoss: 0.627175\n",
360
+ "Epoch: 0 [160/539]\tLoss: 0.575533\n",
361
+ "Epoch: 0 [192/539]\tLoss: 0.729482\n",
362
+ "Epoch: 0 [224/539]\tLoss: 0.509104\n",
363
+ "Epoch: 0 [256/539]\tLoss: 0.786189\n",
364
+ "Epoch: 0 [288/539]\tLoss: 0.806076\n",
365
+ "Epoch: 0 [320/539]\tLoss: 0.607852\n",
366
+ "Epoch: 0 [352/539]\tLoss: 0.473597\n",
367
+ "Epoch: 0 [384/539]\tLoss: 0.373717\n",
368
+ "Epoch: 0 [416/539]\tLoss: 0.531733\n",
369
+ "Epoch: 0 [448/539]\tLoss: 0.715181\n",
370
+ "Epoch: 0 [480/539]\tLoss: 0.494880\n",
371
+ "Epoch: 0 [512/539]\tLoss: 0.229518\n",
372
+ "Epoch 0 - Val F1: 0.7972, Val Accuracy: 0.7975\n",
373
+ "Novo melhor modelo salvo.\n",
374
+ "Epoch: 1 [32/539]\tLoss: 0.107594\n",
375
+ "Epoch: 1 [64/539]\tLoss: 0.467723\n",
376
+ "Epoch: 1 [96/539]\tLoss: 0.631812\n",
377
+ "Epoch: 1 [128/539]\tLoss: 0.572087\n",
378
+ "Epoch: 1 [160/539]\tLoss: 0.248338\n",
379
+ "Epoch: 1 [192/539]\tLoss: 0.256459\n",
380
+ "Epoch: 1 [224/539]\tLoss: 0.099010\n",
381
+ "Epoch: 1 [256/539]\tLoss: 0.167257\n",
382
+ "Epoch: 1 [288/539]\tLoss: 0.507385\n",
383
+ "Epoch: 1 [320/539]\tLoss: 0.845850\n",
384
+ "Epoch: 1 [352/539]\tLoss: 0.117461\n",
385
+ "Epoch: 1 [384/539]\tLoss: 0.114881\n",
386
+ "Epoch: 1 [416/539]\tLoss: 0.073258\n",
387
+ "Epoch: 1 [448/539]\tLoss: 0.160434\n",
388
+ "Epoch: 1 [480/539]\tLoss: 0.303744\n",
389
+ "Epoch: 1 [512/539]\tLoss: 0.108967\n",
390
+ "Epoch 1 - Val F1: 0.9061, Val Accuracy: 0.9061\n",
391
+ "Novo melhor modelo salvo.\n",
392
+ "Epoch: 2 [32/539]\tLoss: 0.026718\n",
393
+ "Epoch: 2 [64/539]\tLoss: 0.107227\n",
394
+ "Epoch: 2 [96/539]\tLoss: 0.018087\n",
395
+ "Epoch: 2 [128/539]\tLoss: 0.653934\n",
396
+ "Epoch: 2 [160/539]\tLoss: 0.084223\n",
397
+ "Epoch: 2 [192/539]\tLoss: 0.127844\n",
398
+ "Epoch: 2 [224/539]\tLoss: 0.085503\n",
399
+ "Epoch: 2 [256/539]\tLoss: 0.012499\n",
400
+ "Epoch: 2 [288/539]\tLoss: 0.081436\n",
401
+ "Epoch: 2 [320/539]\tLoss: 0.062079\n",
402
+ "Epoch: 2 [352/539]\tLoss: 0.023066\n",
403
+ "Epoch: 2 [384/539]\tLoss: 0.033954\n",
404
+ "Epoch: 2 [416/539]\tLoss: 0.016665\n",
405
+ "Epoch: 2 [448/539]\tLoss: 0.035527\n",
406
+ "Epoch: 2 [480/539]\tLoss: 0.024941\n",
407
+ "Epoch: 2 [512/539]\tLoss: 0.386871\n",
408
+ "Epoch 2 - Val F1: 0.9121, Val Accuracy: 0.9123\n",
409
+ "Novo melhor modelo salvo.\n",
410
+ "Epoch: 3 [32/539]\tLoss: 0.163918\n",
411
+ "Epoch: 3 [64/539]\tLoss: 0.009033\n",
412
+ "Epoch: 3 [96/539]\tLoss: 0.085215\n",
413
+ "Epoch: 3 [128/539]\tLoss: 0.006444\n",
414
+ "Epoch: 3 [160/539]\tLoss: 0.025524\n",
415
+ "Epoch: 3 [192/539]\tLoss: 0.069963\n",
416
+ "Epoch: 3 [224/539]\tLoss: 0.046311\n",
417
+ "Epoch: 3 [256/539]\tLoss: 0.039229\n",
418
+ "Epoch: 3 [288/539]\tLoss: 0.323314\n",
419
+ "Epoch: 3 [320/539]\tLoss: 0.010894\n",
420
+ "Epoch: 3 [352/539]\tLoss: 0.042332\n",
421
+ "Epoch: 3 [384/539]\tLoss: 0.009699\n",
422
+ "Epoch: 3 [416/539]\tLoss: 0.006716\n",
423
+ "Epoch: 3 [448/539]\tLoss: 0.012770\n",
424
+ "Epoch: 3 [480/539]\tLoss: 0.011455\n",
425
+ "Epoch: 3 [512/539]\tLoss: 0.009429\n",
426
+ "Epoch 3 - Val F1: 0.9249, Val Accuracy: 0.9248\n",
427
+ "Novo melhor modelo salvo.\n",
428
+ "Epoch: 4 [32/539]\tLoss: 0.113521\n",
429
+ "Epoch: 4 [64/539]\tLoss: 0.011155\n",
430
+ "Epoch: 4 [96/539]\tLoss: 0.014652\n",
431
+ "Epoch: 4 [128/539]\tLoss: 0.069262\n",
432
+ "Epoch: 4 [160/539]\tLoss: 0.006706\n",
433
+ "Epoch: 4 [192/539]\tLoss: 0.324895\n",
434
+ "Epoch: 4 [224/539]\tLoss: 0.074128\n",
435
+ "Epoch: 4 [256/539]\tLoss: 0.006551\n",
436
+ "Epoch: 4 [288/539]\tLoss: 0.006611\n",
437
+ "Epoch: 4 [320/539]\tLoss: 0.003768\n",
438
+ "Epoch: 4 [352/539]\tLoss: 0.005372\n",
439
+ "Epoch: 4 [384/539]\tLoss: 0.004562\n",
440
+ "Epoch: 4 [416/539]\tLoss: 0.006518\n",
441
+ "Epoch: 4 [448/539]\tLoss: 0.005809\n",
442
+ "Epoch: 4 [480/539]\tLoss: 0.005891\n",
443
+ "Epoch: 4 [512/539]\tLoss: 0.009314\n",
444
+ "Epoch 4 - Val F1: 0.9353, Val Accuracy: 0.9353\n",
445
+ "Novo melhor modelo salvo.\n",
446
+ "Desempenho no conjunto de teste desta dobra:\n",
447
+ " precision recall f1-score support\n",
448
+ "\n",
449
+ " 0 0.94 0.95 0.94 627\n",
450
+ " 1 0.94 0.93 0.94 570\n",
451
+ "\n",
452
+ " accuracy 0.94 1197\n",
453
+ " macro avg 0.94 0.94 0.94 1197\n",
454
+ "weighted avg 0.94 0.94 0.94 1197\n",
455
+ "\n",
456
+ "F1 (teste): 0.9407, Accuracy (teste): 0.9407\n",
457
+ "\n",
458
+ "=== Fold 4/5 ===\n",
459
+ "Epoch: 0 [32/539]\tLoss: 0.745997\n",
460
+ "Epoch: 0 [64/539]\tLoss: 0.682468\n",
461
+ "Epoch: 0 [96/539]\tLoss: 0.702846\n",
462
+ "Epoch: 0 [128/539]\tLoss: 0.715030\n",
463
+ "Epoch: 0 [160/539]\tLoss: 0.769712\n",
464
+ "Epoch: 0 [192/539]\tLoss: 0.701377\n",
465
+ "Epoch: 0 [224/539]\tLoss: 0.572981\n",
466
+ "Epoch: 0 [256/539]\tLoss: 0.526011\n",
467
+ "Epoch: 0 [288/539]\tLoss: 0.628082\n",
468
+ "Epoch: 0 [320/539]\tLoss: 0.659288\n",
469
+ "Epoch: 0 [352/539]\tLoss: 0.354942\n",
470
+ "Epoch: 0 [384/539]\tLoss: 0.456815\n",
471
+ "Epoch: 0 [416/539]\tLoss: 0.537916\n",
472
+ "Epoch: 0 [448/539]\tLoss: 0.372000\n",
473
+ "Epoch: 0 [480/539]\tLoss: 0.273390\n",
474
+ "Epoch: 0 [512/539]\tLoss: 0.462089\n",
475
+ "Epoch 0 - Val F1: 0.8598, Val Accuracy: 0.8601\n",
476
+ "Novo melhor modelo salvo.\n",
477
+ "Epoch: 1 [32/539]\tLoss: 0.400376\n",
478
+ "Epoch: 1 [64/539]\tLoss: 0.292799\n",
479
+ "Epoch: 1 [96/539]\tLoss: 0.097472\n",
480
+ "Epoch: 1 [128/539]\tLoss: 0.901339\n",
481
+ "Epoch: 1 [160/539]\tLoss: 0.617738\n",
482
+ "Epoch: 1 [192/539]\tLoss: 0.115037\n",
483
+ "Epoch: 1 [224/539]\tLoss: 0.206575\n",
484
+ "Epoch: 1 [256/539]\tLoss: 0.186537\n",
485
+ "Epoch: 1 [288/539]\tLoss: 0.674716\n",
486
+ "Epoch: 1 [320/539]\tLoss: 0.093894\n",
487
+ "Epoch: 1 [352/539]\tLoss: 0.126228\n",
488
+ "Epoch: 1 [384/539]\tLoss: 0.104674\n",
489
+ "Epoch: 1 [416/539]\tLoss: 0.337742\n",
490
+ "Epoch: 1 [448/539]\tLoss: 0.612781\n",
491
+ "Epoch: 1 [480/539]\tLoss: 0.306427\n",
492
+ "Epoch: 1 [512/539]\tLoss: 0.015238\n",
493
+ "Epoch 1 - Val F1: 0.8899, Val Accuracy: 0.8914\n",
494
+ "Novo melhor modelo salvo.\n",
495
+ "Epoch: 2 [32/539]\tLoss: 0.095481\n",
496
+ "Epoch: 2 [64/539]\tLoss: 0.016074\n",
497
+ "Epoch: 2 [96/539]\tLoss: 0.058413\n",
498
+ "Epoch: 2 [128/539]\tLoss: 0.098460\n",
499
+ "Epoch: 2 [160/539]\tLoss: 0.010817\n",
500
+ "Epoch: 2 [192/539]\tLoss: 0.217947\n",
501
+ "Epoch: 2 [224/539]\tLoss: 0.014442\n",
502
+ "Epoch: 2 [256/539]\tLoss: 0.010201\n",
503
+ "Epoch: 2 [288/539]\tLoss: 0.296449\n",
504
+ "Epoch: 2 [320/539]\tLoss: 0.116354\n",
505
+ "Epoch: 2 [352/539]\tLoss: 0.036927\n",
506
+ "Epoch: 2 [384/539]\tLoss: 0.028675\n",
507
+ "Epoch: 2 [416/539]\tLoss: 0.010761\n",
508
+ "Epoch: 2 [448/539]\tLoss: 0.022918\n",
509
+ "Epoch: 2 [480/539]\tLoss: 0.034252\n",
510
+ "Epoch: 2 [512/539]\tLoss: 0.150974\n",
511
+ "Epoch 2 - Val F1: 0.9436, Val Accuracy: 0.9436\n",
512
+ "Novo melhor modelo salvo.\n",
513
+ "Epoch: 3 [32/539]\tLoss: 0.006742\n",
514
+ "Epoch: 3 [64/539]\tLoss: 0.015323\n",
515
+ "Epoch: 3 [96/539]\tLoss: 0.033706\n",
516
+ "Epoch: 3 [128/539]\tLoss: 0.013044\n",
517
+ "Epoch: 3 [160/539]\tLoss: 0.003634\n",
518
+ "Epoch: 3 [192/539]\tLoss: 0.035571\n",
519
+ "Epoch: 3 [224/539]\tLoss: 0.003196\n",
520
+ "Epoch: 3 [256/539]\tLoss: 0.078975\n",
521
+ "Epoch: 3 [288/539]\tLoss: 0.011156\n",
522
+ "Epoch: 3 [320/539]\tLoss: 0.010516\n",
523
+ "Epoch: 3 [352/539]\tLoss: 0.030506\n",
524
+ "Epoch: 3 [384/539]\tLoss: 0.025923\n",
525
+ "Epoch: 3 [416/539]\tLoss: 0.011039\n",
526
+ "Epoch: 3 [448/539]\tLoss: 0.077258\n",
527
+ "Epoch: 3 [480/539]\tLoss: 0.042708\n",
528
+ "Epoch: 3 [512/539]\tLoss: 0.003598\n",
529
+ "Epoch 3 - Val F1: 0.9519, Val Accuracy: 0.9520\n",
530
+ "Novo melhor modelo salvo.\n",
531
+ "Epoch: 4 [32/539]\tLoss: 0.001914\n",
532
+ "Epoch: 4 [64/539]\tLoss: 0.043992\n",
533
+ "Epoch: 4 [96/539]\tLoss: 0.006504\n",
534
+ "Epoch: 4 [128/539]\tLoss: 0.002461\n",
535
+ "Epoch: 4 [160/539]\tLoss: 0.012732\n",
536
+ "Epoch: 4 [192/539]\tLoss: 0.002560\n",
537
+ "Epoch: 4 [224/539]\tLoss: 0.002396\n",
538
+ "Epoch: 4 [256/539]\tLoss: 0.006392\n",
539
+ "Epoch: 4 [288/539]\tLoss: 0.015300\n",
540
+ "Epoch: 4 [320/539]\tLoss: 0.095689\n",
541
+ "Epoch: 4 [352/539]\tLoss: 0.007642\n",
542
+ "Epoch: 4 [384/539]\tLoss: 0.007563\n",
543
+ "Epoch: 4 [416/539]\tLoss: 0.004592\n",
544
+ "Epoch: 4 [448/539]\tLoss: 0.035378\n",
545
+ "Epoch: 4 [480/539]\tLoss: 0.008071\n",
546
+ "Epoch: 4 [512/539]\tLoss: 0.004615\n",
547
+ "Epoch 4 - Val F1: 0.9415, Val Accuracy: 0.9415\n",
548
+ "Desempenho no conjunto de teste desta dobra:\n",
549
+ " precision recall f1-score support\n",
550
+ "\n",
551
+ " 0 0.92 0.94 0.93 628\n",
552
+ " 1 0.93 0.92 0.92 569\n",
553
+ "\n",
554
+ " accuracy 0.93 1197\n",
555
+ " macro avg 0.93 0.93 0.93 1197\n",
556
+ "weighted avg 0.93 0.93 0.93 1197\n",
557
+ "\n",
558
+ "F1 (teste): 0.9273, Accuracy (teste): 0.9273\n",
559
+ "\n",
560
+ "=== Fold 5/5 ===\n",
561
+ "Epoch: 0 [32/539]\tLoss: 0.736348\n",
562
+ "Epoch: 0 [64/539]\tLoss: 0.757772\n",
563
+ "Epoch: 0 [96/539]\tLoss: 0.679295\n",
564
+ "Epoch: 0 [128/539]\tLoss: 0.660582\n",
565
+ "Epoch: 0 [160/539]\tLoss: 0.802816\n",
566
+ "Epoch: 0 [192/539]\tLoss: 0.547177\n",
567
+ "Epoch: 0 [224/539]\tLoss: 0.691502\n",
568
+ "Epoch: 0 [256/539]\tLoss: 0.685943\n",
569
+ "Epoch: 0 [288/539]\tLoss: 0.451294\n",
570
+ "Epoch: 0 [320/539]\tLoss: 0.541730\n",
571
+ "Epoch: 0 [352/539]\tLoss: 0.150317\n",
572
+ "Epoch: 0 [384/539]\tLoss: 0.223378\n",
573
+ "Epoch: 0 [416/539]\tLoss: 0.731548\n",
574
+ "Epoch: 0 [448/539]\tLoss: 0.226719\n",
575
+ "Epoch: 0 [480/539]\tLoss: 0.282167\n",
576
+ "Epoch: 0 [512/539]\tLoss: 0.424890\n",
577
+ "Epoch 0 - Val F1: 0.7837, Val Accuracy: 0.7891\n",
578
+ "Novo melhor modelo salvo.\n",
579
+ "Epoch: 1 [32/539]\tLoss: 0.398466\n",
580
+ "Epoch: 1 [64/539]\tLoss: 0.383012\n",
581
+ "Epoch: 1 [96/539]\tLoss: 0.431615\n",
582
+ "Epoch: 1 [128/539]\tLoss: 0.117072\n",
583
+ "Epoch: 1 [160/539]\tLoss: 0.245383\n",
584
+ "Epoch: 1 [192/539]\tLoss: 0.419279\n",
585
+ "Epoch: 1 [224/539]\tLoss: 0.275579\n",
586
+ "Epoch: 1 [256/539]\tLoss: 0.336219\n",
587
+ "Epoch: 1 [288/539]\tLoss: 0.250066\n",
588
+ "Epoch: 1 [320/539]\tLoss: 0.293893\n",
589
+ "Epoch: 1 [352/539]\tLoss: 0.088111\n",
590
+ "Epoch: 1 [384/539]\tLoss: 0.242597\n",
591
+ "Epoch: 1 [416/539]\tLoss: 0.061158\n",
592
+ "Epoch: 1 [448/539]\tLoss: 0.260205\n",
593
+ "Epoch: 1 [480/539]\tLoss: 0.208200\n",
594
+ "Epoch: 1 [512/539]\tLoss: 0.113565\n",
595
+ "Epoch 1 - Val F1: 0.8873, Val Accuracy: 0.8873\n",
596
+ "Novo melhor modelo salvo.\n",
597
+ "Epoch: 2 [32/539]\tLoss: 0.070310\n",
598
+ "Epoch: 2 [64/539]\tLoss: 0.163662\n",
599
+ "Epoch: 2 [96/539]\tLoss: 0.108127\n",
600
+ "Epoch: 2 [128/539]\tLoss: 0.281810\n",
601
+ "Epoch: 2 [160/539]\tLoss: 0.364615\n",
602
+ "Epoch: 2 [192/539]\tLoss: 0.147060\n",
603
+ "Epoch: 2 [224/539]\tLoss: 0.062546\n",
604
+ "Epoch: 2 [256/539]\tLoss: 0.125024\n",
605
+ "Epoch: 2 [288/539]\tLoss: 0.048165\n",
606
+ "Epoch: 2 [320/539]\tLoss: 0.241866\n",
607
+ "Epoch: 2 [352/539]\tLoss: 0.244164\n",
608
+ "Epoch: 2 [384/539]\tLoss: 0.155161\n",
609
+ "Epoch: 2 [416/539]\tLoss: 0.300804\n",
610
+ "Epoch: 2 [448/539]\tLoss: 0.125214\n",
611
+ "Epoch: 2 [480/539]\tLoss: 0.029340\n",
612
+ "Epoch: 2 [512/539]\tLoss: 0.132973\n",
613
+ "Epoch 2 - Val F1: 0.9186, Val Accuracy: 0.9186\n",
614
+ "Novo melhor modelo salvo.\n",
615
+ "Epoch: 3 [32/539]\tLoss: 0.182894\n",
616
+ "Epoch: 3 [64/539]\tLoss: 0.080429\n",
617
+ "Epoch: 3 [96/539]\tLoss: 0.168509\n",
618
+ "Epoch: 3 [128/539]\tLoss: 0.070553\n",
619
+ "Epoch: 3 [160/539]\tLoss: 0.051917\n",
620
+ "Epoch: 3 [192/539]\tLoss: 0.011117\n",
621
+ "Epoch: 3 [224/539]\tLoss: 0.080476\n",
622
+ "Epoch: 3 [256/539]\tLoss: 0.004516\n",
623
+ "Epoch: 3 [288/539]\tLoss: 0.007997\n",
624
+ "Epoch: 3 [320/539]\tLoss: 0.106819\n",
625
+ "Epoch: 3 [352/539]\tLoss: 0.036051\n",
626
+ "Epoch: 3 [384/539]\tLoss: 0.056000\n",
627
+ "Epoch: 3 [416/539]\tLoss: 0.017140\n",
628
+ "Epoch: 3 [448/539]\tLoss: 0.019664\n",
629
+ "Epoch: 3 [480/539]\tLoss: 0.002534\n",
630
+ "Epoch: 3 [512/539]\tLoss: 0.016210\n",
631
+ "Epoch 3 - Val F1: 0.9144, Val Accuracy: 0.9144\n",
632
+ "Epoch: 4 [32/539]\tLoss: 0.040328\n",
633
+ "Epoch: 4 [64/539]\tLoss: 0.005345\n",
634
+ "Epoch: 4 [96/539]\tLoss: 0.005071\n",
635
+ "Epoch: 4 [128/539]\tLoss: 0.007578\n",
636
+ "Epoch: 4 [160/539]\tLoss: 0.019537\n",
637
+ "Epoch: 4 [192/539]\tLoss: 0.004254\n",
638
+ "Epoch: 4 [224/539]\tLoss: 0.004656\n",
639
+ "Epoch: 4 [256/539]\tLoss: 0.028694\n",
640
+ "Epoch: 4 [288/539]\tLoss: 0.009497\n",
641
+ "Epoch: 4 [320/539]\tLoss: 0.006693\n",
642
+ "Epoch: 4 [352/539]\tLoss: 0.021709\n",
643
+ "Epoch: 4 [384/539]\tLoss: 0.007690\n",
644
+ "Epoch: 4 [416/539]\tLoss: 0.003045\n",
645
+ "Epoch: 4 [448/539]\tLoss: 0.019487\n",
646
+ "Epoch: 4 [480/539]\tLoss: 0.002427\n",
647
+ "Epoch: 4 [512/539]\tLoss: 0.009079\n",
648
+ "Epoch 4 - Val F1: 0.9270, Val Accuracy: 0.9269\n",
649
+ "Novo melhor modelo salvo.\n",
650
+ "Desempenho no conjunto de teste desta dobra:\n",
651
+ " precision recall f1-score support\n",
652
+ "\n",
653
+ " 0 0.92 0.94 0.93 627\n",
654
+ " 1 0.93 0.91 0.92 569\n",
655
+ "\n",
656
+ " accuracy 0.92 1196\n",
657
+ " macro avg 0.93 0.92 0.92 1196\n",
658
+ "weighted avg 0.92 0.92 0.92 1196\n",
659
+ "\n",
660
+ "F1 (teste): 0.9247, Accuracy (teste): 0.9247\n",
661
+ "\n",
662
+ "=== Resultados Médios da Validação Cruzada ===\n",
663
+ "F1 médio: 0.9334 (+/- 0.0082)\n",
664
+ "Acurácia média: 0.9335 (+/- 0.0082)\n"
665
+ ]
666
+ }
667
+ ],
668
+ "source": [
669
+ "# Modelo\n",
670
+ "class CustomBERTModel(nn.Module):\n",
671
+ " def __init__(self, model_name, nclasses, unfreeze_layers):\n",
672
+ " super(CustomBERTModel, self).__init__()\n",
673
+ " self.bert = AutoModel.from_pretrained(model_name)\n",
674
+ " self.dropout = nn.Dropout(0.3)\n",
675
+ " self.classifier = nn.Linear(self.bert.config.hidden_size, nclasses)\n",
676
+ "\n",
677
+ " # Congelar tudo inicialmente\n",
678
+ " for param in self.bert.parameters():\n",
679
+ " param.requires_grad = False\n",
680
+ "\n",
681
+ " # Descongelar as últimas unfreeze_layers camadas\n",
682
+ " if unfreeze_layers > 0:\n",
683
+ " for param in self.bert.encoder.layer[-unfreeze_layers:].parameters():\n",
684
+ " param.requires_grad = True\n",
685
+ "\n",
686
+ " def forward(self, input_ids, attention_mask, token_type_ids=None):\n",
687
+ " outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)\n",
688
+ " pooled_output = outputs.pooler_output\n",
689
+ " dropped_out = self.dropout(pooled_output)\n",
690
+ " logits = self.classifier(dropped_out)\n",
691
+ " return logits\n",
692
+ "\n",
693
+ "def evaluate(model, dataloader):\n",
694
+ " model.eval()\n",
695
+ " y_real, y_pred = [], []\n",
696
+ " with torch.no_grad():\n",
697
+ " for inputs, labels in dataloader:\n",
698
+ " inputs = {key: val.to(device) for key, val in inputs.items()}\n",
699
+ " labels = labels.to(device)\n",
700
+ " logits = model(**inputs)\n",
701
+ " pred_labels = torch.argmax(logits, 1)\n",
702
+ "\n",
703
+ " y_real.extend(labels.cpu().tolist())\n",
704
+ " y_pred.extend(pred_labels.cpu().tolist())\n",
705
+ "\n",
706
+ " f1 = f1_score(y_real, y_pred, average='weighted')\n",
707
+ " acc = accuracy_score(y_real, y_pred)\n",
708
+ " return f1, acc, (y_real, y_pred)\n",
709
+ "\n",
710
+ "\n",
711
+ "# Cross-validation\n",
712
+ "k = 5\n",
713
+ "skf = StratifiedKFold(n_splits=k, shuffle=True, random_state=seed)\n",
714
+ "X = data.index.values\n",
715
+ "y = data['contra'].values\n",
716
+ "\n",
717
+ "f1_scores = []\n",
718
+ "acc_scores = []\n",
719
+ "\n",
720
+ "tokenizer = AutoTokenizer.from_pretrained(model_name, do_lower_case=False)\n",
721
+ "\n",
722
+ "fold_num = 1\n",
723
+ "for train_val_idx, test_idx in skf.split(X, y):\n",
724
+ " print(f\"\\n=== Fold {fold_num}/{k} ===\")\n",
725
+ "\n",
726
+ " # Separamos test fold\n",
727
+ " test_data = data.iloc[test_idx]\n",
728
+ "\n",
729
+ " # A partir do train_val_idx, dividimos em train e val\n",
730
+ " train_val_data = data.iloc[train_val_idx]\n",
731
+ " train_data, val_data = train_test_split(train_val_data, \n",
732
+ " test_size=0.1, \n",
733
+ " random_state=seed, \n",
734
+ " stratify=train_val_data['contra'])\n",
735
+ " \n",
736
+ " # Criar datasets e dataloaders\n",
737
+ " train_dataset = CustomDataset(train_data, tokenizer, max_length)\n",
738
+ " val_dataset = CustomDataset(val_data, tokenizer, max_length)\n",
739
+ " test_dataset = CustomDataset(test_data, tokenizer, max_length)\n",
740
+ "\n",
741
+ " traindata = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)\n",
742
+ " valdata = DataLoader(val_dataset, batch_size=batch_size, shuffle=False)\n",
743
+ " testdata = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)\n",
744
+ "\n",
745
+ " model = CustomBERTModel(model_name, nclasses, unfreeze_layers).to(device)\n",
746
+ " optimizer = optim.AdamW(filter(lambda p: p.requires_grad, model.parameters()), lr=learning_rate)\n",
747
+ "\n",
748
+ " loss_fn = nn.CrossEntropyLoss()\n",
749
+ "\n",
750
+ " total_steps = len(traindata) * nepochs\n",
751
+ " scheduler = get_linear_schedule_with_warmup(optimizer, \n",
752
+ " num_warmup_steps=int(0.1 * total_steps), \n",
753
+ " num_training_steps=total_steps)\n",
754
+ "\n",
755
+ " max_f1, repeat = 0, 0\n",
756
+ " best_model_path = os.path.join(write_path, f'best_model_fold{fold_num}.pth')\n",
757
+ "\n",
758
+ " for epoch in range(nepochs):\n",
759
+ " model.train()\n",
760
+ " losses = []\n",
761
+ " for batch_idx, (inputs, labels) in enumerate(traindata):\n",
762
+ " inputs = {key: val.to(device) for key, val in inputs.items()}\n",
763
+ " labels = labels.to(device)\n",
764
+ "\n",
765
+ " logits = model(**inputs)\n",
766
+ " loss = loss_fn(logits, labels)\n",
767
+ " losses.append(float(loss))\n",
768
+ "\n",
769
+ " # Backprop\n",
770
+ " loss.backward()\n",
771
+ " optimizer.step()\n",
772
+ " scheduler.step()\n",
773
+ " optimizer.zero_grad()\n",
774
+ "\n",
775
+ " if (batch_idx + 1) % batch_status == 0:\n",
776
+ " print(f'Epoch: {epoch} [{batch_idx + 1}/{len(traindata)}]\\tLoss: {loss:.6f}')\n",
777
+ "\n",
778
+ " f1_val, acc_val, _ = evaluate(model, valdata)\n",
779
+ " print(f'Epoch {epoch} - Val F1: {f1_val:.4f}, Val Accuracy: {acc_val:.4f}')\n",
780
+ "\n",
781
+ " if f1_val > max_f1:\n",
782
+ " torch.save(model.state_dict(), best_model_path)\n",
783
+ " max_f1 = f1_val\n",
784
+ " repeat = 0\n",
785
+ " print('Novo melhor modelo salvo.')\n",
786
+ " else:\n",
787
+ " repeat += 1\n",
788
+ "\n",
789
+ " if repeat == early_stop:\n",
790
+ " print('Early stopping atingido.')\n",
791
+ " break\n",
792
+ "\n",
793
+ " # Avaliar no teste\n",
794
+ " state_dict = torch.load(best_model_path, weights_only=True)\n",
795
+ " model.load_state_dict(state_dict)\n",
796
+ " f1_test, acc_test, (y_real, y_pred) = evaluate(model, testdata)\n",
797
+ "\n",
798
+ " print(\"Desempenho no conjunto de teste desta dobra:\")\n",
799
+ " print(classification_report(y_real, y_pred, target_names=['0', '1']))\n",
800
+ " print(f\"F1 (teste): {f1_test:.4f}, Accuracy (teste): {acc_test:.4f}\")\n",
801
+ "\n",
802
+ " f1_scores.append(f1_test)\n",
803
+ " acc_scores.append(acc_test)\n",
804
+ "\n",
805
+ " fold_num += 1\n",
806
+ "\n",
807
+ "# Resultados médios da validação cruzada\n",
808
+ "print(\"\\n=== Resultados Médios da Validação Cruzada ===\")\n",
809
+ "print(f\"F1 médio: {statistics.mean(f1_scores):.4f} (+/- {statistics.pstdev(f1_scores):.4f})\")\n",
810
+ "print(f\"Acurácia média: {statistics.mean(acc_scores):.4f} (+/- {statistics.pstdev(acc_scores):.4f})\")\n"
811
+ ]
812
+ }
813
+ ],
814
+ "metadata": {
815
+ "kernelspec": {
816
+ "display_name": "Python 3 (ipykernel)",
817
+ "language": "python",
818
+ "name": "python3"
819
+ },
820
+ "language_info": {
821
+ "codemirror_mode": {
822
+ "name": "ipython",
823
+ "version": 3
824
+ },
825
+ "file_extension": ".py",
826
+ "mimetype": "text/x-python",
827
+ "name": "python",
828
+ "nbconvert_exporter": "python",
829
+ "pygments_lexer": "ipython3",
830
+ "version": "3.12.7"
831
+ }
832
+ },
833
+ "nbformat": 4,
834
+ "nbformat_minor": 5
835
+ }