2024-11-08 20:50:41 +09:00
|
|
|
# %%
|
|
|
|
|
|
|
|
# from datasets import load_from_disk
|
|
|
|
import os
|
|
|
|
|
|
|
|
os.environ['NCCL_P2P_DISABLE'] = '1'
|
|
|
|
os.environ['NCCL_IB_DISABLE'] = '1'
|
|
|
|
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
|
|
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3"
|
|
|
|
|
|
|
|
import torch
|
|
|
|
from transformers import (
|
|
|
|
AutoTokenizer,
|
|
|
|
AutoModelForSequenceClassification,
|
|
|
|
DataCollatorWithPadding,
|
|
|
|
Trainer,
|
|
|
|
EarlyStoppingCallback,
|
|
|
|
TrainingArguments
|
|
|
|
)
|
|
|
|
import evaluate
|
|
|
|
import numpy as np
|
|
|
|
import pandas as pd
|
|
|
|
# import matplotlib.pyplot as plt
|
|
|
|
from datasets import Dataset, DatasetDict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
torch.set_float32_matmul_precision('high')
|
|
|
|
|
|
|
|
# %%
|
|
|
|
|
|
|
|
# we need to create the mdm_list
|
|
|
|
# import the full mdm-only file
|
2024-11-20 15:07:47 +09:00
|
|
|
# data_path = '../../data_import/exports/data_mapping_mdm.csv'
|
|
|
|
# full_df = pd.read_csv(data_path, skipinitialspace=True)
|
|
|
|
# rather than use pattern, we use the real thing and property
|
|
|
|
|
2024-11-08 20:50:41 +09:00
|
|
|
|
|
|
|
# %%
|
2024-11-20 15:07:47 +09:00
|
|
|
id2label = {0: False, 1: True}
|
|
|
|
label2id = {False: 0, True: 1}
|
2024-11-08 20:50:41 +09:00
|
|
|
|
|
|
|
# %%
|
|
|
|
|
|
|
|
# outputs a list of dictionaries
|
|
|
|
# processes dataframe into lists of dictionaries
|
|
|
|
# each element maps input to output
|
|
|
|
# input: tag_description
|
|
|
|
# output: class label
|
2024-11-20 15:07:47 +09:00
|
|
|
def process_df_to_dict(df):
|
2024-11-08 20:50:41 +09:00
|
|
|
output_list = []
|
|
|
|
for _, row in df.iterrows():
|
|
|
|
desc = f"<DESC>{row['tag_description']}<DESC>"
|
2024-11-10 20:28:47 +09:00
|
|
|
unit = f"<UNIT>{row['unit']}<UNIT>"
|
2024-11-20 15:07:47 +09:00
|
|
|
in_mdm_label = int(row['MDM'])
|
2024-11-08 20:50:41 +09:00
|
|
|
element = {
|
2024-11-10 20:28:47 +09:00
|
|
|
'text' : f"{desc}{unit}",
|
2024-11-20 15:07:47 +09:00
|
|
|
'label': in_mdm_label,
|
2024-11-08 20:50:41 +09:00
|
|
|
}
|
|
|
|
output_list.append(element)
|
|
|
|
|
|
|
|
return output_list
|
|
|
|
|
|
|
|
|
2024-11-20 15:07:47 +09:00
|
|
|
def create_split_dataset(fold):
|
2024-11-08 20:50:41 +09:00
|
|
|
# train
|
2024-11-20 15:07:47 +09:00
|
|
|
# data_path = f"../../data_preprocess/exports/dataset/group_{fold}/train_all.csv"
|
|
|
|
|
|
|
|
# reconstruct full training data with non-mdm data
|
|
|
|
data_path = f"../../data_preprocess/exports/dataset/group_{fold}/test_all.csv"
|
|
|
|
test_df = pd.read_csv(data_path, skipinitialspace=True)
|
|
|
|
ships_list = list(set(test_df['ships_idx']))
|
|
|
|
data_path = '../../data_preprocess/exports/preprocessed_data.csv'
|
|
|
|
full_df = pd.read_csv(data_path, skipinitialspace=True)
|
|
|
|
train_df = full_df[~full_df['ships_idx'].isin(ships_list)]
|
2024-11-08 20:50:41 +09:00
|
|
|
|
|
|
|
# valid
|
|
|
|
data_path = f"../../data_preprocess/exports/dataset/group_{fold}/valid.csv"
|
|
|
|
validation_df = pd.read_csv(data_path, skipinitialspace=True)
|
|
|
|
|
|
|
|
combined_data = DatasetDict({
|
2024-11-20 15:07:47 +09:00
|
|
|
'train': Dataset.from_list(process_df_to_dict(train_df)),
|
|
|
|
'validation' : Dataset.from_list(process_df_to_dict(validation_df)),
|
2024-11-08 20:50:41 +09:00
|
|
|
})
|
|
|
|
return combined_data
|
|
|
|
|
|
|
|
|
|
|
|
# %%
|
|
|
|
|
|
|
|
# function to perform training for a given fold
|
2024-11-10 20:28:47 +09:00
|
|
|
def train(fold):
|
|
|
|
|
|
|
|
save_path = f'checkpoint_fold_{fold}'
|
2024-11-20 15:07:47 +09:00
|
|
|
split_datasets = create_split_dataset(fold)
|
2024-11-10 20:28:47 +09:00
|
|
|
|
|
|
|
# prepare tokenizer
|
|
|
|
|
|
|
|
model_checkpoint = "distilbert/distilbert-base-uncased"
|
2024-11-20 15:07:47 +09:00
|
|
|
# model_checkpoint = 'google-bert/bert-base-uncased'
|
2024-11-10 20:28:47 +09:00
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint, return_tensors="pt", clean_up_tokenization_spaces=True)
|
|
|
|
# Define additional special tokens
|
|
|
|
additional_special_tokens = ["<THING_START>", "<THING_END>", "<PROPERTY_START>", "<PROPERTY_END>", "<NAME>", "<DESC>", "<SIG>", "<UNIT>", "<DATA_TYPE>"]
|
|
|
|
# Add the additional special tokens to the tokenizer
|
|
|
|
tokenizer.add_special_tokens({"additional_special_tokens": additional_special_tokens})
|
|
|
|
|
|
|
|
max_length = 120
|
|
|
|
|
|
|
|
# given a dataset entry, run it through the tokenizer
|
|
|
|
def preprocess_function(example):
|
|
|
|
input = example['text']
|
|
|
|
# text_target sets the corresponding label to inputs
|
|
|
|
# there is no need to create a separate 'labels'
|
|
|
|
model_inputs = tokenizer(
|
|
|
|
input,
|
|
|
|
max_length=max_length,
|
|
|
|
truncation=True,
|
|
|
|
padding=True
|
|
|
|
)
|
|
|
|
return model_inputs
|
|
|
|
|
|
|
|
# map maps function to each "row" in the dataset
|
|
|
|
# aka the data in the immediate nesting
|
|
|
|
tokenized_datasets = split_datasets.map(
|
|
|
|
preprocess_function,
|
|
|
|
batched=True,
|
|
|
|
num_proc=8,
|
|
|
|
remove_columns="text",
|
2024-11-08 20:50:41 +09:00
|
|
|
)
|
|
|
|
|
2024-11-10 20:28:47 +09:00
|
|
|
# %% temp
|
|
|
|
# tokenized_datasets['train'].rename_columns()
|
|
|
|
|
|
|
|
# %%
|
|
|
|
# create data collator
|
|
|
|
|
|
|
|
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
|
|
|
|
|
|
|
|
# %%
|
|
|
|
# compute metrics
|
|
|
|
metric = evaluate.load("accuracy")
|
|
|
|
|
|
|
|
|
|
|
|
def compute_metrics(eval_preds):
|
|
|
|
preds, labels = eval_preds
|
|
|
|
preds = np.argmax(preds, axis=1)
|
|
|
|
return metric.compute(predictions=preds, references=labels)
|
|
|
|
|
|
|
|
# %%
|
|
|
|
# create id2label and label2id
|
|
|
|
|
|
|
|
|
|
|
|
# %%
|
|
|
|
model = AutoModelForSequenceClassification.from_pretrained(
|
|
|
|
model_checkpoint,
|
2024-11-20 15:07:47 +09:00
|
|
|
num_labels=2)
|
2024-11-10 20:28:47 +09:00
|
|
|
# important! after extending tokens vocab
|
|
|
|
model.resize_token_embeddings(len(tokenizer))
|
|
|
|
|
|
|
|
# model = torch.compile(model, backend="inductor", dynamic=True)
|
|
|
|
|
|
|
|
|
|
|
|
# %%
|
|
|
|
# Trainer
|
|
|
|
|
|
|
|
training_args = TrainingArguments(
|
|
|
|
output_dir=f"{save_path}",
|
|
|
|
# eval_strategy="epoch",
|
|
|
|
eval_strategy="no",
|
|
|
|
logging_dir="tensorboard-log",
|
|
|
|
logging_strategy="epoch",
|
|
|
|
# save_strategy="epoch",
|
|
|
|
load_best_model_at_end=False,
|
|
|
|
learning_rate=1e-5,
|
|
|
|
per_device_train_batch_size=64,
|
|
|
|
per_device_eval_batch_size=64,
|
|
|
|
auto_find_batch_size=False,
|
|
|
|
ddp_find_unused_parameters=False,
|
|
|
|
weight_decay=0.01,
|
|
|
|
save_total_limit=1,
|
2024-11-20 15:07:47 +09:00
|
|
|
num_train_epochs=40,
|
2024-11-10 20:28:47 +09:00
|
|
|
bf16=True,
|
|
|
|
push_to_hub=False,
|
|
|
|
remove_unused_columns=False,
|
|
|
|
)
|
2024-11-08 20:50:41 +09:00
|
|
|
|
|
|
|
|
2024-11-10 20:28:47 +09:00
|
|
|
trainer = Trainer(
|
|
|
|
model,
|
|
|
|
training_args,
|
|
|
|
train_dataset=tokenized_datasets["train"],
|
|
|
|
eval_dataset=tokenized_datasets["validation"],
|
|
|
|
tokenizer=tokenizer,
|
|
|
|
data_collator=data_collator,
|
|
|
|
compute_metrics=compute_metrics,
|
|
|
|
# callbacks=[EarlyStoppingCallback(early_stopping_patience=3)],
|
|
|
|
)
|
2024-11-08 20:50:41 +09:00
|
|
|
|
2024-11-10 20:28:47 +09:00
|
|
|
# uncomment to load training from checkpoint
|
|
|
|
# checkpoint_path = 'default_40_1/checkpoint-5600'
|
|
|
|
# trainer.train(resume_from_checkpoint=checkpoint_path)
|
2024-11-08 20:50:41 +09:00
|
|
|
|
2024-11-10 20:28:47 +09:00
|
|
|
trainer.train()
|
2024-11-08 20:50:41 +09:00
|
|
|
|
2024-11-10 20:28:47 +09:00
|
|
|
# execute training
|
|
|
|
for fold in [1,2,3,4,5]:
|
|
|
|
print(fold)
|
|
|
|
train(fold)
|
2024-11-08 20:50:41 +09:00
|
|
|
|
|
|
|
|
|
|
|
# %%
|