Views
No views yet
text_classification_beto_tf_9_24_20241 = requires search, 0 = no search).input_text): Text inputs requiring classification.requires_search): Binary labels indicating whether the prompt necessitates a search (1) or not (0).(prompt, requires_search) pairs were selected where both prompt and requires_search fields are non-empty, ensuring high-quality and relevant training data.pandas to load data from a CSV file containing the necessary columns.BertTokenizer) suitable for Spanish text.truncation=True: Truncate sequences longer than the maximum length.padding=True: Pad shorter sequences to the maximum length.max_length=512: Set maximum token length to 512 tokens.dccuchile/bert-base-spanish-wwm-casedTFBertForSequenceClassification adapted for binary classification (num_labels=2).SparseCategoricalCrossentropy with from_logits=True to handle integer labels directly.5e-5 and weight decay of 0.01.SparseCategoricalAccuracy to monitor classification accuracy during training.SparseCategoricalCrossentropy on both training and validation sets.Test Loss and Test Sparse Categorical Accuracy.transformers, tensorflow, pandas, etc.) and have the model saved in the specified output_dir.1import tensorflow as tf
2from transformers import BertTokenizer, TFBertForSequenceClassification
3
4# Load the trained model and tokenizer
5model_dir = "./text_classification_beto_tf_9_24_2024"
6tokenizer = BertTokenizer.from_pretrained(model_dir)
7model = TFBertForSequenceClassification.from_pretrained(model_dir)
8
9# Prepare the input
10prompt = "¿Cómo puedo mejorar la eficiencia energética en mi hogar?"
11
12# Tokenize the input
13inputs = tokenizer(
14 prompt,
15 return_tensors="tf",
16 max_length=512,
17 truncation=True,
18 padding=True
19)
20
21# Perform prediction
22outputs = model(inputs)
23logits = outputs.logits
24predicted_class = tf.argmax(logits, axis=1).numpy()[0]
25
26# Interpret the result
27if predicted_class == 1:
28 print("Requiere búsqueda: Sí")
29else:
30 print("Requiere búsqueda: No")Requiere búsqueda: Sí