Spaces:
Runtime error
Runtime error
File size: 9,062 Bytes
375a093 d13c5f2 375a093 d13c5f2 375a093 50c9e76 375a093 d13c5f2 375a093 d13c5f2 375a093 d13c5f2 375a093 d13c5f2 375a093 d13c5f2 50c9e76 d13c5f2 375a093 d13c5f2 375a093 d13c5f2 375a093 d13c5f2 375a093 d13c5f2 375a093 d13c5f2 375a093 |
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 |
from datasets import load_dataset
import pandas as pd
import numpy as np
import os
import json
import torch
import sys
from torch.utils.data import Dataset, DataLoader
from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
from transformers import Trainer, TrainingArguments, AdamW
torch.backends.cuda.matmul.allow_tf32 = True
model_name = "distilbert-base-uncased"
upsto_abstracts_model_path = './models/uspto_abstracts'
upsto_claims_model_path = './models/uspto_claims'
class USPTODataset(Dataset):
def __init__(self, encodings, labels):
self.encodings = encodings
self.labels = labels
def __getitem__(self, idx):
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
item['labels'] = torch.tensor(self.labels[idx])
return item
def __len__(self):
return len(self.labels)
def LoadDataset():
print("=== LOADING THE DATASET ===")
# Extracting the dataset, filtering only for Jan. 2016
dataset_dict = load_dataset('HUPD/hupd',
name='sample',
data_files="https://huggingface.co/datasets/HUPD/hupd/blob/main/hupd_metadata_2022-02-22.feather",
icpr_label=None,
train_filing_start_date='2016-01-01',
train_filing_end_date='2016-01-21',
val_filing_start_date='2016-01-22',
val_filing_end_date='2016-01-31',
)
print("Separating between training and validation data")
df_train = pd.DataFrame(dataset_dict['train'] )
df_val = pd.DataFrame(dataset_dict['validation'] )
print("=== PRE-PROCESSING THE DATASET ===")
#We are interested in the following columns:
# - Abstract
# - Claims
# - Decision <- our `y`
# Let's preprocess them both out of our training and validation data
# Also, consider that the "Decision" column has three types of values: "Accepted", "Rejected", and "Pending". To remove unecessary baggage, we will be only looking for "Accepted" and "Rejected".
necessary_columns = ["abstract","claims","decision"]
output_values = ['ACCEPTED','REJECTED']
print("Dropping unused columns")
trainFeaturesToDrop = [col for col in list(df_train.columns) if col not in necessary_columns]
trainDF = df_train.dropna()
trainDF.drop(columns=trainFeaturesToDrop, inplace=True)
trainDF = trainDF[trainDF['decision'].isin(output_values)]
valFeaturesToDrop = [col for col in list(df_val.columns) if col not in necessary_columns]
valDF = df_val.dropna()
valDF.drop(columns=valFeaturesToDrop, inplace=True)
valDF = valDF[valDF['decision'].isin(output_values)]
# We need to replace the values in the `decision` column to numerical representations. ]
# We will set "ACCEPTED" as `1` and "REJECTED" as `0`.
print("Replacing values in `decision` column")
yKey = {"ACCEPTED":1,"REJECTED":0}
trainDF2 = trainDF.replace({"decision": yKey})
valDF2 = valDF.replace({"decision": yKey})
# We re-label the `decision` column to `label`.
print("Renaming `decision` to `label`")
trainDF3 = trainDF2.rename(columns={'decision': 'label'})
valDF3 = valDF2.rename(columns={'decision': 'label'})
# We can grab the data for each column so that we have a list of values for training labels,
# training texts, validation labels, and validation texts.
print("Extracting label and text data from dataframes")
trainData = {
"labels":trainDF3["label"].tolist(),
"abstracts":trainDF3["abstract"].tolist(),
"claims":trainDF3["claims"].tolist(),
}
valData = {
"labels":valDF3["label"].tolist(),
"abstracts":valDF3["abstract"].tolist(),
"claims":valDF3["claims"].tolist(),
}
#print(f'TRAINING:\t# labels: {len(trainData["labels"])}\t# texts: {len(trainData["text"])}')
#print(f'VALID:\t# labels: {len(valData["labels"])}\t# texts: {len(valData["text"])}')
if not os.path.exists("./data"):
os.makedirs('./data')
with open("./data/train.json", "w") as outfile:
json.dump(trainData, outfile, indent=2)
with open("./data/val.json", "w") as outfile:
json.dump(valData, outfile, indent=2)
return trainData, valData
def TrainModel(trainData, valData):
print("=== ENCODING DATA ===")
#print(len(trainData["labels"]), len(trainData["text"]), len(valData["labels"]), len(valData["text"]))
print("\t- initializing tokenizer")
tokenizer = DistilBertTokenizerFast.from_pretrained(model_name)
print("\t- encoding training data")
train_abstracts_encodings = tokenizer(trainData["abstracts"], truncation=True, padding=True)
train_claims_encodings = tokenizer(trainData["claims"], truncation=True, padding=True)
#print("\t- encoding validation data")
#val_abstracts_encodings = tokenizer(valData["abstracts"], truncation=True, padding=True)
#val_claims_encodings = tokenizer(valData["claims"], truncation=True, padding=True)
print(trainData["abstracts"][:10])
print(trainData["labels"][:10])
print("=== CREATING DATASETS ===")
print("\t- initializing dataset for training data")
train_abstracts_dataset = USPTODataset(train_abstracts_encodings, trainData["labels"])
train_claims_dataset = USPTODataset(train_claims_encodings, trainData["labels"])
#print("\t- initializing dataset for validation data")
#val_abstracts_dataset = USPTODataset(val_abstracts_encodings, valData["labels"])
#val_claims_dataset = USPTODataset(val_claims_encodings, valData["labels"])
print("=== PREPARING MODEL ===")
print("\t- setting up device")
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
print("\t- initializing model")
model = DistilBertForSequenceClassification.from_pretrained(model_name)
model.to(device)
model.train()
print("== PREPARING TRAINING ===")
print("\t- initializing trainers")
train_abstracts_loader = DataLoader(train_abstracts_dataset, batch_size=4, shuffle=True)
train_claims_loader = DataLoader(train_claims_dataset, batch_size=4, shuffle=True)
#train_claims_loader = DataLoader(train_claims_dataset, batch_size=4, shuffle=True)
print("\t- initializing optim")
optim = AdamW(model.parameters(), lr=5e-5)
def Train(loader, save_path, num_train_epochs=2):
batch_num = len(loader)
for epoch in range(num_train_epochs):
print(f'\t- Training epoch {epoch+1}/{num_train_epochs}')
batch_count = 0
for batch in loader:
print(f'{batch_count}|{batch_num} - {round((batch_count/batch_num)*100)}%', end="")
#print('\t\t- optim zero grad')
optim.zero_grad()
#print('\t\t- input_ids')
input_ids = batch['input_ids'].to(device)
#print('\t\t- attention_mask')
attention_mask = batch['attention_mask'].to(device)
#print('\t\t- labels0')
labels = batch['labels'].to(device)
#print('\t\t- outputs')
outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
#print('\t\t- loss')
loss = outputs[0]
#print('\t\t- backwards')
loss.backward()
#print('\t\t- step')
optim.step()
batch_count += 1
print("\r", end="")
model.eval()
model.save_pretrained(save_path, from_pt=True)
print(f'Saved model in {save_path}!')
print("=== TRAINING ABSTRACTS ===")
Train(train_abstracts_loader,upsto_abstracts_model_path)
print("=== TRAINING CLAIMS ===")
Train(train_claims_loader,upsto_claims_model_path)
def main():
trainDataPath = "./data/train.json"
valDataPath = "./data/val.json"
trainData = None
valData = None
if os.path.exists(trainDataPath) and os.path.exists(valDataPath):
print("Loading from existing data files")
ftrain = open(trainDataPath)
trainData = json.load(ftrain)
ftrain.close()
fval = open(valDataPath)
valData = json.load(fval)
fval.close()
else:
trainData, valData = LoadDataset()
#print(len(trainData["labels"]), len(trainData["text"]), len(valData["labels"]), len(valData["text"]))
print("Data loaded successfully!")
TrainModel(trainData, valData)
"""
train_args = TrainingArguments(
output_dir="./results",
num_train_epochs=2,
per_device_train_batch_size=16,
per_device_eval_batch_size=64,
warmup_steps=500,
learning_rate=5e-5,
weight_decay=0.01,
logging_dir="./logs",
logging_steps=10
)
model = DistilBertForSequenceClassification.from_pretrained(model_name)
trainer = Trainer(
model=model,
args=train_args,
train_dataset=train_dataset,
eval_dataset=val_dataset
)
trainer.train()
"""
if __name__ == "__main__":
main() |