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
| from transformers import pipeline, AutoTokenizer, AutoModelForMaskedLM, Trainer, TrainingArguments from datasets import load_dataset
tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased", clean_up_tokenization_spaces=True) model = AutoModelForMaskedLM.from_pretrained("google-bert/bert-base-uncased")
print("Original model configuration:") print(model.config)
print("Modified model configuration:") print(model.config)
dataset = load_dataset("wikitext", "wikitext-2-raw-v1") def tokenize_function(examples): return tokenizer(examples["text"], padding="max_length", truncation=True)
tokenized_datasets = dataset.map(tokenize_function, batched=True)
training_args = TrainingArguments( output_dir="./results", eval_strategy="epoch", learning_rate=2e-5, per_device_train_batch_size=8, per_device_eval_batch_size=8, num_train_epochs=3, weight_decay=0.01, no_cuda=True, )
trainer = Trainer( model=model, args=training_args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], )
trainer.train()
model.save_pretrained("./trained_model") tokenizer.save_pretrained("./trained_model")
pipe = pipeline("fill-mask", model=model, tokenizer=tokenizer)
result = pipe("you are an [MASK] that I have ever seen") print(result)
|