Fix more docstrings to be compatible with the doctest format.
PiperOrigin-RevId: 267424383
This commit is contained in:
parent
eee9afd6db
commit
6016e8009f
tensorflow/python/ops
@ -173,12 +173,8 @@ class Constant(Initializer):
|
|||||||
of the `value` list, even reshaped, as shown in the two commented lines
|
of the `value` list, even reshaped, as shown in the two commented lines
|
||||||
below the `value` list initialization.
|
below the `value` list initialization.
|
||||||
|
|
||||||
```
|
|
||||||
>>> value = [0, 1, 2, 3, 4, 5, 6, 7]
|
>>> 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)
|
>>> init = tf.compat.v1.constant_initializer(value)
|
||||||
>>>
|
|
||||||
>>> # fitting shape
|
>>> # fitting shape
|
||||||
>>> with tf.compat.v1.Session():
|
>>> with tf.compat.v1.Session():
|
||||||
... x = tf.compat.v1.get_variable('x', shape=[2, 4], initializer=init)
|
... x = tf.compat.v1.get_variable('x', shape=[2, 4], initializer=init)
|
||||||
@ -186,30 +182,28 @@ class Constant(Initializer):
|
|||||||
... print(x.eval())
|
... print(x.eval())
|
||||||
[[0. 1. 2. 3.]
|
[[0. 1. 2. 3.]
|
||||||
[4. 5. 6. 7.]]
|
[4. 5. 6. 7.]]
|
||||||
>>>
|
|
||||||
>>> # Larger shape
|
>>> # Larger shape
|
||||||
>>> with tf.compat.v1.Session():
|
>>> with tf.compat.v1.Session():
|
||||||
... x = tf.compat.v1.get_variable('x', shape=[3, 4], initializer=init)
|
... y = tf.compat.v1.get_variable('y', shape=[3, 4], initializer=init)
|
||||||
... x.initializer.run()
|
... y.initializer.run()
|
||||||
... print(x.eval())
|
... print(y.eval())
|
||||||
[[ 0. 1. 2. 3.]
|
[[0. 1. 2. 3.]
|
||||||
[ 4. 5. 6. 7.]
|
[4. 5. 6. 7.]
|
||||||
[ 7. 7. 7. 7.]]
|
[7. 7. 7. 7.]]
|
||||||
>>>
|
|
||||||
>>> # Smaller shape
|
>>> # Smaller shape
|
||||||
>>> with tf.compat.v1.Session():
|
>>> 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
|
ValueError: Too many elements provided. Needed at most 6, but received 8
|
||||||
>>>
|
|
||||||
>>> # Shape verification
|
>>> # Shape verification
|
||||||
>>> init_verify = tf.compat.v1.constant_initializer(value,
|
>>> init_verify = tf.compat.v1.constant_initializer(value, verify_shape=True)
|
||||||
verify_shape=True)
|
|
||||||
>>> with tf.compat.v1.Session():
|
>>> with tf.compat.v1.Session():
|
||||||
... x = tf.compat.v1.get_variable('x', shape=[3, 4],
|
... u = tf.compat.v1.get_variable('u', shape=[3, 4],
|
||||||
... initializer=init_verify)
|
... initializer=init_verify)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
TypeError: Expected Tensor's shape: (3, 4), got (8,).
|
TypeError: Expected Tensor's shape: (3, 4), got (8,).
|
||||||
>>>
|
|
||||||
```
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@deprecated_args(None,
|
@deprecated_args(None,
|
||||||
|
@ -59,16 +59,12 @@ def batch_gather_with_default(params,
|
|||||||
`result.ragged_rank = max(indices.ragged_rank, params.ragged_rank)`.
|
`result.ragged_rank = max(indices.ragged_rank, params.ragged_rank)`.
|
||||||
|
|
||||||
#### Example:
|
#### Example:
|
||||||
```python
|
|
||||||
>>> params = tf.ragged.constant([
|
>>> params = tf.ragged.constant([['a', 'b', 'c'], ['d'], [], ['e']])
|
||||||
['a', 'b', 'c'],
|
|
||||||
['d'],
|
|
||||||
[],
|
|
||||||
['e']])
|
|
||||||
>>> indices = tf.ragged.constant([[1, 2, -1], [], [], [0, 10]])
|
>>> indices = tf.ragged.constant([[1, 2, -1], [], [], [0, 10]])
|
||||||
>>> batch_gather_with_default(params, indices, 'FOO')
|
>>> batch_gather_with_default(params, indices, 'FOO')
|
||||||
[['b', 'c', 'FOO'], [], [], ['e', 'FOO']]
|
<tf.RaggedTensor [[b'b', b'c', b'FOO'], [], [], [b'e', b'FOO']]>
|
||||||
```
|
|
||||||
"""
|
"""
|
||||||
with ops.name_scope(name, 'RaggedBatchGatherWithDefault'):
|
with ops.name_scope(name, 'RaggedBatchGatherWithDefault'):
|
||||||
params = ragged_tensor.convert_to_tensor_or_ragged_tensor(
|
params = ragged_tensor.convert_to_tensor_or_ragged_tensor(
|
||||||
|
@ -112,11 +112,11 @@ def unicode_encode(input,
|
|||||||
A `N` dimensional `string` tensor with shape `[D1...DN]`.
|
A `N` dimensional `string` tensor with shape `[D1...DN]`.
|
||||||
|
|
||||||
#### Example:
|
#### Example:
|
||||||
```python
|
|
||||||
>>> input = [[71, 246, 246, 100, 110, 105, 103, 104, 116], [128522]]
|
>>> input = [[71, 246, 246, 100, 110, 105, 103, 104, 116], [128522]]
|
||||||
>>> unicode_encode(input, 'UTF-8')
|
>>> unicode_encode(input, 'UTF-8')
|
||||||
['G\xc3\xb6\xc3\xb6dnight', '\xf0\x9f\x98\x8a']
|
['G\xc3\xb6\xc3\xb6dnight', '\xf0\x9f\x98\x8a']
|
||||||
```
|
|
||||||
"""
|
"""
|
||||||
with ops.name_scope(name, "UnicodeEncode", [input]):
|
with ops.name_scope(name, "UnicodeEncode", [input]):
|
||||||
input_tensor = ragged_tensor.convert_to_tensor_or_ragged_tensor(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.
|
`tf.RaggedTensor`s otherwise.
|
||||||
|
|
||||||
#### Example:
|
#### Example:
|
||||||
```python
|
|
||||||
>>> input = [s.encode('utf8') for s in (u'G\xf6\xf6dnight', u'\U0001f60a')]
|
>>> input = [s.encode('utf8') for s in (u'G\xf6\xf6dnight', u'\U0001f60a')]
|
||||||
>>> result = tf.strings.unicode_decode_with_offsets(input, 'UTF-8')
|
>>> result = tf.strings.unicode_decode_with_offsets(input, 'UTF-8')
|
||||||
>>> result[0].tolist() # codepoints
|
>>> result[0].tolist() # codepoints
|
||||||
[[71, 246, 246, 100, 110, 105, 103, 104, 116], [128522]]
|
[[71, 246, 246, 100, 110, 105, 103, 104, 116], [128522]]
|
||||||
>>> result[1].tolist() # offsets
|
>>> result[1].tolist() # offsets
|
||||||
[[0, 1, 3, 5, 6, 7, 8, 9, 10], [0]]
|
[[0, 1, 3, 5, 6, 7, 8, 9, 10], [0]]
|
||||||
```
|
|
||||||
"""
|
"""
|
||||||
with ops.name_scope(name, "UnicodeDecodeWithOffsets", [input]):
|
with ops.name_scope(name, "UnicodeDecodeWithOffsets", [input]):
|
||||||
return _unicode_decode(input, input_encoding, errors, replacement_char,
|
return _unicode_decode(input, input_encoding, errors, replacement_char,
|
||||||
@ -374,7 +374,7 @@ def unicode_split_with_offsets(input,
|
|||||||
`tf.RaggedTensor`s otherwise.
|
`tf.RaggedTensor`s otherwise.
|
||||||
|
|
||||||
#### Example:
|
#### Example:
|
||||||
```python
|
|
||||||
>>> input = [s.encode('utf8') for s in (u'G\xf6\xf6dnight', u'\U0001f60a')]
|
>>> input = [s.encode('utf8') for s in (u'G\xf6\xf6dnight', u'\U0001f60a')]
|
||||||
>>> result = tf.strings.unicode_split_with_offsets(input, 'UTF-8')
|
>>> result = tf.strings.unicode_split_with_offsets(input, 'UTF-8')
|
||||||
>>> result[0].tolist() # character substrings
|
>>> result[0].tolist() # character substrings
|
||||||
@ -382,7 +382,7 @@ def unicode_split_with_offsets(input,
|
|||||||
['\xf0\x9f\x98\x8a']]
|
['\xf0\x9f\x98\x8a']]
|
||||||
>>> result[1].tolist() # offsets
|
>>> result[1].tolist() # offsets
|
||||||
[[0, 1, 3, 5, 6, 7, 8, 9, 10], [0]]
|
[[0, 1, 3, 5, 6, 7, 8, 9, 10], [0]]
|
||||||
```
|
|
||||||
"""
|
"""
|
||||||
with ops.name_scope(name, "UnicodeSplitWithOffsets", [input]):
|
with ops.name_scope(name, "UnicodeSplitWithOffsets", [input]):
|
||||||
codepoints, offsets = _unicode_decode(input, input_encoding, errors,
|
codepoints, offsets = _unicode_decode(input, input_encoding, errors,
|
||||||
|
@ -104,12 +104,10 @@ class RaggedTensor(composite_tensor.CompositeTensor):
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```python
|
|
||||||
>>> print(tf.RaggedTensor.from_row_splits(
|
>>> print(tf.RaggedTensor.from_row_splits(
|
||||||
... values=[3, 1, 4, 1, 5, 9, 2, 6],
|
... values=[3, 1, 4, 1, 5, 9, 2, 6],
|
||||||
... row_splits=[0, 4, 4, 7, 8, 8]))
|
... row_splits=[0, 4, 4, 7, 8, 8]))
|
||||||
<tf.RaggedTensor [[3, 1, 4, 1], [], [5, 9, 2], [6], []]>
|
<tf.RaggedTensor [[3, 1, 4, 1], [], [5, 9, 2], [6], []]>
|
||||||
```
|
|
||||||
|
|
||||||
### Alternative Row-Partitioning Schemes
|
### Alternative Row-Partitioning Schemes
|
||||||
|
|
||||||
@ -139,7 +137,6 @@ class RaggedTensor(composite_tensor.CompositeTensor):
|
|||||||
Example: The following ragged tensors are equivalent, and all represent the
|
Example: The following ragged tensors are equivalent, and all represent the
|
||||||
nested list `[[3, 1, 4, 1], [], [5, 9, 2], [6], []]`.
|
nested list `[[3, 1, 4, 1], [], [5, 9, 2], [6], []]`.
|
||||||
|
|
||||||
```python
|
|
||||||
>>> values = [3, 1, 4, 1, 5, 9, 2, 6]
|
>>> values = [3, 1, 4, 1, 5, 9, 2, 6]
|
||||||
>>> rt1 = RaggedTensor.from_row_splits(values, row_splits=[0, 4, 4, 7, 8, 8])
|
>>> 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])
|
>>> 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)
|
... 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])
|
>>> 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])
|
>>> rt5 = RaggedTensor.from_row_limits(values, row_limits=[4, 4, 7, 8, 8])
|
||||||
```
|
|
||||||
|
|
||||||
### Multiple Ragged Dimensions
|
### Multiple Ragged Dimensions
|
||||||
|
|
||||||
@ -155,7 +151,6 @@ class RaggedTensor(composite_tensor.CompositeTensor):
|
|||||||
a nested `RaggedTensor` for the `values` tensor. Each nested `RaggedTensor`
|
a nested `RaggedTensor` for the `values` tensor. Each nested `RaggedTensor`
|
||||||
adds a single ragged dimension.
|
adds a single ragged dimension.
|
||||||
|
|
||||||
```python
|
|
||||||
>>> inner_rt = RaggedTensor.from_row_splits( # =rt1 from above
|
>>> 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])
|
... values=[3, 1, 4, 1, 5, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
|
||||||
>>> outer_rt = RaggedTensor.from_row_splits(
|
>>> outer_rt = RaggedTensor.from_row_splits(
|
||||||
@ -164,25 +159,21 @@ class RaggedTensor(composite_tensor.CompositeTensor):
|
|||||||
[[[3, 1, 4, 1], [], [5, 9, 2]], [], [[6], []]]
|
[[[3, 1, 4, 1], [], [5, 9, 2]], [], [[6], []]]
|
||||||
>>> print outer_rt.ragged_rank
|
>>> print outer_rt.ragged_rank
|
||||||
2
|
2
|
||||||
```
|
|
||||||
|
|
||||||
The factory function `RaggedTensor.from_nested_row_splits` may be used to
|
The factory function `RaggedTensor.from_nested_row_splits` may be used to
|
||||||
construct a `RaggedTensor` with multiple ragged dimensions directly, by
|
construct a `RaggedTensor` with multiple ragged dimensions directly, by
|
||||||
providing a list of `row_splits` tensors:
|
providing a list of `row_splits` tensors:
|
||||||
|
|
||||||
```python
|
|
||||||
>>> RaggedTensor.from_nested_row_splits(
|
>>> RaggedTensor.from_nested_row_splits(
|
||||||
... flat_values=[3, 1, 4, 1, 5, 9, 2, 6],
|
... 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()
|
... nested_row_splits=([0, 3, 3, 5], [0, 4, 4, 7, 8, 8])).to_list()
|
||||||
[[[3, 1, 4, 1], [], [5, 9, 2]], [], [[6], []]]
|
[[[3, 1, 4, 1], [], [5, 9, 2]], [], [[6], []]]
|
||||||
```
|
|
||||||
|
|
||||||
### Uniform Inner Dimensions
|
### Uniform Inner Dimensions
|
||||||
|
|
||||||
`RaggedTensor`s with uniform inner dimensions can be defined
|
`RaggedTensor`s with uniform inner dimensions can be defined
|
||||||
by using a multidimensional `Tensor` for `values`.
|
by using a multidimensional `Tensor` for `values`.
|
||||||
|
|
||||||
```python
|
|
||||||
>>> rt = RaggedTensor.from_row_splits(values=tf.ones([5, 3]),
|
>>> rt = RaggedTensor.from_row_splits(values=tf.ones([5, 3]),
|
||||||
.. row_splits=[0, 2, 5])
|
.. row_splits=[0, 2, 5])
|
||||||
>>> print rt.to_list()
|
>>> print rt.to_list()
|
||||||
@ -190,7 +181,6 @@ class RaggedTensor(composite_tensor.CompositeTensor):
|
|||||||
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]]
|
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]]
|
||||||
>>> print rt.shape
|
>>> print rt.shape
|
||||||
(2, ?, 3)
|
(2, ?, 3)
|
||||||
```
|
|
||||||
|
|
||||||
### Uniform Outer Dimensions
|
### Uniform Outer Dimensions
|
||||||
|
|
||||||
@ -200,7 +190,6 @@ class RaggedTensor(composite_tensor.CompositeTensor):
|
|||||||
constructed with this method from a `RaggedTensor` values with shape
|
constructed with this method from a `RaggedTensor` values with shape
|
||||||
`[4, None]`:
|
`[4, None]`:
|
||||||
|
|
||||||
```python
|
|
||||||
>>> values = tf.ragged.constant([[1, 2, 3], [4], [5, 6], [7, 8, 9, 10]])
|
>>> values = tf.ragged.constant([[1, 2, 3], [4], [5, 6], [7, 8, 9, 10]])
|
||||||
>>> print values.shape
|
>>> print values.shape
|
||||||
(4, None)
|
(4, None)
|
||||||
@ -209,17 +198,14 @@ class RaggedTensor(composite_tensor.CompositeTensor):
|
|||||||
<tf.RaggedTensor [[[1, 2, 3], [4]], [[5, 6], [7, 8, 9, 10]]])>
|
<tf.RaggedTensor [[[1, 2, 3], [4]], [[5, 6], [7, 8, 9, 10]]])>
|
||||||
>>> print rt1.shape
|
>>> print rt1.shape
|
||||||
(2, 2, None)
|
(2, 2, None)
|
||||||
```
|
|
||||||
|
|
||||||
Note that `rt1` only contains one ragged dimension (the innermost
|
Note that `rt1` only contains one ragged dimension (the innermost
|
||||||
dimension). In contrast, if `from_row_splits` is used to construct a similar
|
dimension). In contrast, if `from_row_splits` is used to construct a similar
|
||||||
`RaggedTensor`, then that `RaggedTensor` will have two ragged dimensions:
|
`RaggedTensor`, then that `RaggedTensor` will have two ragged dimensions:
|
||||||
|
|
||||||
```python
|
|
||||||
>>> rt2 = tf.RaggedTensor.from_row_splits(values, [0, 2, 4])
|
>>> rt2 = tf.RaggedTensor.from_row_splits(values, [0, 2, 4])
|
||||||
>>> print rt2.shape
|
>>> print rt2.shape
|
||||||
(2, None, None)
|
(2, None, None)
|
||||||
```
|
|
||||||
|
|
||||||
Uniform and ragged outer dimensions may be interleaved, meaning that a
|
Uniform and ragged outer dimensions may be interleaved, meaning that a
|
||||||
tensor with any combination of ragged and uniform dimensions may be created.
|
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]
|
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]
|
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]
|
t4 = RaggedTensor.from_row_lengths(t3, [...]) # [3, None, 4, 8, None, 2]
|
||||||
|
```
|
||||||
"""
|
"""
|
||||||
|
|
||||||
#=============================================================================
|
#=============================================================================
|
||||||
|
Loading…
Reference in New Issue
Block a user