Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/course/fr/chapter7/section2_pt.ipynb
Views: 2555
Kernel: Python 3
Classification de token (PyTorch)
Installez les bibliothèques 🤗 Datasets, 🤗 Transformers et 🤗 Accelerate pour exécuter ce notebook.
In [ ]:
!pip install datasets transformers[sentencepiece] !pip install accelerate # Pour exécuter l'entraînement sur TPU, vous devrez décommenter la ligne suivante: # !pip install cloud-tpu-client==0.10 torch==1.9.0 https://storage.googleapis.com/tpu-pytorch/wheels/torch_xla-1.9-cp37-cp37m-linux_x86_64.whl !apt install git-lfs
Vous aurez besoin de configurer git, adaptez votre email et votre nom dans la cellule suivante.
In [ ]:
Vous devrez également être connecté au Hub d'Hugging Face. Exécutez ce qui suit et entrez vos informations d'identification.
In [ ]:
from huggingface_hub import notebook_login notebook_login()
In [ ]:
from datasets import load_dataset raw_datasets = load_dataset("wikiann","fr")
In [ ]:
raw_datasets
In [ ]:
raw_datasets["train"][0]["tokens"]
In [ ]:
raw_datasets["train"][0]["ner_tags"]
In [ ]:
ner_feature = raw_datasets["train"].features["ner_tags"] ner_feature
In [ ]:
label_names = ner_feature.feature.names label_names
In [ ]:
words = raw_datasets["train"][0]["tokens"] labels = raw_datasets["train"][0]["ner_tags"] line1 = "" line2 = "" for word, label in zip(words, labels): full_label = label_names[label] max_length = max(len(word), len(full_label)) line1 += word + " " * (max_length - len(word) + 1) line2 += full_label + " " * (max_length - len(full_label) + 1) print(line1) print(line2)
In [ ]:
from transformers import AutoTokenizer model_checkpoint = "camembert-base" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
In [ ]:
tokenizer.is_fast
In [ ]:
inputs = tokenizer(raw_datasets["train"][0]["tokens"], is_split_into_words=True) inputs.tokens()
In [ ]:
inputs.word_ids()
In [ ]:
def align_labels_with_tokens(labels, word_ids): new_labels = [] current_word = None for word_id in word_ids: if word_id != current_word: # Début d'un nouveau mot ! current_word = word_id label = -100 if word_id is None else labels[word_id] new_labels.append(label) elif word_id is None: # Token special new_labels.append(-100) else: # Même mot que le token précédent label = labels[word_id] # Si l'étiquette est B-XXX, nous la changeons en I-XXX if label % 2 == 1: label += 1 new_labels.append(label) return new_labels
In [ ]:
labels = raw_datasets["train"][0]["ner_tags"] word_ids = inputs.word_ids() print(labels) print(align_labels_with_tokens(labels, word_ids))
In [ ]:
def tokenize_and_align_labels(examples): tokenized_inputs = tokenizer( examples["tokens"], truncation=True, is_split_into_words=True ) all_labels = examples["ner_tags"] new_labels = [] for i, labels in enumerate(all_labels): word_ids = tokenized_inputs.word_ids(i) new_labels.append(align_labels_with_tokens(labels, word_ids)) tokenized_inputs["labels"] = new_labels return tokenized_inputs
In [ ]:
tokenized_datasets = raw_datasets.map( tokenize_and_align_labels, batched=True, remove_columns=raw_datasets["train"].column_names, )
In [ ]:
from transformers import DataCollatorForTokenClassification data_collator = DataCollatorForTokenClassification(tokenizer=tokenizer)
In [ ]:
batch = data_collator([tokenized_datasets["train"][i] for i in range(2)]) batch["labels"]
In [ ]:
for i in range(2): print(tokenized_datasets["train"][i]["labels"])
In [ ]:
!pip install seqeval
In [ ]:
from datasets import load_metric metric = load_metric("seqeval")
In [ ]:
labels = raw_datasets["train"][0]["ner_tags"] labels = [label_names[i] for i in labels] labels
In [ ]:
predictions = labels.copy() predictions[2] = "O" metric.compute(predictions=[predictions], references=[labels])
In [ ]:
import numpy as np def compute_metrics(eval_preds): logits, labels = eval_preds predictions = np.argmax(logits, axis=-1) # Suppression de l'index ignoré (tokens spéciaux) et conversion en étiquettes true_labels = [[label_names[l] for l in label if l != -100] for label in labels] true_predictions = [ [label_names[p] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] all_metrics = metric.compute(predictions=true_predictions, references=true_labels) return { "precision": all_metrics["overall_precision"], "recall": all_metrics["overall_recall"], "f1": all_metrics["overall_f1"], "accuracy": all_metrics["overall_accuracy"], }
In [ ]:
id2label = {str(i): label for i, label in enumerate(label_names)} label2id = {v: k for k, v in id2label.items()}
In [ ]:
from transformers import AutoModelForTokenClassification model = AutoModelForTokenClassification.from_pretrained( model_checkpoint, id2label=id2label, label2id=label2id, )
In [ ]:
model.config.num_labels
In [ ]:
from huggingface_hub import notebook_login notebook_login()
In [ ]:
from transformers import TrainingArguments args = TrainingArguments( "camembert-finetuned-ner", evaluation_strategy="epoch", save_strategy="epoch", learning_rate=2e-5, num_train_epochs=3, weight_decay=0.01, push_to_hub=True, )
In [ ]:
from transformers import Trainer trainer = Trainer( model=model, args=args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], data_collator=data_collator, compute_metrics=compute_metrics, tokenizer=tokenizer, ) trainer.train()
In [ ]:
trainer.push_to_hub(commit_message="Training complete")
In [ ]:
from torch.utils.data import DataLoader train_dataloader = DataLoader( tokenized_datasets["train"], shuffle=True, collate_fn=data_collator, batch_size=8, ) eval_dataloader = DataLoader( tokenized_datasets["validation"], collate_fn=data_collator, batch_size=8 )
In [ ]:
model = AutoModelForTokenClassification.from_pretrained( model_checkpoint, id2label=id2label, label2id=label2id, )
In [ ]:
from torch.optim import AdamW optimizer = AdamW(model.parameters(), lr=2e-5)
In [ ]:
from accelerate import Accelerator accelerator = Accelerator() model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader )
In [ ]:
from transformers import get_scheduler num_train_epochs = 3 num_update_steps_per_epoch = len(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, )
In [ ]:
from huggingface_hub import Repository, get_full_repo_name model_name = "camembert-finetuned-ner-accelerate" repo_name = get_full_repo_name(model_name) repo_name
In [ ]:
output_dir = "camembert-finetuned-ner-accelerate" repo = Repository(output_dir, clone_from=repo_name)
In [ ]:
def postprocess(predictions, labels): predictions = predictions.detach().cpu().clone().numpy() labels = labels.detach().cpu().clone().numpy() # Suppression de l'index ignoré (tokens spéciaux) et conversion en étiquettes true_labels = [[label_names[l] for l in label if l != -100] for label in labels] true_predictions = [ [label_names[p] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] return true_labels, true_predictions
In [ ]:
from tqdm.auto import tqdm import torch progress_bar = tqdm(range(num_training_steps)) for epoch in range(num_train_epochs): # Entraînement model.train() for batch in train_dataloader: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) # Evaluation model.eval() for batch in eval_dataloader: with torch.no_grad(): outputs = model(**batch) predictions = outputs.logits.argmax(dim=-1) labels = batch["labels"] # Nécessaire pour rembourrer les prédictions et les étiquettes à rassembler predictions = accelerator.pad_across_processes(predictions, dim=1, pad_index=-100) labels = accelerator.pad_across_processes(labels, dim=1, pad_index=-100) predictions_gathered = accelerator.gather(predictions) labels_gathered = accelerator.gather(labels) true_predictions, true_labels = postprocess(predictions_gathered, labels_gathered) metric.add_batch(predictions=true_predictions, references=true_labels) results = metric.compute() print( f"epoch {epoch}:", { key: results[f"overall_{key}"] for key in ["precision", "recall", "f1", "accuracy"] }, ) # Sauvegarder et télécharger accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) if accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=f"Training in progress epoch {epoch}", blocking=False )
In [ ]:
accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save)
In [ ]:
from transformers import pipeline # Remplacez par votre propre checkpoint model_checkpoint = "huggingface-course/camembert-finetuned-ner" token_classifier = pipeline( "token-classification", model=model_checkpoint, aggregation_strategy="simple" ) token_classifier("Je m'appelle Sylvain et je travaille à Hugging Face à Brooklyn.")