|
from datasets import load_dataset, DatasetDict |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments |
|
|
|
|
|
dataset = load_dataset('yiyang0101/yiyang-test') |
|
|
|
|
|
train_test_split = dataset['train'].train_test_split(test_size=0.2) |
|
datasets_split = DatasetDict({ |
|
'train': train_test_split['train'], |
|
'validation': train_test_split['test'] |
|
}) |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") |
|
model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=2) |
|
|
|
|
|
def tokenize_function(example): |
|
return tokenizer(example['text'], padding="max_length", truncation=True) |
|
|
|
|
|
tokenized_datasets = datasets_split.map(tokenize_function, batched=True) |
|
|
|
|
|
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, |
|
push_to_hub=False |
|
) |
|
|
|
|
|
trainer = Trainer( |
|
model=model, |
|
args=training_args, |
|
train_dataset=tokenized_datasets['train'], |
|
eval_dataset=tokenized_datasets['validation'], |
|
) |
|
|
|
|
|
trainer.train() |
|
|
|
|
|
trainer.save_model("./") |
|
|
|
|
|
trainer.push_to_hub(repo_id="yiyang0101/yiyang-test", use_temp_dir=True) |
|
tokenizer.push_to_hub(repo_id="yiyang0101/yiyang-test") |
|
|