Add code-fences to doctest blocks.

Most >>> blocks already have ``` fences. Doctest runs them with or without the fences.
This change adds the ``` anywhere they're missing when api docs are generated from the docstrings. This will ensure that they look right when viewed as markdown.

+ fix docstring for `constant_initializer`: you can't have blank lines inside a doctest block. This prevents the rendering from getting corrupted.

PiperOrigin-RevId: 267009925
This commit is contained in:
Mark Daoust 2019-09-03 14:12:54 -07:00 committed by TensorFlower Gardener
parent fe6529d095
commit 76ca9f1060
2 changed files with 60 additions and 77 deletions

View File

@ -173,50 +173,42 @@ class Constant(Initializer):
of the `value` list, even reshaped, as shown in the two commented lines
below the `value` list initialization.
```python
>>> import numpy as np
>>> import tensorflow as tf
>>> 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)
>>> print('fitting shape:')
>>> with tf.compat.v1.Session():
>>> x = tf.compat.v1.get_variable('x', shape=[2, 4], initializer=init)
>>> x.initializer.run()
>>> print(x.eval())
fitting shape:
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]]
>>> print('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())
larger shape:
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 7. 7. 7. 7.]]
>>> print('smaller shape:')
>>> with tf.compat.v1.Session():
>>> x = tf.compat.v1.get_variable('x', shape=[2, 3], initializer=init)
ValueError: Too many elements provided. Needed at most 6, but received 8
>>> print('shape verification:')
>>> 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)
TypeError: Expected Tensor's shape: (3, 4), got (8,).
```
>>> 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)
... x.initializer.run()
... 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.]]
>>>
>>> # Smaller shape
>>> with tf.compat.v1.Session():
... x = tf.compat.v1.get_variable('x', shape=[2, 3], initializer=init)
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)
>>> with tf.compat.v1.Session():
... x = tf.compat.v1.get_variable('x', shape=[3, 4],
... initializer=init_verify)
TypeError: Expected Tensor's shape: (3, 4), got (8,).
>>>
```
"""

View File

@ -150,40 +150,31 @@ class Constant(Initializer):
below the `value` list initialization.
```python
>>> import numpy as np
>>> import tensorflow as tf
>>> 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)
... x.initializer.run()
... 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.]]
>>> # Smaller shape
>>> with tf.compat.v1.Session():
... x = tf.compat.v1.get_variable('x', shape=[2, 3], initializer=init)
ValueError: Too many elements provided. Needed at most 6, but received 8
>>> 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)
>>> print('fitting shape:')
>>> with tf.compat.v1.Session():
>>> x = tf.compat.v1.get_variable('x', shape=[2, 4], initializer=init)
>>> x.initializer.run()
>>> print(x.eval())
fitting shape:
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]]
>>> print('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())
larger shape:
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 7. 7. 7. 7.]]
>>> print('smaller shape:')
>>> with tf.compat.v1.Session():
>>> x = tf.compat.v1.get_variable('x', shape=[2, 3], initializer=init)
ValueError: Too many elements provided. Needed at most 6, but received 8
```
"""