Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions examples/nlp/tweet-classification-using-tfdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
import keras
import tensorflow_hub as hub
from tensorflow.keras import layers
import tensorflow_decision_forests as tfdf
import matplotlib.pyplot as plt

Expand Down Expand Up @@ -158,9 +157,16 @@ def create_dataset(dataframe):

"""

sentence_encoder_layer = hub.KerasLayer(
"https://tfhub.dev/google/universal-sentence-encoder/4"
)
sentence_encoder_url = "https://tfhub.dev/google/universal-sentence-encoder/4"

class SentenceEncoderLayer(keras.layers.Layer):
def __init__(self, **kwargs):
super(SentenceEncoderLayer, self).__init__(**kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better maintainability and to adhere to modern Python 3 conventions, it's recommended to use the zero-argument super() call.

Suggested change
super(SentenceEncoderLayer, self).__init__(**kwargs)
super().__init__(**kwargs)

self.encoder = hub.KerasLayer(sentence_encoder_url)

def call(self, inputs):
return self.encoder(inputs)
Comment on lines +167 to +168
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It's a good practice for custom layers to accept and forward the training argument in the call method. This ensures the layer behaves correctly in both training and inference modes, even if the underlying hub.KerasLayer doesn't change its behavior in this case. It makes the layer more robust and aligned with Keras API conventions.

Suggested change
def call(self, inputs):
return self.encoder(inputs)
def call(self, inputs, training=False):
return self.encoder(inputs, training=training)



"""
## Creating our models
Expand All @@ -175,8 +181,8 @@ def create_dataset(dataframe):
Building model_1
"""

inputs = layers.Input(shape=(), dtype=tf.string)
outputs = sentence_encoder_layer(inputs)
inputs = keras.layers.Input(shape=(), dtype="string")
outputs = SentenceEncoderLayer()(inputs)
preprocessor = keras.Model(inputs=inputs, outputs=outputs)
model_1 = tfdf.keras.GradientBoostedTreesModel(preprocessing=preprocessor)

Expand Down Expand Up @@ -278,9 +284,9 @@ def plot_curve(logs):

test_df.reset_index(inplace=True, drop=True)
for index, row in test_df.iterrows():
text = tf.expand_dims(row["text"], axis=0)
text = keras.ops.expand_dims(row["text"], axis=0)
preds = model_1.predict_step(text)
preds = tf.squeeze(tf.round(preds))
preds = keras.ops.squeeze(keras.ops.round(preds))
Comment on lines 288 to +289
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While predict_step works, it's not part of the public Keras API and is intended for internal use. The recommended public API for getting predictions is model.predict(). It's more idiomatic and returns NumPy arrays, which simplifies the subsequent processing. Using predict() would make the code more robust to future Keras updates.

Suggested change
preds = model_1.predict_step(text)
preds = tf.squeeze(tf.round(preds))
preds = keras.ops.squeeze(keras.ops.round(preds))
preds = model_1.predict(text)
preds = np.squeeze(np.round(preds))

print(f"Text: {row['text']}")
print(f"Prediction: {int(preds)}")
print(f"Ground Truth : {row['target']}")
Expand Down