From 6016e8009ff8d9b3afa5ae5bafd052ed51d22f74 Mon Sep 17 00:00:00 2001 From: Yash Katariya <yashkatariya@google.com> Date: Thu, 5 Sep 2019 12:10:55 -0700 Subject: [PATCH] Fix more docstrings to be compatible with the doctest format. PiperOrigin-RevId: 267424383 --- tensorflow/python/ops/init_ops.py | 34 ++++++-------- .../ragged_batch_gather_with_default_op.py | 16 +++---- .../python/ops/ragged/ragged_string_ops.py | 44 +++++++++---------- tensorflow/python/ops/ragged/ragged_tensor.py | 19 ++------ 4 files changed, 45 insertions(+), 68 deletions(-) diff --git a/tensorflow/python/ops/init_ops.py b/tensorflow/python/ops/init_ops.py index f300a331e1f..d418fa64c52 100644 --- a/tensorflow/python/ops/init_ops.py +++ b/tensorflow/python/ops/init_ops.py @@ -173,12 +173,8 @@ class Constant(Initializer): of the `value` list, even reshaped, as shown in the two commented lines below the `value` list initialization. - ``` >>> value = [0, 1, 2, 3, 4, 5, 6, 7] - >>> # value = np.array(value) - >>> # value = value.reshape([2, 4]) >>> init = tf.compat.v1.constant_initializer(value) - >>> >>> # fitting shape >>> with tf.compat.v1.Session(): ... x = tf.compat.v1.get_variable('x', shape=[2, 4], initializer=init) @@ -186,30 +182,28 @@ class Constant(Initializer): ... print(x.eval()) [[0. 1. 2. 3.] [4. 5. 6. 7.]] - >>> >>> # Larger shape >>> with tf.compat.v1.Session(): - ... x = tf.compat.v1.get_variable('x', shape=[3, 4], initializer=init) - ... x.initializer.run() - ... print(x.eval()) - [[ 0. 1. 2. 3.] - [ 4. 5. 6. 7.] - [ 7. 7. 7. 7.]] - >>> + ... y = tf.compat.v1.get_variable('y', shape=[3, 4], initializer=init) + ... y.initializer.run() + ... print(y.eval()) + [[0. 1. 2. 3.] + [4. 5. 6. 7.] + [7. 7. 7. 7.]] >>> # Smaller shape >>> with tf.compat.v1.Session(): - ... x = tf.compat.v1.get_variable('x', shape=[2, 3], initializer=init) + ... z = tf.compat.v1.get_variable('z', shape=[2, 3], initializer=init) + Traceback (most recent call last): + ... ValueError: Too many elements provided. Needed at most 6, but received 8 - >>> >>> # Shape verification - >>> init_verify = tf.compat.v1.constant_initializer(value, - verify_shape=True) + >>> init_verify = tf.compat.v1.constant_initializer(value, verify_shape=True) >>> with tf.compat.v1.Session(): - ... x = tf.compat.v1.get_variable('x', shape=[3, 4], - ... initializer=init_verify) + ... u = tf.compat.v1.get_variable('u', shape=[3, 4], + ... initializer=init_verify) + Traceback (most recent call last): + ... TypeError: Expected Tensor's shape: (3, 4), got (8,). - >>> - ``` """ @deprecated_args(None, diff --git a/tensorflow/python/ops/ragged/ragged_batch_gather_with_default_op.py b/tensorflow/python/ops/ragged/ragged_batch_gather_with_default_op.py index b10524c39ad..377fd84f96e 100644 --- a/tensorflow/python/ops/ragged/ragged_batch_gather_with_default_op.py +++ b/tensorflow/python/ops/ragged/ragged_batch_gather_with_default_op.py @@ -59,16 +59,12 @@ def batch_gather_with_default(params, `result.ragged_rank = max(indices.ragged_rank, params.ragged_rank)`. #### Example: - ```python - >>> params = tf.ragged.constant([ - ['a', 'b', 'c'], - ['d'], - [], - ['e']]) - >>> indices = tf.ragged.constant([[1, 2, -1], [], [], [0, 10]]) - >>> batch_gather_with_default(params, indices, 'FOO') - [['b', 'c', 'FOO'], [], [], ['e', 'FOO']] - ``` + + >>> params = tf.ragged.constant([['a', 'b', 'c'], ['d'], [], ['e']]) + >>> indices = tf.ragged.constant([[1, 2, -1], [], [], [0, 10]]) + >>> batch_gather_with_default(params, indices, 'FOO') + <tf.RaggedTensor [[b'b', b'c', b'FOO'], [], [], [b'e', b'FOO']]> + """ with ops.name_scope(name, 'RaggedBatchGatherWithDefault'): params = ragged_tensor.convert_to_tensor_or_ragged_tensor( diff --git a/tensorflow/python/ops/ragged/ragged_string_ops.py b/tensorflow/python/ops/ragged/ragged_string_ops.py index b93b02bc12c..8088e7f0e66 100644 --- a/tensorflow/python/ops/ragged/ragged_string_ops.py +++ b/tensorflow/python/ops/ragged/ragged_string_ops.py @@ -112,11 +112,11 @@ def unicode_encode(input, A `N` dimensional `string` tensor with shape `[D1...DN]`. #### Example: - ```python - >>> input = [[71, 246, 246, 100, 110, 105, 103, 104, 116], [128522]] - >>> unicode_encode(input, 'UTF-8') - ['G\xc3\xb6\xc3\xb6dnight', '\xf0\x9f\x98\x8a'] - ``` + + >>> input = [[71, 246, 246, 100, 110, 105, 103, 104, 116], [128522]] + >>> unicode_encode(input, 'UTF-8') + ['G\xc3\xb6\xc3\xb6dnight', '\xf0\x9f\x98\x8a'] + """ with ops.name_scope(name, "UnicodeEncode", [input]): input_tensor = ragged_tensor.convert_to_tensor_or_ragged_tensor(input) @@ -269,14 +269,14 @@ def unicode_decode_with_offsets(input, `tf.RaggedTensor`s otherwise. #### Example: - ```python - >>> input = [s.encode('utf8') for s in (u'G\xf6\xf6dnight', u'\U0001f60a')] - >>> result = tf.strings.unicode_decode_with_offsets(input, 'UTF-8') - >>> result[0].tolist() # codepoints - [[71, 246, 246, 100, 110, 105, 103, 104, 116], [128522]] - >>> result[1].tolist() # offsets - [[0, 1, 3, 5, 6, 7, 8, 9, 10], [0]] - ``` + + >>> input = [s.encode('utf8') for s in (u'G\xf6\xf6dnight', u'\U0001f60a')] + >>> result = tf.strings.unicode_decode_with_offsets(input, 'UTF-8') + >>> result[0].tolist() # codepoints + [[71, 246, 246, 100, 110, 105, 103, 104, 116], [128522]] + >>> result[1].tolist() # offsets + [[0, 1, 3, 5, 6, 7, 8, 9, 10], [0]] + """ with ops.name_scope(name, "UnicodeDecodeWithOffsets", [input]): return _unicode_decode(input, input_encoding, errors, replacement_char, @@ -374,15 +374,15 @@ def unicode_split_with_offsets(input, `tf.RaggedTensor`s otherwise. #### Example: - ```python - >>> input = [s.encode('utf8') for s in (u'G\xf6\xf6dnight', u'\U0001f60a')] - >>> result = tf.strings.unicode_split_with_offsets(input, 'UTF-8') - >>> result[0].tolist() # character substrings - [['G', '\xc3\xb6', '\xc3\xb6', 'd', 'n', 'i', 'g', 'h', 't'], - ['\xf0\x9f\x98\x8a']] - >>> result[1].tolist() # offsets - [[0, 1, 3, 5, 6, 7, 8, 9, 10], [0]] - ``` + + >>> input = [s.encode('utf8') for s in (u'G\xf6\xf6dnight', u'\U0001f60a')] + >>> result = tf.strings.unicode_split_with_offsets(input, 'UTF-8') + >>> result[0].tolist() # character substrings + [['G', '\xc3\xb6', '\xc3\xb6', 'd', 'n', 'i', 'g', 'h', 't'], + ['\xf0\x9f\x98\x8a']] + >>> result[1].tolist() # offsets + [[0, 1, 3, 5, 6, 7, 8, 9, 10], [0]] + """ with ops.name_scope(name, "UnicodeSplitWithOffsets", [input]): codepoints, offsets = _unicode_decode(input, input_encoding, errors, diff --git a/tensorflow/python/ops/ragged/ragged_tensor.py b/tensorflow/python/ops/ragged/ragged_tensor.py index b5ea9b0145f..07ea19902f9 100644 --- a/tensorflow/python/ops/ragged/ragged_tensor.py +++ b/tensorflow/python/ops/ragged/ragged_tensor.py @@ -104,12 +104,10 @@ class RaggedTensor(composite_tensor.CompositeTensor): Example: - ```python >>> print(tf.RaggedTensor.from_row_splits( - ... values=[3, 1, 4, 1, 5, 9, 2, 6], - ... row_splits=[0, 4, 4, 7, 8, 8])) + ... values=[3, 1, 4, 1, 5, 9, 2, 6], + ... row_splits=[0, 4, 4, 7, 8, 8])) <tf.RaggedTensor [[3, 1, 4, 1], [], [5, 9, 2], [6], []]> - ``` ### Alternative Row-Partitioning Schemes @@ -139,7 +137,6 @@ class RaggedTensor(composite_tensor.CompositeTensor): Example: The following ragged tensors are equivalent, and all represent the nested list `[[3, 1, 4, 1], [], [5, 9, 2], [6], []]`. - ```python >>> values = [3, 1, 4, 1, 5, 9, 2, 6] >>> rt1 = RaggedTensor.from_row_splits(values, row_splits=[0, 4, 4, 7, 8, 8]) >>> rt2 = RaggedTensor.from_row_lengths(values, row_lengths=[4, 0, 3, 1, 0]) @@ -147,7 +144,6 @@ class RaggedTensor(composite_tensor.CompositeTensor): ... values, value_rowids=[0, 0, 0, 0, 2, 2, 2, 3], nrows=5) >>> rt4 = RaggedTensor.from_row_starts(values, row_starts=[0, 4, 4, 7, 8]) >>> rt5 = RaggedTensor.from_row_limits(values, row_limits=[4, 4, 7, 8, 8]) - ``` ### Multiple Ragged Dimensions @@ -155,7 +151,6 @@ class RaggedTensor(composite_tensor.CompositeTensor): a nested `RaggedTensor` for the `values` tensor. Each nested `RaggedTensor` adds a single ragged dimension. - ```python >>> inner_rt = RaggedTensor.from_row_splits( # =rt1 from above ... values=[3, 1, 4, 1, 5, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8]) >>> outer_rt = RaggedTensor.from_row_splits( @@ -164,25 +159,21 @@ class RaggedTensor(composite_tensor.CompositeTensor): [[[3, 1, 4, 1], [], [5, 9, 2]], [], [[6], []]] >>> print outer_rt.ragged_rank 2 - ``` The factory function `RaggedTensor.from_nested_row_splits` may be used to construct a `RaggedTensor` with multiple ragged dimensions directly, by providing a list of `row_splits` tensors: - ```python >>> RaggedTensor.from_nested_row_splits( ... flat_values=[3, 1, 4, 1, 5, 9, 2, 6], ... nested_row_splits=([0, 3, 3, 5], [0, 4, 4, 7, 8, 8])).to_list() [[[3, 1, 4, 1], [], [5, 9, 2]], [], [[6], []]] - ``` ### Uniform Inner Dimensions `RaggedTensor`s with uniform inner dimensions can be defined by using a multidimensional `Tensor` for `values`. - ```python >>> rt = RaggedTensor.from_row_splits(values=tf.ones([5, 3]), .. row_splits=[0, 2, 5]) >>> print rt.to_list() @@ -190,7 +181,6 @@ class RaggedTensor(composite_tensor.CompositeTensor): [[1, 1, 1], [1, 1, 1], [1, 1, 1]]] >>> print rt.shape (2, ?, 3) - ``` ### Uniform Outer Dimensions @@ -200,7 +190,6 @@ class RaggedTensor(composite_tensor.CompositeTensor): constructed with this method from a `RaggedTensor` values with shape `[4, None]`: - ```python >>> values = tf.ragged.constant([[1, 2, 3], [4], [5, 6], [7, 8, 9, 10]]) >>> print values.shape (4, None) @@ -209,17 +198,14 @@ class RaggedTensor(composite_tensor.CompositeTensor): <tf.RaggedTensor [[[1, 2, 3], [4]], [[5, 6], [7, 8, 9, 10]]])> >>> print rt1.shape (2, 2, None) - ``` Note that `rt1` only contains one ragged dimension (the innermost dimension). In contrast, if `from_row_splits` is used to construct a similar `RaggedTensor`, then that `RaggedTensor` will have two ragged dimensions: - ```python >>> rt2 = tf.RaggedTensor.from_row_splits(values, [0, 2, 4]) >>> print rt2.shape (2, None, None) - ``` Uniform and ragged outer dimensions may be interleaved, meaning that a tensor with any combination of ragged and uniform dimensions may be created. @@ -232,6 +218,7 @@ class RaggedTensor(composite_tensor.CompositeTensor): t2 = RaggedTensor.from_uniform_row_length(t1, 8) # [20, 8, None, 2] t3 = RaggedTensor.from_uniform_row_length(t2, 4) # [5, 4, 8, None, 2] t4 = RaggedTensor.from_row_lengths(t3, [...]) # [3, None, 4, 8, None, 2] + ``` """ #=============================================================================