diff --git a/fairness_indicators/example_model.py b/fairness_indicators/example_model.py index 7bfdec2..5708986 100644 --- a/fairness_indicators/example_model.py +++ b/fairness_indicators/example_model.py @@ -1,3 +1,4 @@ + # Copyright 2019 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,6 +20,7 @@ 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 @@ -40,41 +42,50 @@ 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)) + + +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 = tf.cast(text_vector, tf.float32) + output1 = dense1(text_vector) + output2 = dense2(output1) + return tf.keras.Model(inputs=inputs, outputs=output2) def evaluate_model( @@ -83,6 +94,7 @@ def evaluate_model( tfma_eval_result_path, eval_config, ): + """Evaluate Model using Tensorflow Model Analysis. Args: diff --git a/fairness_indicators/example_model_test.py b/fairness_indicators/example_model_test.py index c9df07d..09266a2 100644 --- a/fairness_indicators/example_model_test.py +++ b/fairness_indicators/example_model_test.py @@ -81,7 +81,7 @@ 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]), @@ -89,6 +89,7 @@ def test_example_model(self): 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') diff --git a/setup.py b/setup.py index 0451434..ec804d4 100644 --- a/setup.py +++ b/setup.py @@ -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', ] diff --git a/tensorboard_plugin/setup.py b/tensorboard_plugin/setup.py index 6663771..1c35093 100644 --- a/tensorboard_plugin/setup.py +++ b/tensorboard_plugin/setup.py @@ -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',