From 1ccbc833e85e1905166223ae305e1c268853b0b3 Mon Sep 17 00:00:00 2001 From: janasangeetha Date: Wed, 6 Nov 2024 14:33:21 +0530 Subject: [PATCH 1/3] Fix Ruff B008 errors --- tfx/dsl/component/experimental/decorators_test.py | 4 +++- .../component/experimental/decorators_typeddict_test.py | 6 ++++-- tfx/examples/bert/utils/bert_models.py | 9 +++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/tfx/dsl/component/experimental/decorators_test.py b/tfx/dsl/component/experimental/decorators_test.py index 21f3113a32..4458f5dd5a 100644 --- a/tfx/dsl/component/experimental/decorators_test.py +++ b/tfx/dsl/component/experimental/decorators_test.py @@ -140,8 +140,10 @@ def verify_beam_pipeline_arg(a: int) -> OutputDict(b=float): # pytype: disable= def verify_beam_pipeline_arg_non_none_default_value( a: int, - beam_pipeline: BeamComponentParameter[beam.Pipeline] = beam.Pipeline(), + beam_pipeline: BeamComponentParameter[beam.Pipeline] = 0, ) -> OutputDict(b=float): # pytype: disable=invalid-annotation,wrong-arg-types + if beam_pipeline == 0: + beam_pipeline = beam.Pipeline() del beam_pipeline return {'b': float(a)} diff --git a/tfx/dsl/component/experimental/decorators_typeddict_test.py b/tfx/dsl/component/experimental/decorators_typeddict_test.py index 0e4ef8f41f..3d22c182f6 100644 --- a/tfx/dsl/component/experimental/decorators_typeddict_test.py +++ b/tfx/dsl/component/experimental/decorators_typeddict_test.py @@ -140,8 +140,10 @@ def verify_beam_pipeline_arg(a: int) -> TypedDict('Output6', dict(b=float)): # def verify_beam_pipeline_arg_non_none_default_value( a: int, - beam_pipeline: BeamComponentParameter[beam.Pipeline] = beam.Pipeline(), + beam_pipeline: BeamComponentParameter[beam.Pipeline] = 0, ) -> TypedDict('Output7', dict(b=float)): # pytype: disable=wrong-arg-types + if beam_pipeline == 0: + beam_pipeline = beam.Pipeline() del beam_pipeline return {'b': float(a)} @@ -807,4 +809,4 @@ def testListOfArtifacts(self): ], ) - beam_dag_runner.BeamDagRunner().run(test_pipeline) + beam_dag_runner.BeamDagRunner().run(test_pipeline) \ No newline at end of file diff --git a/tfx/examples/bert/utils/bert_models.py b/tfx/examples/bert/utils/bert_models.py index d67fa1c6b0..a75f129f21 100644 --- a/tfx/examples/bert/utils/bert_models.py +++ b/tfx/examples/bert/utils/bert_models.py @@ -59,16 +59,15 @@ def build_bert_classifier(bert_layer: tf.keras.layers.Layer, def compile_bert_classifier( model: tf.keras.Model, - loss: tf.keras.losses.Loss = tf.keras.losses.SparseCategoricalCrossentropy( - from_logits=True), + loss: tf.keras.losses.Loss | None = None, learning_rate: float = 2e-5, metrics: Optional[List[Union[str, tf.keras.metrics.Metric]]] = None): """Compile the BERT classifier using suggested parameters. Args: model: A keras model. Most likely the output of build_bert_classifier. - loss: tf.keras.losses. The suggested loss function expects integer labels - (e.g. 0, 1, 2). If the labels are one-hot encoded, consider using + loss: Default None will use tf.keras.losses. The suggested loss function expects + integer labels (e.g. 0, 1, 2). If the labels are one-hot encoded, consider using tf.keras.lossesCategoricalCrossEntropy with from_logits set to true. learning_rate: Suggested learning rate to be used in tf.keras.optimizer.Adam. The three suggested learning_rates for @@ -79,6 +78,8 @@ def compile_bert_classifier( Returns: None. """ + if loss is None: + loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) if metrics is None: metrics = ["sparse_categorical_accuracy"] From dc27c84d98aae1e80ba76d50a58f33454bfb789b Mon Sep 17 00:00:00 2001 From: janasangeetha Date: Wed, 6 Nov 2024 14:46:36 +0530 Subject: [PATCH 2/3] New line at EOF --- tfx/dsl/component/experimental/decorators_typeddict_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tfx/dsl/component/experimental/decorators_typeddict_test.py b/tfx/dsl/component/experimental/decorators_typeddict_test.py index 3d22c182f6..7bd97d7bd2 100644 --- a/tfx/dsl/component/experimental/decorators_typeddict_test.py +++ b/tfx/dsl/component/experimental/decorators_typeddict_test.py @@ -809,4 +809,4 @@ def testListOfArtifacts(self): ], ) - beam_dag_runner.BeamDagRunner().run(test_pipeline) \ No newline at end of file + beam_dag_runner.BeamDagRunner().run(test_pipeline) From 2f6a67d72b2ac250fdc4896ce93b1a7d76379c4d Mon Sep 17 00:00:00 2001 From: janasangeetha Date: Thu, 7 Nov 2024 09:25:49 +0530 Subject: [PATCH 3/3] Fix Ruff B008 --- tfx/dsl/component/experimental/decorators_test.py | 5 ++--- tfx/dsl/component/experimental/decorators_typeddict_test.py | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/tfx/dsl/component/experimental/decorators_test.py b/tfx/dsl/component/experimental/decorators_test.py index 4458f5dd5a..5757a7bb36 100644 --- a/tfx/dsl/component/experimental/decorators_test.py +++ b/tfx/dsl/component/experimental/decorators_test.py @@ -42,6 +42,7 @@ from tfx.types.system_executions import SystemExecution _TestBeamPipelineArgs = ['--my_testing_beam_pipeline_args=foo'] +_TestEmptyBeamPipeline = beam.Pipeline() class _InputArtifact(types.Artifact): @@ -140,10 +141,8 @@ def verify_beam_pipeline_arg(a: int) -> OutputDict(b=float): # pytype: disable= def verify_beam_pipeline_arg_non_none_default_value( a: int, - beam_pipeline: BeamComponentParameter[beam.Pipeline] = 0, + beam_pipeline: BeamComponentParameter[beam.Pipeline] = _TestEmptyBeamPipeline, ) -> OutputDict(b=float): # pytype: disable=invalid-annotation,wrong-arg-types - if beam_pipeline == 0: - beam_pipeline = beam.Pipeline() del beam_pipeline return {'b': float(a)} diff --git a/tfx/dsl/component/experimental/decorators_typeddict_test.py b/tfx/dsl/component/experimental/decorators_typeddict_test.py index 7bd97d7bd2..b631b812c5 100644 --- a/tfx/dsl/component/experimental/decorators_typeddict_test.py +++ b/tfx/dsl/component/experimental/decorators_typeddict_test.py @@ -40,6 +40,7 @@ from tfx.types.system_executions import SystemExecution _TestBeamPipelineArgs = ['--my_testing_beam_pipeline_args=foo'] +_TestEmptyBeamPipeline = beam.Pipeline() class _InputArtifact(types.Artifact): @@ -140,10 +141,8 @@ def verify_beam_pipeline_arg(a: int) -> TypedDict('Output6', dict(b=float)): # def verify_beam_pipeline_arg_non_none_default_value( a: int, - beam_pipeline: BeamComponentParameter[beam.Pipeline] = 0, + beam_pipeline: BeamComponentParameter[beam.Pipeline] = _TestEmptyBeamPipeline, ) -> TypedDict('Output7', dict(b=float)): # pytype: disable=wrong-arg-types - if beam_pipeline == 0: - beam_pipeline = beam.Pipeline() del beam_pipeline return {'b': float(a)}