Spaces:
Configuration error
Configuration error
File size: 2,066 Bytes
68f05a6 978a5b4 31d916b 978a5b4 31d916b 978a5b4 68f05a6 978a5b4 31d916b 978a5b4 31d916b 978a5b4 31d916b 68f05a6 31d916b 68f05a6 |
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 |
import os
import torch
from transformers import AutoTokenizer, AutoModelForTokenClassification, TrainingArguments, Trainer
from datasets import load_dataset, load_metric
# Load dataset
dataset = load_dataset("conll2003")
# Load tokenizer and model checkpoint
model_checkpoint = "dbmdz/bert-large-cased-finetuned-conll03-english"
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
# Tokenize the dataset
def tokenize_and_align_labels(examples):
tokenized_inputs = tokenizer(examples["tokens"], truncation=True, is_split_into_words=True)
return tokenized_inputs
tokenized_datasets = dataset.map(tokenize_and_align_labels, batched=True)
# Load model for token classification (with specified number of labels)
model = AutoModelForTokenClassification.from_pretrained(model_checkpoint, num_labels=9)
# Training arguments
training_args = TrainingArguments(
output_dir="./models/ner_model",
evaluation_strategy="epoch",
save_strategy="epoch",
learning_rate=2e-5,
per_device_train_batch_size=16,
per_device_eval_batch_size=16,
num_train_epochs=3,
weight_decay=0.01,
)
# Load metric
metric = load_metric("seqeval")
def compute_metrics(eval_pred):
predictions, labels = eval_pred
predictions = predictions.argmax(-1)
return metric.compute(predictions=predictions, references=labels)
# Initialize Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_datasets["train"],
eval_dataset=tokenized_datasets["validation"],
tokenizer=tokenizer,
compute_metrics=compute_metrics,
)
# Train model
trainer.train()
# Ensure the output directory exists
output_dir = "./models/ner_model"
os.makedirs(output_dir, exist_ok=True)
# Make sure the model config has a model_type key.
# Since we started with a BERT checkpoint, we set it to "bert".
if not hasattr(model.config, "model_type") or not model.config.model_type:
model.config.model_type = "bert"
# Save the trained model and tokenizer
model.save_pretrained(output_dir)
tokenizer.save_pretrained(output_dir) |