Skip to content
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

Upgrade tensorflow-model-analysis and tensorflow to >=2.16.2. #374

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
65 changes: 42 additions & 23 deletions fairness_indicators/example_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
results can be visualized using tools like TensorBoard.
"""

from typing import Any

from fairness_indicators import fairness_indicators_metrics # pylint: disable=unused-import
from tensorflow import keras
import tensorflow.compat.v1 as tf
Expand All @@ -40,41 +42,58 @@ class ExampleParser(keras.layers.Layer):

def __init__(self, input_feature_key):
self._input_feature_key = input_feature_key
self.input_spec = keras.layers.InputSpec(shape=(1,), dtype=tf.string)
super().__init__()

def compute_output_shape(self, input_shape: Any):
return [1, 1]

def call(self, serialized_examples):
def get_feature(serialized_example):
parsed_example = tf.io.parse_single_example(
serialized_example, features=FEATURE_MAP
)
return parsed_example[self._input_feature_key]

serialized_examples = tf.cast(serialized_examples, tf.string)
return tf.map_fn(get_feature, serialized_examples)


class ExampleModel(keras.Model):
"""A Example Keras NLP model."""
class Reshaper(keras.layers.Layer):
"""A Keras layer that reshapes the input."""

def __init__(self, input_feature_key):
super().__init__()
self.parser = ExampleParser(input_feature_key)
self.text_vectorization = keras.layers.TextVectorization(
max_tokens=32,
output_mode='int',
output_sequence_length=32,
)
self.text_vectorization.adapt(
['nontoxic', 'toxic comment', 'test comment', 'abc', 'abcdef', 'random']
)
self.dense1 = keras.layers.Dense(32, activation='relu')
self.dense2 = keras.layers.Dense(1)

def call(self, inputs, training=True, mask=None):
parsed_example = self.parser(inputs)
text_vector = self.text_vectorization(parsed_example)
output1 = self.dense1(tf.cast(text_vector, tf.float32))
output2 = self.dense2(output1)
return output2
def call(self, inputs):
return tf.reshape(inputs, (1, 32))


class Caster(keras.layers.Layer):
"""A Keras layer that reshapes the input."""

def call(self, inputs):
return tf.cast(inputs, tf.float32)


def get_example_model(input_feature_key: str):
"""Returns a Keras model for testing."""
parser = ExampleParser(input_feature_key)
text_vectorization = keras.layers.TextVectorization(
max_tokens=32,
output_mode='int',
output_sequence_length=32,
)
text_vectorization.adapt(
['nontoxic', 'toxic comment', 'test comment', 'abc', 'abcdef', 'random']
)
dense1 = keras.layers.Dense(32, activation='relu')
dense2 = keras.layers.Dense(1)

inputs = tf.keras.Input(shape=(), dtype=tf.string)
parsed_example = parser(inputs)
text_vector = text_vectorization(parsed_example)
text_vector = Reshaper()(text_vector)
text_vector = Caster()(text_vector)
output1 = dense1(text_vector)
output2 = dense2(output1)
return tf.keras.Model(inputs=inputs, outputs=output2)


def evaluate_model(
Expand Down
3 changes: 2 additions & 1 deletion fairness_indicators/example_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ def _write_tf_records(self, examples):

def test_example_model(self):
data = self._create_data()
classifier = example_model.ExampleModel(example_model.TEXT_FEATURE)
classifier = example_model.get_example_model(example_model.TEXT_FEATURE)
classifier.compile(optimizer=keras.optimizers.Adam(), loss='mse')
classifier.fit(
tf.constant([e.SerializeToString() for e in data]),
np.array([
e.features.feature[example_model.LABEL].float_list.value[:][0]
for e in data
]),
batch_size=1,
)
classifier.save(self._model_dir, save_format='tf')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
},
"outputs": [],
"source": [
"classifier = example_model.ExampleModel(example_model.TEXT_FEATURE)\n",
"classifier = example_model.get_example_model(example_model.TEXT_FEATURE)\n",
"classifier.compile(optimizer=keras.optimizers.Adam(), loss='mse')\n",
"\n",
"# Read the data from the training file\n",
Expand Down
22 changes: 13 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ def select_constraint(default, nightly=None, git_master=None):
return default

REQUIRED_PACKAGES = [
'tensorflow>=2.15,<2.16',
'tensorflow>=2.16.2,<2.17.0',
'tensorflow-hub>=0.16.1,<1.0.0',
'tensorflow-data-validation' + select_constraint(
default='>=1.15.1,<2.0.0',
nightly='>=1.16.0.dev',
git_master='@git+https://github.com/tensorflow/data-validation@master'),
'tensorflow-model-analysis' + select_constraint(
default='>=0.46,<0.47',
nightly='>=0.47.0.dev',
git_master='@git+https://github.com/tensorflow/model-analysis@master'),
'tensorflow-data-validation'
+ select_constraint(
default='>=1.16.1,<2.0.0',
nightly='>=1.17.0.dev',
git_master='@git+https://github.com/tensorflow/data-validation@master',
),
'tensorflow-model-analysis'
+ select_constraint(
default='>=0.47,<0.48',
nightly='>=0.48.0.dev',
git_master='@git+https://github.com/tensorflow/model-analysis@master',
),
'witwidget>=1.4.4,<2',
'protobuf>=3.20.3,<5',
]
Expand Down
8 changes: 4 additions & 4 deletions tensorboard_plugin/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ def select_constraint(default, nightly=None, git_master=None):

REQUIRED_PACKAGES = [
'protobuf>=3.20.3,<5',
'tensorboard>=2.15.2,<2.16.0',
'tensorflow>=2.15,<2.16',
'tensorboard>=2.16.2,<2.17.0',
'tensorflow>=2.16.2,<2.17.0',
'tensorflow-model-analysis'
+ select_constraint(
default='>=0.46,<0.47',
nightly='>=0.47.0.dev',
default='>=0.47,<0.48',
nightly='>=0.48.0.dev',
git_master='@git+https://github.com/tensorflow/model-analysis@master',
),
'werkzeug<2',
Expand Down
Loading