-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patht5_train.py
246 lines (196 loc) · 9.19 KB
/
t5_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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from datasets import Dataset, DatasetDict
import torch
from random import randrange, sample
from transformers import DataCollatorForSeq2Seq
import pandas as pd
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from peft import LoraConfig, get_peft_model, prepare_model_for_int8_training, TaskType
from transformers import DataCollatorForSeq2Seq
from transformers import Seq2SeqTrainer, Seq2SeqTrainingArguments
import argparse
import numpy as np
from transformers import T5Tokenizer, T5ForConditionalGeneration
import os
from collections import Counter
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
parser = argparse.ArgumentParser()
parser.add_argument('--model_path', type=str, help='path to save fine-tuned t5 model')
parser.add_argument('--model', type=str, help='pretrained t5', default='google/flan-t5-xl')
parser.add_argument('--synthetic_data', type=str, help='path to synthetic data file')
parser.add_argument('--adverse', action='store_true', help='only add adverse labels')
parser.add_argument('--prompt', type=str, help='prepend string to prompt T5 model', default='summarize: ')
parser.add_argument('--undersample', type=float, help='amount to keep', default=0.0)
parser.add_argument('--gold', type=float, help='amount fo REAL data to keep', default=0.0)
args = parser.parse_args()
device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")
if args.adverse:
LABELS = {'TRANSPORTATION_distance', 'TRANSPORTATION_resource',
'TRANSPORTATION_other', 'HOUSING_poor', 'HOUSING_undomiciled','HOUSING_other',
'RELATIONSHIP_divorced', 'RELATIONSHIP_widowed', 'RELATIONSHIP_single',
'PARENT','EMPLOYMENT_underemployed','EMPLOYMENT_unemployed', 'EMPLOYMENT_disability','SUPPORT_minus'}
else:
LABELS = {'TRANSPORTATION_distance', 'TRANSPORTATION_resource',
'TRANSPORTATION_other', 'HOUSING_poor', 'HOUSING_undomiciled',
'HOUSING_other', 'RELATIONSHIP_married', 'RELATIONSHIP_partnered',
'RELATIONSHIP_divorced', 'RELATIONSHIP_widowed', 'RELATIONSHIP_single',
'PARENT','EMPLOYMENT_employed', 'EMPLOYMENT_underemployed',
'EMPLOYMENT_unemployed', 'EMPLOYMENT_disability', 'EMPLOYMENT_retired',
'EMPLOYMENT_student', 'SUPPORT_plus', 'SUPPORT_minus'}
BROAD_LABELS = {lab.split('_')[0] for lab in LABELS}
BROAD_LABELS.add('<NO_SDOH>')
LABEL_BROAD_NARROW = LABELS.union(BROAD_LABELS)
MODEL_ID= args.model
TOKENIZER = AutoTokenizer.from_pretrained(MODEL_ID)
MAX_S_LEN = 100
MAX_T_LEN = 40
def undersample(df, label, keep_percent):
"""
Undersamples the majority class in a Pandas dataframe to balance the classes.
Parameters:
df (pandas.DataFrame): The dataframe to undersample.
keep_percent (float): The percentage of the majority class to keep.
Returns:
pandas.DataFrame: The undersampled dataframe.
"""
# Find the majority class based on the labels column
counts = df[label].value_counts()
majority_class = counts.idxmax()
# Get the indices of rows in the majority class
majority_indices = df[df[label] == majority_class].index
# Calculate the number of majority class rows to keep
num_majority_keep = int(keep_percent * counts[majority_class])
# Get a random subset of the majority class rows to keep
majority_keep_indices = np.random.choice(majority_indices, num_majority_keep, replace=False)
# Get the indices of rows in the minority class
minority_indices = df[df[label] != majority_class].index
# Combine the majority class subset and the minority class rows
undersampled_indices = np.concatenate([majority_keep_indices, minority_indices])
# Return the undersampled dataframe
return df.loc[undersampled_indices]
def filter_rows_by_label_percentage(df, percentage):
# Calculate the number of rows to keep for '<NO_SDOH>' label
no_sdoh_rows = int(len(df[df['LABEL'] == '<NO_SDOH>']) * percentage)
# Calculate the number of rows to keep for other label values
other_rows = int(len(df[df['LABEL'] != '<NO_SDOH>']) * percentage)
# Filter rows with '<NO_SDOH>' label and sample
no_sdoh_data = df[df['LABEL'] == '<NO_SDOH>'].sample(n=no_sdoh_rows)
# Filter rows with other label values and sample
other_data = df[df['LABEL'] != '<NO_SDOH>'].sample(n=other_rows)
# Concatenate the two filtered DataFrames
filtered_df = pd.concat([no_sdoh_data, other_data])
filtered_df.reset_index(inplace=True, drop=True)
return filtered_df
def generate_label_list(row: pd.DataFrame) -> str:
"""
Generate a label list based on the given row from a Pandas DataFrame.
Args:
row (pd.DataFrame): A row from a Pandas DataFrame.
Returns:
str: A comma-separated string of labels extracted from the row.
Examples:
>>> df = pd.DataFrame({'label1_1': [1], 'label2_0': [0], 'label3_1': [1]})
>>> generate_label_list(df.iloc[0])
'label1,label3'
>>> df = pd.DataFrame({'label2_0': [0], 'label3_0': [0]})
>>> generate_label_list(df.iloc[0])
'<NO_SDOH>'
"""
labels = set()
for col_name, value in row.items():
if col_name in LABELS and value == 1:
labels.add(col_name.split('_')[0])
if len(labels) == 0:
labels.add('<NO_SDOH>')
return ','.join(list(labels))
def preprocess_function(sample,padding="max_length"):
# add prefix to the input for t5
inputs = [args.prompt + item for item in sample["text"]]
# tokenize inputs
model_inputs = TOKENIZER(inputs, max_length=MAX_S_LEN, padding=padding, truncation=True)
# Tokenize targets with the `text_target` keyword argument
labels = TOKENIZER(text_target=sample["SDOHlabels"], max_length=MAX_T_LEN, padding=padding, truncation=True)
# If we are padding here, replace all tokenizer.pad_token_id in the labels by -100 when we want to ignore
# padding in the loss.
if padding == "max_length":
labels["input_ids"] = [
[(l if l != TOKENIZER.pad_token_id else -100) for l in label] for label in labels["input_ids"]
]
model_inputs["labels"] = labels["input_ids"]
return model_inputs
if __name__ == '__main__':
train_data = pd.read_csv('../data/train_sents.csv')
train_data.fillna(value={'text':''}, inplace=True)
train_data['LABEL'] = train_data.apply(generate_label_list, axis=1).tolist()
if args.undersample:
train_data = undersample(train_data, label='LABEL', keep_percent=args.undersample)
if args.gold:
train_data = filter_rows_by_label_percentage(train_data, args.gold)
train_text = train_data['text'].tolist()
train_labels = train_data['LABEL'].tolist()
if args.synthetic_data:
synthetic_data = pd.read_csv(args.synthetic_data)
synthetic_data = synthetic_data[synthetic_data['label'].isin(BROAD_LABELS)]
if args.adverse:
synthetic_data = synthetic_data[synthetic_data['adverse']=='adverse']
synthetic_data.reset_index(inplace=True, drop=True)
binary_synthetic = pd.get_dummies(synthetic_data['label'])
binary_synthetic['text'] = synthetic_data['text']
synth_labels = binary_synthetic.apply(generate_label_list, axis=1).tolist()
synth_text = synthetic_data['text'].tolist()
train_text.extend(synth_text)
train_labels.extend(synth_labels)
train_t5 = pd.DataFrame({'text':train_text, 'SDOHlabels':train_labels})
train_dataset = Dataset.from_pandas(train_t5)
dataset = DatasetDict()
dataset['train'] = train_dataset
print(f"Train dataset size: {len(dataset['train'])}")
tokenized_dataset = dataset.map(preprocess_function, batched=True, remove_columns=["text", "SDOHlabels"])
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID, load_in_8bit=True, device_map={"":0}) #{"":1}
# Define LoRA Config
lora_config = LoraConfig(
r=16,
lora_alpha=32,
target_modules=["q", "v"],
lora_dropout=0.05,
bias="none",
task_type=TaskType.SEQ_2_SEQ_LM
)
# prepare int-8 model for training
model = prepare_model_for_int8_training(model)
# add LoRA adaptor
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()
# we want to ignore tokenizer pad token in the loss
label_pad_token_id = -100
# Data collator
data_collator = DataCollatorForSeq2Seq(
TOKENIZER,
model=model,
label_pad_token_id=label_pad_token_id,
pad_to_multiple_of=8
)
peft_model_id = args.model_path
output_dir = peft_model_id
training_args = Seq2SeqTrainingArguments(
output_dir=output_dir,
per_device_train_batch_size=32,
learning_rate=1e-3, # higher learning rate
num_train_epochs=3,
logging_dir=f"{output_dir}/logs",
logging_strategy="steps",
logging_steps=500,
save_strategy="no",
# report_to="tensorboard",
)
trainer = Seq2SeqTrainer(
model=model,
args=training_args,
data_collator=data_collator,
train_dataset=tokenized_dataset["train"],
)
model.config.use_cache = False # silence the warnings. Please re-enable for inference!
# train model
trainer.train()
# Save our LoRA model & TOKENIZER results
trainer.model.save_pretrained(output_dir)
TOKENIZER.save_pretrained(output_dir)