Fill-Mask
Transformers
Safetensors
Korean
Literary Chinese
bert
text-generation
sillok
history
korean-history
classical-chinese
Instructions to use ddokbaro/SillokBert-Scratch with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ddokbaro/SillokBert-Scratch with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("fill-mask", model="ddokbaro/SillokBert-Scratch")# Load model directly from transformers import AutoTokenizer, AutoModelForMaskedLM tokenizer = AutoTokenizer.from_pretrained("ddokbaro/SillokBert-Scratch") model = AutoModelForMaskedLM.from_pretrained("ddokbaro/SillokBert-Scratch", device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 2,903 Bytes
170de4d | 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 | # SillokBert-Scratch ํ๋ก์ ํธ 4๋จ๊ณ: ์ต์ข
๋ชจ๋ธ ํ๊ฐ
# -----------------------------------------------------------------
# 3๋จ๊ณ์์ ์ฌ์ ํ์ต์ ๋ง์น ์ต์ข
๋ชจ๋ธ์ Perplexity(PPL)๋ฅผ ๊ณ์ฐํฉ๋๋ค.
# -----------------------------------------------------------------
import os
import math
from pathlib import Path
from transformers import (
BertForMaskedLM,
PreTrainedTokenizerFast,
DataCollatorForLanguageModeling,
Trainer,
TrainingArguments,
)
from datasets import load_dataset, Dataset
from itertools import chain
def evaluate_sillok_bert():
"""์ฌ์ ํ์ต์ด ์๋ฃ๋ SillokBert-Scratch ๋ชจ๋ธ์ ์ฑ๋ฅ(Perplexity)๋ฅผ ํ๊ฐํฉ๋๋ค."""
# --- ๊ฒฝ๋ก ์ค์ ---
project_dir = Path("/home/work/baro/sillok/sillok_scratch_20250626")
final_model_path = project_dir / "sillokbert_scratch_pretraining_output" / "final_model"
test_dataset_file = "/home/work/baro/sillok25060103/preprocessed_corpus/test.txt"
eval_output_dir = project_dir / "evaluation_output"
print("--- 4. SillokBert-Scratch Model Evaluation ---")
# --- ๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์ ๋ก๋ ---
model = BertForMaskedLM.from_pretrained(final_model_path)
tokenizer = PreTrainedTokenizerFast.from_pretrained(final_model_path)
# --- ๋ฐ์ดํฐ์
์ค๋น ---
test_dataset = load_dataset('text', data_files={'test': test_dataset_file})
block_size = 512
def tokenize_function(examples):
return tokenizer(examples['text'], add_special_tokens=False, return_special_tokens_mask=False)
tokenized_datasets = test_dataset.map(tokenize_function, batched=True, num_proc=4, remove_columns=['text'])
all_input_ids = list(chain(*tokenized_datasets['test']['input_ids']))
total_length = len(all_input_ids)
total_length = (total_length // block_size) * block_size
grouped_input_ids = [all_input_ids[i : i + block_size] for i in range(0, total_length, block_size)]
eval_dataset = Dataset.from_dict({'input_ids': grouped_input_ids})
print(f"์ด {len(eval_dataset)}๊ฐ์ ํ๊ฐ ์ํ์ด ์์ฑ๋์์ต๋๋ค.")
# --- ํ๊ฐ ์ํ ---
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)
training_args = TrainingArguments(output_dir=eval_output_dir, per_device_eval_batch_size=8, fp16=True)
trainer = Trainer(
model=model, args=training_args, data_collator=data_collator, eval_dataset=eval_dataset
)
metrics = trainer.evaluate()
eval_loss = metrics["eval_loss"]
perplexity = math.exp(eval_loss)
# --- ์ต์ข
๊ฒฐ๊ณผ ๋ฐํ ---
print("\n--- ์ต์ข
ํ๊ฐ ๊ฒฐ๊ณผ ---")
print(f" - ์ต์ข
Eval Loss: {eval_loss:.4f}")
print(f" - ์ต์ข
Perplexity(PPL): {perplexity:.4f}")
print("-" * 30)
if __name__ == "__main__":
evaluate_sillok_bert()
|