2024-09-26 10:59:38 +00:00
|
|
|
from transformers import T5ForConditionalGeneration, T5Tokenizer, Trainer, TrainingArguments
|
2024-08-16 14:35:54 +00:00
|
|
|
from datasets import load_dataset
|
|
|
|
|
2024-10-11 07:22:18 +00:00
|
|
|
|
2024-11-11 22:51:16 +00:00
|
|
|
model_name = "T5Autocorrection_Book3"
|
|
|
|
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
2024-09-26 10:59:38 +00:00
|
|
|
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
2024-08-16 14:35:54 +00:00
|
|
|
|
|
|
|
def preprocess_function(examples):
|
2024-09-26 10:59:38 +00:00
|
|
|
before_list = []
|
|
|
|
after_list = []
|
|
|
|
for ex in examples["before after"]:
|
|
|
|
if ex is not None:
|
|
|
|
splits = ex.split(" before after ")
|
2024-10-11 07:22:18 +00:00
|
|
|
before_list.append(splits[0] if len(splits) == 2 else ex)
|
|
|
|
after_list.append(splits[1] if len(splits) == 2 else '')
|
2024-09-26 10:59:38 +00:00
|
|
|
else:
|
|
|
|
before_list.append('')
|
|
|
|
after_list.append('')
|
|
|
|
|
2024-10-11 07:22:18 +00:00
|
|
|
model_inputs = tokenizer(before_list, padding="max_length", truncation=True, max_length=512)
|
|
|
|
labels = tokenizer(after_list, padding="max_length", truncation=True, max_length=512)
|
2024-08-16 14:35:54 +00:00
|
|
|
model_inputs["labels"] = labels["input_ids"]
|
|
|
|
return model_inputs
|
|
|
|
|
2024-11-11 22:51:16 +00:00
|
|
|
dataset = load_dataset("csv", data_files={"train": "filtered_book_typos.csv"}, delimiter=",", column_names=["before after"])
|
2024-09-26 10:59:38 +00:00
|
|
|
|
2024-10-11 07:22:18 +00:00
|
|
|
|
2024-09-26 10:59:38 +00:00
|
|
|
tokenized_datasets = dataset.map(preprocess_function, batched=True)
|
2024-08-16 14:35:54 +00:00
|
|
|
|
2024-10-11 07:22:18 +00:00
|
|
|
|
2024-08-16 14:35:54 +00:00
|
|
|
training_args = TrainingArguments(
|
2024-11-11 22:51:16 +00:00
|
|
|
output_dir="./results_book3",
|
2024-09-26 10:59:38 +00:00
|
|
|
save_strategy="epoch",
|
2024-08-16 14:35:54 +00:00
|
|
|
learning_rate=2e-5,
|
2024-10-11 07:22:18 +00:00
|
|
|
per_device_train_batch_size=1,
|
|
|
|
per_device_eval_batch_size=1,
|
2024-11-10 11:40:38 +00:00
|
|
|
num_train_epochs=5,
|
2024-08-16 14:35:54 +00:00
|
|
|
weight_decay=0.01,
|
2024-10-11 07:22:18 +00:00
|
|
|
gradient_accumulation_steps=64,
|
|
|
|
fp16=True,
|
2024-08-16 14:35:54 +00:00
|
|
|
)
|
|
|
|
|
2024-10-11 07:22:18 +00:00
|
|
|
|
2024-08-16 14:35:54 +00:00
|
|
|
trainer = Trainer(
|
|
|
|
model=model,
|
|
|
|
args=training_args,
|
|
|
|
train_dataset=tokenized_datasets["train"],
|
2024-09-26 10:59:38 +00:00
|
|
|
tokenizer=tokenizer,
|
2024-08-16 14:35:54 +00:00
|
|
|
)
|
|
|
|
|
2024-10-11 07:22:18 +00:00
|
|
|
|
2024-09-26 10:59:38 +00:00
|
|
|
trainer.train()
|
2024-10-11 07:22:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
for param in model.parameters():
|
|
|
|
param.data = param.data.contiguous()
|
|
|
|
|
|
|
|
|
2024-11-11 22:51:16 +00:00
|
|
|
model.save_pretrained("T5Autocorrection_Book_Typos", safe_serialization=False) # Отключаем safetensors для простого сохранения
|
|
|
|
tokenizer.save_pretrained("T5Autocorrection_Book_Typos")
|