CoolFace
Modelpublic

Hamses/EU_Regulation_261_2004

sourceHugging Facecc0-1.0updated 2y agoView on Hugging Face
0likes
Model Card

pip install transformers datasets torch

from datasets import load_dataset

Load your custom dataset (ensure it's in the proper format)

dataset = loaddataset('Hamses/EURegulation2612004', data_files={'train': 'train.txt', 'test': 'test.txt'})

Load the GPT-2 tokenizer

from transformers import GPT2Tokenizer

tokenizer = GPT2Tokenizer.from_pretrained('gpt2')

Preprocess the dataset

def preprocessfunction(examples): return tokenizer(examples['text'], padding='maxlength', truncation=True)

encodeddataset = dataset.map(preprocessfunction, batched=True)

from transformers import GPT2LMHeadModel, TrainingArguments, Trainer

Load the GPT-2 model

model = GPT2LMHeadModel.from_pretrained('gpt2')

Define training arguments

trainingargs = TrainingArguments( outputdir='./results', numtrainepochs=3, perdevicetrainbatchsize=4, perdeviceevalbatchsize=4, warmupsteps=500, weightdecay=0.01, logging_dir='./logs', )

Initialize the Trainer

trainer = Trainer( model=model, args=trainingargs, traindataset=encodeddataset['train'], evaldataset=encoded_dataset['test'] )

Train the model

trainer.train()

Evaluate the model

results = trainer.evaluate() print(results)

Save the model

model.savepretrained('./gpt2-finetuned') tokenizer.savepretrained('./gpt2-finetuned')