-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
203 lines (152 loc) · 6.41 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import torch
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
import re
from sklearn.model_selection import train_test_split
from transformers import BertTokenizer
from transformers import BertForSequenceClassification, AdamW, BertConfig
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader, random_split
from torch.utils.data.sampler import SubsetRandomSampler
import transformers
from transformers import RobertaTokenizer, BertTokenizer, RobertaModel, BertModel, AdamW# get_linear_schedule_with_warmup
from transformers import get_linear_schedule_with_warmup
import time
from utils import *
from Custom_Dataset_Class import ConsumerComplaintsDataset1
from Bert_Classification import Bert_Classification_Model
from RoBERT import RoBERT_Model
from BERT_Hierarchical import BERT_Hierarchical_Model
import warnings
warnings.filterwarnings("ignore")
# If there's a GPU available...
if torch.cuda.is_available():
# Tell PyTorch to use the GPU.
device = torch.device("cuda")
print('There are %d GPU(s) available.' % torch.cuda.device_count())
print('We will use the GPU:', torch.cuda.get_device_name(0))
# If not...
else:
print('No GPU available, using the CPU instead.')
device = torch.device("cpu")
# Load the dataset into a pandas dataframe.
df=pd.read_csv("./us-consumer-finance-complaints/consumer_complaints.csv")
# Report the number of sentences.
print('Number of training sentences: {:,}\n'.format(df.shape[0]))
train_raw = df[df.consumer_complaint_narrative.notnull()]
print('Number of training sentences with complain narrative not null: {:,}\n'.format(train_raw.shape[0]))
# Display 10 random rows from the data.
print (train_raw.sample(10))
train_raw['len_txt'] =train_raw.consumer_complaint_narrative.apply(lambda x: len(x.split()))
train_raw.describe()
#Select only the row with number of words greater than 250:
train_raw = train_raw[train_raw.len_txt >249]
print(train_raw.shape)
#Select only the column 'consumer_complaint_narrative' and 'product'
train_raw = train_raw[['consumer_complaint_narrative', 'product']]
'count starts'
# word_count = []
# sent_count = []
# from nltk.tokenize import word_tokenize,sent_tokenize
# for row in train_raw['consumer_complaint_narrative']:
# text = row
# sents = sent_tokenize(text.strip())
# sent_count.append(len(sents))
#
# words = word_tokenize(text.strip())
# word_count.append(len(words))
'count ends'
# import pdb;pdb.set_trace()
train_raw.reset_index(inplace=True, drop=True)
train_raw.head()
#Group similar products
train_raw.at[train_raw['product'] == 'Credit reporting', 'product'] = 'Credit reporting, credit repair services, or other personal consumer reports'
train_raw.at[train_raw['product'] == 'Credit card', 'product'] = 'Credit card or prepaid card'
train_raw.at[train_raw['product'] == 'Prepaid card', 'product'] = 'Credit card or prepaid card'
train_raw.at[train_raw['product'] == 'Payday loan', 'product'] = 'Payday loan, title loan, or personal loan'
train_raw.at[train_raw['product'] == 'Virtual currency', 'product'] = 'Money transfer, virtual currency, or money service'
print(train_raw.head())
#all the different classes
print ('Found {} classes'.format(len(np.unique(train_raw['product']))))
for l in np.unique(train_raw['product']):
print(l)
train_raw=train_raw.rename(columns = {'consumer_complaint_narrative':'text', 'product':'label'})
train_raw.head()
import pdb;pdb.set_trace()
print ('Data check done.')
TRAIN_BATCH_SIZE=8
EPOCH=5
validation_split = .2
shuffle_dataset = True
random_seed= 42
MIN_LEN=249
MAX_LEN = 100000
CHUNK_LEN=200
OVERLAP_LEN=50
#MAX_LEN=10000000
#MAX_SIZE_DATASET=1000
print('Loading BERT tokenizer...')
bert_tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)
dataset=ConsumerComplaintsDataset1(
tokenizer=bert_tokenizer,
min_len=MIN_LEN,
max_len=MAX_LEN,
chunk_len=CHUNK_LEN,
#max_size_dataset=MAX_SIZE_DATASET,
approach='all',
overlap_len=OVERLAP_LEN)
#train_size = int(0.8 * len(dataset))
#test_size = len(dataset) - train_size
#train_dataset, test_dataset = random_split(dataset, [train_size, test_size])
# Creating data indices for training and validation splits:
dataset_size = len(dataset)
indices = list(range(dataset_size))
split = int(np.floor(validation_split * dataset_size))
if shuffle_dataset :
np.random.seed(random_seed)
np.random.shuffle(indices)
train_indices, val_indices = indices[split:], indices[:split]
# Creating PT data samplers and loaders:
train_sampler = SubsetRandomSampler(train_indices)
valid_sampler = SubsetRandomSampler(val_indices)
from utils import my_collate1
train_data_loader=DataLoader(
dataset,
batch_size=TRAIN_BATCH_SIZE,
sampler=train_sampler,collate_fn=my_collate1)
valid_data_loader=DataLoader(
dataset,
batch_size=TRAIN_BATCH_SIZE,
sampler=valid_sampler,collate_fn=my_collate1)
print ('Model building done.')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('Using device:', device)
lr=3e-5#1e-3
num_training_steps=int(len(dataset) / TRAIN_BATCH_SIZE * EPOCH)
from utils import train_loop_fun1,evaluate,eval_loop_fun1
model=Bert_Classification_Model().to(device)
optimizer=AdamW(model.parameters(), lr=lr)
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps = 0,
num_training_steps = num_training_steps)
val_losses=[]
batches_losses=[]
val_acc=[]
for epoch in range(EPOCH):
t0 = time.time()
print(f"\n=============== EPOCH {epoch+1} / {EPOCH} ===============\n")
batches_losses_tmp=train_loop_fun1(train_data_loader, model, optimizer, device)
epoch_loss=np.mean(batches_losses_tmp)
print(f"\n*** avg_loss : {epoch_loss:.2f}, time : ~{(time.time()-t0)//60} min ({time.time()-t0:.2f} sec) ***\n")
t1=time.time()
output, target, val_losses_tmp=eval_loop_fun1(valid_data_loader, model, device)
print(f"==> evaluation : avg_loss = {np.mean(val_losses_tmp):.2f}, time : {time.time()-t1:.2f} sec\n")
tmp_evaluate=evaluate(target.reshape(-1), output)
print(f"=====>\t{tmp_evaluate}")
val_acc.append(tmp_evaluate['accuracy'])
val_losses.append(val_losses_tmp)
batches_losses.append(batches_losses_tmp)
print("\t§§ model has been saved §§")
torch.save(model, f"model_bert/model_epoch{epoch+1}.pt")