From 5679c6f03a5f0053e8946c61da99eb3480e133aa Mon Sep 17 00:00:00 2001 From: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com> Date: Fri, 1 Feb 2019 16:11:47 -0600 Subject: [PATCH 1/2] Add example code to tf.data.Dataset.filter() documentation --- tensorflow/python/data/ops/dataset_ops.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tensorflow/python/data/ops/dataset_ops.py b/tensorflow/python/data/ops/dataset_ops.py index 766c6d5395e..6f90b52e69c 100644 --- a/tensorflow/python/data/ops/dataset_ops.py +++ b/tensorflow/python/data/ops/dataset_ops.py @@ -1100,6 +1100,21 @@ class DatasetV2(object): def filter(self, predicate): """Filters this dataset according to `predicate`. + ```python + # NOTE: The following examples use `{ ... }` to represent the + # contents of a dataset. + a = { 1, 2, 3 } + b = { 4, 5, 6, 7 } + + a.filter(lambda x: x < 3) == { 1, 2 } + + # `tf.math.equal(x, y)` is required for equality comparison + def filter_fn(x): + return tf.math.equal(x, 4) + + b.filter(filter_fn) == { 4 } + ``` + Args: predicate: A function mapping a nested structure of tensors (having shapes and types defined by `self.output_shapes` and `self.output_types`) to a From 131270d780a85ccb97e1f7c6e1c92dcee176bc28 Mon Sep 17 00:00:00 2001 From: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com> Date: Fri, 1 Feb 2019 19:57:33 -0600 Subject: [PATCH 2/2] Changed tensor pseudo-code so that the example is runnable --- tensorflow/python/data/ops/dataset_ops.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tensorflow/python/data/ops/dataset_ops.py b/tensorflow/python/data/ops/dataset_ops.py index 6f90b52e69c..338cfb331e9 100644 --- a/tensorflow/python/data/ops/dataset_ops.py +++ b/tensorflow/python/data/ops/dataset_ops.py @@ -1101,18 +1101,15 @@ class DatasetV2(object): """Filters this dataset according to `predicate`. ```python - # NOTE: The following examples use `{ ... }` to represent the - # contents of a dataset. - a = { 1, 2, 3 } - b = { 4, 5, 6, 7 } - - a.filter(lambda x: x < 3) == { 1, 2 } + d = tf.data.Dataset.from_tensor_slices([1, 2, 3]) + + d = d.filter(lambda x: x < 3) # [1, 2] # `tf.math.equal(x, y)` is required for equality comparison def filter_fn(x): - return tf.math.equal(x, 4) + return tf.math.equal(x, 1) - b.filter(filter_fn) == { 4 } + d = d.filter(filter_fn) # [1] ``` Args: