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

View File

@ -150,40 +150,31 @@ class Constant(Initializer):
below the `value` list initialization. below the `value` list initialization.
```python ```python
>>> import numpy as np >>> value = [0, 1, 2, 3, 4, 5, 6, 7]
>>> import tensorflow as tf >>> # 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
``` ```
""" """