Views
No views yet
trained-byt5-smallinput_text): User queries or initial text snippets.output_text): Corresponding generated or relevant text passages with a high relevance rank (rank > 4).(prompt, content) pairs were selected where both prompt and content have non-zero lengths and a relevance rank greater than 4, ensuring high-quality training data.output_text entries were split into chunks of up to 512 characters to manage model input size and to enhance training efficiency.max_length = 512doc_stride = 256 (for handling long texts with overlapping contexts)google/byt5-smalltorch.nn.CrossEntropyLoss) to train the model to predict the next tokens in the sequence.5e-5 and weight decay of 0.01fp16 = False) to prevent issues with NaNs during training.test_loss in the training logs.transformers, torch) and have the model saved in the ./trained-byt5-small directory.1import torch
2from transformers import T5ForConditionalGeneration, ByT5Tokenizer
3
4# Load the trained model and tokenizer
5model_dir = "./trained-byt5-small"
6tokenizer = ByT5Tokenizer.from_pretrained(model_dir)
7model = T5ForConditionalGeneration.from_pretrained(model_dir)
8
9# Move model to device
10device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11model.to(device)
12
13model.eval()
14
15prompt = "¿Cómo implementar un sistema solar en una escuela primaria?"
16
17# Tokenize the input text
18inputs = tokenizer(
19 prompt,
20 return_tensors="pt",
21 max_length=512,
22 truncation=True
23).to(device)
24
25# Generate outputs
26with torch.no_grad():
27 outputs = model.generate(
28 input_ids=inputs['input_ids'],
29 attention_mask=inputs['attention_mask'],
30 max_length=512,
31 num_return_sequences=1,
32 do_sample=True,
33 temperature=0.5,
34 top_k=2000,
35 top_p=0.95,
36 repetition_penalty=1.2,
37 early_stopping=True
38 )
39
40# Decode and print the generated text
41generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
42print(f"Generated Text: {generated_text}")Generated Text: Para implementar un sistema solar en una escuela primaria, se puede comenzar por educar a los estudiantes sobre los planetas y sus características. Luego, se pueden realizar actividades prácticas como construir maquetas del sistema solar, organizar excursiones a planetarios o utilizar software educativo interactivo. Además, es importante fomentar la curiosidad y el interés de los alumnos mediante proyectos de investigación y presentaciones sobre diferentes aspectos del espacio.