Skip to content

Commit

Permalink
Upgrade tensorflow-model-analysis and tensorflow to >=2.16.2.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 705990022
  • Loading branch information
vkarampudi authored and Responsible ML Infra Team committed Dec 17, 2024
1 parent 8789c99 commit 3525d1a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 37 deletions.
58 changes: 35 additions & 23 deletions fairness_indicators/example_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -83,6 +94,7 @@ def evaluate_model(
tfma_eval_result_path,
eval_config,
):

"""Evaluate Model using Tensorflow Model Analysis.
Args:
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
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

0 comments on commit 3525d1a

Please sign in to comment.