|
from datasets import load_dataset
|
|
dataset = load_dataset("yelp_review_full")
|
|
|
|
|
|
print(dataset["train"][100])
|
|
|
|
from transformers import AutoTokenizer
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
|
|
|
|
def tokenize_function(examples):
|
|
return tokenizer(examples["text"], padding="max_length", truncation=True)
|
|
|
|
tokenized_datasets = dataset.map(tokenize_function, batched=True)
|
|
|
|
small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(100))
|
|
small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(100))
|
|
|
|
from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
|
|
|
|
model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", num_labels=5)
|
|
|
|
training_args = TrainingArguments(
|
|
output_dir="./results",
|
|
evaluation_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,
|
|
)
|
|
|
|
trainer = Trainer(
|
|
model=model,
|
|
args=training_args,
|
|
train_dataset=small_train_dataset,
|
|
eval_dataset=small_eval_dataset,
|
|
)
|
|
|
|
trainer.train()
|
|
|