# Model predicting with fine-tuned model with Keras

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmodel-predicting-with-fine-tuned-model-with-keras%2F19112%3E%5C
**Category:** Beginners\
**Created:** [June 13, 2022, 4:21pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmodel-predicting-with-fine-tuned-model-with-keras%2F19112 "2022-06-13T16:21:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![IonVSmlnlp](https://avatars.discourse-cdn.com/v4/letter/i/c89c15/32.png) [@IonVSmlnlp](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FIonVSmlnlp)%5C
**Post date:** [June 13, 2022, 4:21pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmodel-predicting-with-fine-tuned-model-with-keras%2F19112%2F1 "2022-06-13T16:21:37Z")

</div>

Hi!  
I am implementing the steps provided by the book “Natural Language Processing with Transformers” (by Lewis Tunstall, Leandro von Werra and Thomas Wolf) for text classification; so, I am referring to the chapter 2, “Text classification”.

The fine-tuning I chose to implement was the one suggested at p.50 (Fine-Tuning with Keras).  
The first thing I had to do in order for the model to work was to provide a collate\_fn argument to the to\_tf\_dataset() function, because Keras, apparently, has changed the required arguments since the publishing of the original version book.

The code I used was the following (I followed almost the same steps presented by the book):  
from transformers import TFAutoModelForSequenceClassification  
tf\_model=(TFAutoModelForSequenceClassification.from\_pretrained(model\_cpkt,num\_labels=num\_labels)) #p.46 #of the book

tokenizer\_columns=tokenizer.model\_input\_names

from transformers import DataCollatorWithPadding # Implemented by myself given that the collate\_fn #argument is required

data\_collator = DataCollatorWithPadding(tokenizer=tokenizer, return\_tensors=“tf”)

tf\_train\_dataset=emotions\_encoded[‘train’].to\_tf\_dataset(columns=tokenizer\_columns,label\_cols=[“label”],shuffle=True,batch\_size=16,collate\_fn=data\_collator)  
tf\_eval\_dataset=emotions\_encoded[‘validation’].to\_tf\_dataset(columns=tokenizer\_columns,label\_cols=[“label”],shuffle=False,batch\_size=16,collate\_fn=data\_collator)

tf\_model.compile(  
optimizer=tf.keras.optimizers.Adam(learning\_rate=5e-5),  
loss=tf.keras.losses.SparseCategoricalCrossentropy(from\_logits=True),  
metrics=tf.metrics.SparseCategoricalAccuracy()  
)

tf\_model.fit(tf\_train\_dataset,validation\_data=tf\_eval\_dataset,epochs=2)

The model was trained and returned a good accuracy (above 0,90). Now, my question is the following: what is the syntax for predicting the classes of new texts, for example for the tf\_eval\_dataset PrefetchDataset element ? I tried many configurations for the input tensor, but all failed. I always used the tf\_model.predict() to try to predict new elements.

Second question: what would be the code to predict a given sentence already encoded into a variable ? Will I still have to use the Dataset functions in order to properly encode a given string ?

Thank you !  
Ion
