from transformers import T5ForConditionalGeneration, T5Tokenizer, Trainer,…
- User
from transformers import T5ForConditionalGeneration, T5Tokenizer, Trainer, TrainingArguments from datasets import load_dataset
Load the pre-trained T5 model and tokenizer
model_name = 't5-base' tokenizer = T5Tokenizer.from_pretrained(model_name) model = T5ForConditionalGeneration.from_pretrained(model_name)
Load and prepare the dataset
For demonstration, we'll use the 'wikisplit' dataset
dataset = load_dataset('wikisplit')
train_data = dataset['train'] val_data = dataset['validation']
def preprocess_data(examples): inputs = ["simplify: " + sentence for sentence in examples['input']] targets = examples['output'] model_inputs = tokenizer(inputs, max_length=512, truncation=True, padding='max_length') labels = tokenizer(targets, max_length=512, truncation=True, padding='max_length')
codemodel_inputs['labels'] = labels['input_ids'] return model_inputstokenized_train = train_data.map(preprocess_data, batched=True) tokenized_val = val_data.map(preprocess_data, batched=True)
Define training arguments
training_args = TrainingArguments( output_dir='./t5-simplification', evaluation_strategy='epoch', learning_rate=2e-5, per_device_train_batch_size=4, per_device_eval_batch_size=4, num_train_epochs=3, weight_decay=0.01, save_total_limit=2, save_steps=500, logging_steps=100, fp16=True, # Enable for faster training if using GPU )
Initialize the Trainer
trainer = Trainer( model=model, args=training_args, train_dataset=tokenized_train, eval_dataset=tokenized_val, )
Start Training
trainer.train()
Save the fine-tuned model
trainer.save_model('./t5-simplification') tokenizer.save_pretrained('./t5-simplification')
DatasetNotFoundError Traceback (most recent call last) Cell In[4], line 11 7 model = T5ForConditionalGeneration.from_pretrained(model_name) 9 # Load and prepare the dataset 10 # For demonstration, we'll use the 'wikisplit' dataset ---> 11 dataset = load_dataset('wikisplit') 13 train_data = dataset['train'] 14 val_data = dataset['validation']
File /opt/conda/lib/python3.10/site-packages/datasets/load.py:2074, in load_dataset(path, name, data_dir, data_files, split, cache_dir, features, download_config, download_mode, verification_mode, keep_in_memory, save_infos, revision, token, streaming, num_proc, storage_options, trust_remote_code, **config_kwargs) 2069 verification_mode = VerificationMode( 2070 (verification_mode or VerificationMode.BASIC_CHECKS) if not save_infos else VerificationMode.ALL_CHECKS 2071 ) 2073 # Create a dataset builder -> 2074 builder_instance = load_dataset_builder( 2075 path=path, 2076 name=name, 2077 data_dir=data_dir, 2078 data_files=data_files, 2079 cache_dir=cache_dir, 2080 features=features, 2081 download_config=download_config, 2082 download_mode=download_mode, 2083 revision=revision, 2084 token=token, 2085 storage_options=storage_options, 2086 trust_remote_code=trust_remote_code, 2087 _require_default_config_name=name is None, 2088 **config_kwargs, 2089 ) 2091 # Return iterable dataset in case of streaming 2092 if streaming:
- Assistant