Skip to content

Commit 1788549

Browse files
zsdonghaoluomai
authored andcommitted
Improve docs (#331)
* check layers docs * check cost docs * check prepro * fixed DeformableConv2d TODO * check iterate * check utils * check nlp * check rein * check files * check visulize * check activation * yapf * fix utils docs * add ops back * add supress stdout back for compatibility. * add an alias for ops.supress_output * fix indentation * fix all docs warnings. * fix luomai comment * nlp * fix nlp typos.
1 parent 6784356 commit 1788549

File tree

23 files changed

+847
-503
lines changed

23 files changed

+847
-503
lines changed

docs/modules/cost.rst

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ then you can apply L2 regularization on the weights matrix of first two layer as
3030
.. code-block:: python
3131
3232
cost = tl.cost.cross_entropy(y, y_)
33-
cost = cost + tf.contrib.layers.l2_regularizer(0.001)(network.all_params[0]) + tf.contrib.layers.l2_regularizer(0.001)(network.all_params[2])
33+
cost = cost + tf.contrib.layers.l2_regularizer(0.001)(network.all_params[0])
34+
+ tf.contrib.layers.l2_regularizer(0.001)(network.all_params[2])
3435
3536
Besides, TensorLayer provides a easy way to get all variables by a given name, so you can also
3637
apply L2 regularization on some weights as follow.
@@ -44,6 +45,7 @@ apply L2 regularization on some weights as follow.
4445
4546
4647
48+
4749
Regularization of Weights
4850
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4951

@@ -75,12 +77,11 @@ Then max-norm regularization on W1 and W2 can be performed as follow.
7577

7678
.. code-block:: python
7779
78-
y = network.outputs
79-
# Alternatively, you can use tl.cost.cross_entropy(y, y_) instead.
80-
cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_))
81-
cost = cross_entropy
82-
cost = cost + tl.cost.maxnorm_regularizer(1.0)(network.all_params[0]) +
83-
tl.cost.maxnorm_regularizer(1.0)(network.all_params[2])
80+
max_norm = 0
81+
for w in tl.layers.get_variables_with_name('W', train_only=True, printable=False):
82+
max_norm += tl.cost.maxnorm_regularizer(1)(w)
83+
cost = tl.cost.cross_entropy(y, y_) + max_norm
84+
8485
8586
In addition, all TensorFlow's regularizers like
8687
``tf.contrib.layers.l2_regularizer`` can be used with TensorLayer.

docs/modules/prepro.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Images
108108

109109
- These functions only apply on a single image, use ``threading_data`` to apply multiple threading see ``tutorial_image_preprocess.py``.
110110
- All functions have argument ``is_random``.
111-
- All functions end with `multi` , usually be used for image segmentation i.e. the input and output image should be matched.
111+
- All functions end with ``*_multi`` process all images together, usually be used for image segmentation i.e. the input and output image should be matched.
112112

113113
Rotation
114114
^^^^^^^^^

docs/modules/utils.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ API - Utility
1414
dict_to_one
1515
list_string_to_dict
1616
flatten_list
17+
exit_tensorflow
18+
open_tensorboard
19+
clear_all_placeholder_variables
20+
set_gpu_fraction
1721

1822
Training, testing and predicting
1923
----------------------------------
@@ -58,17 +62,17 @@ Flatten a list
5862
.. autofunction:: flatten_list
5963

6064
Close TF session and associated processes
61-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
65+
-----------------------------------------
6266
.. autofunction:: exit_tensorflow
6367

6468
Open TensorBoard
65-
^^^^^^^^^^^^^^^^^^^
69+
----------------
6670
.. autofunction:: open_tensorboard
6771

6872
Clear TensorFlow placeholder
69-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
73+
----------------------------
7074
.. autofunction:: clear_all_placeholder_variables
7175

7276
Set GPU functions
73-
---------------------------
74-
.. autofunction:: set_gpu_fraction
77+
-----------------
78+
.. autofunction:: set_gpu_fraction

tensorlayer/activation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ def pixel_wise_softmax(x, name='pixel_wise_softmax'):
112112
----------
113113
x : Tensor
114114
input.
115-
- For 2d image, 4D tensor (batch_size, height, weight, channel), where channel >= 2.
116-
- For 3d image, 5D tensor (batch_size, depth, height, weight, channel), where channel >= 2.
115+
- For 2d image, 4D tensor (batch_size, height, weight, channel), where channel >= 2.
116+
- For 3d image, 5D tensor (batch_size, depth, height, weight, channel), where channel >= 2.
117117
name : str
118118
function name (optional)
119119

tensorlayer/cost.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77

88
def cross_entropy(output, target, name=None):
9-
"""It is a softmax cross-entropy operation, returns the TensorFlow expression of cross-entropy of two distributions, implement
9+
"""Softmax cross-entropy operation, returns the TensorFlow expression of cross-entropy for two distributions, it implements
1010
softmax internally. See ``tf.nn.sparse_softmax_cross_entropy_with_logits``.
1111
1212
Parameters
1313
----------
14-
output : Tensorflow variable
14+
output : Tensor
1515
A batch of distribution with shape: [batch_size, num of classes].
16-
target : Tensorflow variable
16+
target : Tensor
1717
A batch of index with shape: [batch_size, ].
1818
name : string
1919
Name of this loss.
@@ -36,7 +36,7 @@ def cross_entropy(output, target, name=None):
3636

3737

3838
def sigmoid_cross_entropy(output, target, name=None):
39-
"""It is a sigmoid cross-entropy operation, see ``tf.nn.sigmoid_cross_entropy_with_logits``.
39+
"""Sigmoid cross-entropy operation, see ``tf.nn.sigmoid_cross_entropy_with_logits``.
4040
4141
Parameters
4242
----------
@@ -55,11 +55,7 @@ def sigmoid_cross_entropy(output, target, name=None):
5555

5656

5757
def binary_cross_entropy(output, target, epsilon=1e-8, name='bce_loss'):
58-
"""It is a binary cross entropy operation.
59-
60-
# For brevity, let `x = output`, `z = target`. The binary cross entropy loss is
61-
#
62-
# loss(x, z) = - sum_i (x[i] * log(z[i]) + (1 - x[i]) * log(1 - z[i]))
58+
"""Binary cross entropy operation.
6359
6460
Parameters
6561
----------
@@ -74,7 +70,7 @@ def binary_cross_entropy(output, target, epsilon=1e-8, name='bce_loss'):
7470
7571
References
7672
-----------
77-
- `DRAW <https://github.com/ericjang/draw/blob/master/draw.py#L73>`__
73+
- `ericjang-DRAW <https://github.com/ericjang/draw/blob/master/draw.py#L73>`__
7874
7975
"""
8076
# from tensorflow.python.framework import ops
@@ -84,6 +80,10 @@ def binary_cross_entropy(output, target, epsilon=1e-8, name='bce_loss'):
8480
with tf.name_scope(name):
8581
return tf.reduce_mean(tf.reduce_sum(-(target * tf.log(output + epsilon) + (1. - target) * tf.log(1. - output + epsilon)), axis=1))
8682

83+
# For brevity, let `x = output`, `z = target`. The binary cross entropy loss is
84+
#
85+
# loss(x, z) = - sum_i (x[i] * log(z[i]) + (1 - x[i]) * log(1 - z[i]))
86+
8787

8888
def mean_squared_error(output, target, is_mean=False, name="mean_squared_error"):
8989
"""Return the TensorFlow expression of mean-square-error (L2) of two batch of data.
@@ -96,8 +96,8 @@ def mean_squared_error(output, target, is_mean=False, name="mean_squared_error")
9696
The target distribution, format the same with `output`.
9797
is_mean : boolean
9898
Whether compute the mean or sum for each example.
99-
- If True, use ``tf.reduce_mean`` to compute the loss between one target and predict data.
100-
- If False, use ``tf.reduce_sum`` (default).
99+
- If True, use ``tf.reduce_mean`` to compute the loss between one target and predict data.
100+
- If False, use ``tf.reduce_sum`` (default).
101101
102102
References
103103
------------
@@ -161,8 +161,8 @@ def absolute_difference_error(output, target, is_mean=False):
161161
The target distribution, format the same with `output`.
162162
is_mean : boolean
163163
Whether compute the mean or sum for each example.
164-
- If True, use ``tf.reduce_mean`` to compute the loss between one target and predict data.
165-
- If False, use ``tf.reduce_sum`` (default).
164+
- If True, use ``tf.reduce_mean`` to compute the loss between one target and predict data.
165+
- If False, use ``tf.reduce_sum`` (default).
166166
167167
"""
168168
with tf.name_scope("mean_squared_error_loss"):
@@ -203,10 +203,8 @@ def dice_coe(output, target, loss_type='jaccard', axis=[1, 2, 3], smooth=1e-5):
203203
All dimensions are reduced, default ``[1,2,3]``.
204204
smooth : float
205205
This small value will be added to the numerator and denominator.
206-
If both output and target are empty, it makes sure dice is 1.
207-
If either output or target are empty (all pixels are background), dice = ```smooth/(small_value + smooth)``,
208-
then if smooth is very small, dice close to 0 (even the image values lower than the threshold),
209-
so in this case, higher smooth can have a higher dice.
206+
- If both output and target are empty, it makes sure dice is 1.
207+
- If either output or target are empty (all pixels are background), dice = ```smooth/(small_value + smooth)``, then if smooth is very small, dice close to 0 (even the image values lower than the threshold), so in this case, higher smooth can have a higher dice.
210208
211209
Examples
212210
---------
@@ -359,8 +357,8 @@ def cross_entropy_seq(logits, target_seqs, batch_size=None): #, batch_size=1, n
359357
The target sequence, 2D tensor `[batch_size, n_steps]`, if the number of step is dynamic, please use ``tl.cost.cross_entropy_seq_with_mask`` instead.
360358
batch_size : None or int.
361359
Whether to divide the cost by batch size.
362-
- If integer, the return cost will be divided by `batch_size`.
363-
- If None (default), the return cost will not be divided by anything.
360+
- If integer, the return cost will be divided by `batch_size`.
361+
- If None (default), the return cost will not be divided by anything.
364362
365363
Examples
366364
--------
@@ -401,8 +399,8 @@ def cross_entropy_seq_with_mask(logits, target_seqs, input_mask, return_details=
401399
The mask to compute loss, it has the same size with `target_seqs`, normally 0 or 1.
402400
return_details : boolean
403401
Whether to return detailed losses.
404-
- If False (default), only returns the loss.
405-
- If True, returns the loss, losses, weights and targets (see source code).
402+
- If False (default), only returns the loss.
403+
- If True, returns the loss, losses, weights and targets (see source code).
406404
407405
Examples
408406
--------
@@ -581,7 +579,7 @@ def lo(weights, name='lo_regularizer'):
581579
def maxnorm_regularizer(scale=1.0, scope=None):
582580
"""Max-norm regularization returns a function that can be used to apply max-norm regularization to weights.
583581
584-
More about max-norm, see `<https://en.wikipedia.org/wiki/Matrix_norm#Max_norm>`__.
582+
More about max-norm, see `wiki-max norm <https://en.wikipedia.org/wiki/Matrix_norm#Max_norm>`_.
585583
The implementation follows `TensorFlow contrib <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/layers/python/layers/regularizers.py>`__.
586584
587585
Parameters

tensorlayer/files.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def load_matt_mahoney_text8_dataset(path='data'):
316316
317317
Returns
318318
--------
319-
word_list : list of str
319+
list of str
320320
The raw text data e.g. [.... 'their', 'families', 'who', 'were', 'expelled', 'from', 'jerusalem', ...]
321321
322322
Examples
@@ -439,13 +439,16 @@ def load_imdb_dataset(path='data', nb_words=None, skip_top=0, maxlen=None, test_
439439
def load_nietzsche_dataset(path='data'):
440440
"""Load Nietzsche dataset.
441441
442-
Returns a string.
443-
444442
Parameters
445443
----------
446444
path : str
447445
The path that the data is downloaded to, defaults is ``data/nietzsche/``.
448446
447+
Returns
448+
--------
449+
str
450+
The content.
451+
449452
Examples
450453
--------
451454
>>> see tutorial_generate_text.py
@@ -544,8 +547,8 @@ def load_flickr25k_dataset(tag='sky', path="data", n_threads=50, printable=False
544547
------------
545548
tag : str or None
546549
What images to return.
547-
- If you want to get images with tag, use string like 'dog', 'red', see `Flickr Search <https://www.flickr.com/search/>`__.
548-
- If you want to get all images, set to ``None``.
550+
- If you want to get images with tag, use string like 'dog', 'red', see `Flickr Search <https://www.flickr.com/search/>`__.
551+
- If you want to get all images, set to ``None``.
549552
550553
path : str
551554
The path that the data is downloaded to, defaults is ``data/flickr25k/``.
@@ -612,8 +615,8 @@ def load_flickr1M_dataset(tag='sky', size=10, path="data", n_threads=50, printab
612615
------------
613616
tag : str or None
614617
What images to return.
615-
- If you want to get images with tag, use string like 'dog', 'red', see `Flickr Search <https://www.flickr.com/search/>`__.
616-
- If you want to get all images, set to ``None``.
618+
- If you want to get images with tag, use string like 'dog', 'red', see `Flickr Search <https://www.flickr.com/search/>`__.
619+
- If you want to get all images, set to ``None``.
617620
618621
size : int
619622
integer between 1 to 10. 1 means 100k images ... 5 means 500k images, 10 means all 1 million images. Default is 10.
@@ -1202,7 +1205,7 @@ def load_npz(path='', name='model.npz'):
12021205
12031206
Returns
12041207
--------
1205-
params : list of array
1208+
list of array
12061209
A list of parameters in order.
12071210
12081211
Examples
@@ -1247,7 +1250,7 @@ def assign_params(sess, params, network):
12471250
12481251
Returns
12491252
--------
1250-
ops : list of operations
1253+
list of operations
12511254
A list of tf ops in order that assign params. Support sess.run(ops) manually.
12521255
12531256
Examples
@@ -1281,7 +1284,8 @@ def load_and_assign_npz(sess=None, name=None, network=None):
12811284
12821285
Returns
12831286
--------
1284-
Returns False if faild to model is not exist.
1287+
False or network
1288+
Returns False, if the model is not exist.
12851289
12861290
Examples
12871291
--------
@@ -1646,7 +1650,8 @@ def exists_or_mkdir(path, verbose=True):
16461650
16471651
Returns
16481652
--------
1649-
True if folder exist, otherwise, returns False and create the folder
1653+
boolean
1654+
True if folder already exist, otherwise, returns False and create the folder.
16501655
16511656
Examples
16521657
--------
@@ -1683,7 +1688,8 @@ def maybe_download_and_extract(filename, working_directory, url_source, extract=
16831688
16841689
Returns
16851690
----------
1686-
Filepath to dowloaded (uncompressed) file
1691+
str
1692+
File path of the dowloaded (uncompressed) file.
16871693
16881694
Examples
16891695
--------

0 commit comments

Comments
 (0)