File size: 1,701 Bytes
f04260c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7e4f47
f04260c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7e4f47
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
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, Seq2SeqTrainer, TrainingArguments
from datasets import load_dataset

# Define model and tokenizer names
model_name = "facebook/bart-base"
tokenizer_name = model_name

# Load dataset
dataset = load_dataset("cnn_dailymail", split="train")

# Preprocess data (example) - define your cleaning and tokenization functions here
def preprocess_function(examples):
    inputs = [ex["article"] for ex in examples]
    targets = [ex["highlights"] for ex in examples]
    # Tokenize inputs and targets, add padding
    tokenized_data = tokenizer(inputs, targets, padding="max_length", truncation=True)
    return tokenized_data

# Preprocess train and validation data
train_data = dataset.map(preprocess_function, batched=True)

# Define training arguments
training_args = TrainingArguments(
    output_dir="./outputs",  # any desired output directory
    per_device_train_batch_size=8,
    per_device_eval_batch_size=8,
    num_train_epochs=3,  # Adjust number of epochs for training
    save_steps=10_000,
    evaluation_strategy="epoch",
    logging_steps=500,
    push_to_hub=True,  # Set to True for direct upload to Hub during training
)

# Load pre-trained model and tokenizer
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)

# Define Trainer instance
trainer = Seq2SeqTrainer(
    model=model,
    args=training_args,
    train_dataset=train_data,
    tokenizer=tokenizer,
)

# Start training
trainer.train()

# Model is now trained and uploaded to the Hub if push_to_hub was True

# For manual upload after training, we use the Hub API (refer to Hugging Face documentation)