-
Notifications
You must be signed in to change notification settings - Fork 2.1k
migrate tweet classification example to use keras 3 #2211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||||||
|
|
||||||||||||
|
|
@@ -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) | ||||||||||||
| self.encoder = hub.KerasLayer(sentence_encoder_url) | ||||||||||||
|
|
||||||||||||
| def call(self, inputs): | ||||||||||||
| return self.encoder(inputs) | ||||||||||||
|
Comment on lines
+167
to
+168
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a good practice for custom layers to accept and forward the
Suggested change
|
||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| """ | ||||||||||||
| ## Creating our models | ||||||||||||
|
|
@@ -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) | ||||||||||||
|
|
||||||||||||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While
Suggested change
|
||||||||||||
| print(f"Text: {row['text']}") | ||||||||||||
| print(f"Prediction: {int(preds)}") | ||||||||||||
| print(f"Ground Truth : {row['target']}") | ||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better maintainability and to adhere to modern Python 3 conventions, it's recommended to use the zero-argument
super()call.