diff --git a/tensorflow_addons/conftest.py b/tensorflow_addons/conftest.py index 80e8aec9ac..8aa24adfdf 100644 --- a/tensorflow_addons/conftest.py +++ b/tensorflow_addons/conftest.py @@ -14,20 +14,6 @@ pytest_collection_modifyitems, ) -import numpy as np -import pytest - -import tensorflow as tf -import tensorflow_addons as tfa - - # fixtures present in this file will be available # when running tests and can be referenced with strings # https://docs.pytest.org/en/latest/fixture.html#conftest-py-sharing-fixture-functions - - -@pytest.fixture(autouse=True) -def add_np(doctest_namespace): - doctest_namespace["np"] = np - doctest_namespace["tf"] = tf - doctest_namespace["tfa"] = tfa diff --git a/tensorflow_addons/layers/wrappers.py b/tensorflow_addons/layers/wrappers.py index b950a78bc4..811d320f7c 100644 --- a/tensorflow_addons/layers/wrappers.py +++ b/tensorflow_addons/layers/wrappers.py @@ -16,7 +16,6 @@ import logging import tensorflow as tf - from typeguard import typechecked @@ -31,25 +30,21 @@ class WeightNormalization(tf.keras.layers.Wrapper): Training of Deep Neural Networks: https://arxiv.org/abs/1602.07868 Tim Salimans, Diederik P. Kingma (2016) WeightNormalization wrapper works for keras and tf layers. - - Usage: - - >>> net = tfa.layers.WeightNormalization( - ... tf.keras.layers.Conv2D(2, 2, activation='relu'), - ... input_shape=(32, 32, 3), - ... data_init=True)(np.random.rand(32, 32, 3, 1).astype('f')) - >>> net = tfa.layers.WeightNormalization( - ... tf.keras.layers.Conv2D(16, 2, activation='relu'), - ... data_init=True)(net) - >>> net = tfa.layers.WeightNormalization( - ... tf.keras.layers.Dense(120, activation='relu'), - ... data_init=True)(net) - >>> net = tfa.layers.WeightNormalization( - ... tf.keras.layers.Dense(2), - ... data_init=True)(net) - >>> net.shape - TensorShape([32, 30, 1, 2]) - + ```python + net = WeightNormalization( + tf.keras.layers.Conv2D(2, 2, activation='relu'), + input_shape=(32, 32, 3), + data_init=True)(x) + net = WeightNormalization( + tf.keras.layers.Conv2D(16, 5, activation='relu'), + data_init=True)(net) + net = WeightNormalization( + tf.keras.layers.Dense(120, activation='relu'), + data_init=True)(net) + net = WeightNormalization( + tf.keras.layers.Dense(n_classes), + data_init=True)(net) + ``` Arguments: layer: a layer instance. data_init: If `True` use data dependent variable initialization diff --git a/tensorflow_addons/losses/focal_loss.py b/tensorflow_addons/losses/focal_loss.py index dda4587a29..47c785a465 100644 --- a/tensorflow_addons/losses/focal_loss.py +++ b/tensorflow_addons/losses/focal_loss.py @@ -15,7 +15,6 @@ """Implements Focal loss.""" import tensorflow as tf - import tensorflow.keras.backend as K from tensorflow_addons.utils.keras_utils import LossFunctionWrapper @@ -38,11 +37,15 @@ class SigmoidFocalCrossEntropy(LossFunctionWrapper): Usage: - >>> fl = tfa.losses.SigmoidFocalCrossEntropy() - >>> loss = fl([[0.97], [0.91], [0.03]], [[1.0], [1.0], [0.0]]) - >>> loss - - + ```python + fl = tfa.losses.SigmoidFocalCrossEntropy() + loss = fl( + y_true = [[1.0], [1.0], [0.0]], + y_pred = [[0.97], [0.91], [0.03]]) + print('Loss: ', loss.numpy()) # Loss: [6.8532745e-06, + 1.9097870e-04, + 2.0559824e-05] + ``` Usage with tf.keras API: ```python diff --git a/tensorflow_addons/losses/giou_loss.py b/tensorflow_addons/losses/giou_loss.py index 8948381353..685d71adf7 100644 --- a/tensorflow_addons/losses/giou_loss.py +++ b/tensorflow_addons/losses/giou_loss.py @@ -33,13 +33,13 @@ class GIoULoss(LossFunctionWrapper): Usage: - >>> gl = tfa.losses.GIoULoss() - >>> boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - >>> boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]]) - >>> loss = gl(boxes1, boxes2) - >>> loss - - + ```python + gl = tfa.losses.GIoULoss() + boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) + boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]]) + loss = gl(boxes1, boxes2) + print('Loss: ', loss.numpy()) # Loss: [1.07500000298023224, 1.9333333373069763] + ``` Usage with tf.keras API: ```python diff --git a/tensorflow_addons/losses/quantiles.py b/tensorflow_addons/losses/quantiles.py index ef3aca0a39..e758f62611 100644 --- a/tensorflow_addons/losses/quantiles.py +++ b/tensorflow_addons/losses/quantiles.py @@ -84,11 +84,15 @@ class PinballLoss(LossFunctionWrapper): See: https://en.wikipedia.org/wiki/Quantile_regression Usage: + ```python + pinball = tfa.losses.PinballLoss(tau=.1) + loss = pinball([0., 0., 1., 1.], [1., 1., 1., 0.]) + + # loss = max(0.1 * (y_true - y_pred), (0.1 - 1) * (y_true - y_pred)) + # = (0.9 + 0.9 + 0 + 0.1) / 4 - >>> pinball = tfa.losses.PinballLoss(tau=.1) - >>> loss = pinball([0., 0., 1., 1.], [1., 1., 1., 0.]) - >>> loss - + print('Loss: ', loss.numpy()) # Loss: 0.475 + ``` Usage with the `compile` API: diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 504b42a24b..cb473530b7 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -15,7 +15,6 @@ """Implements Cohen's Kappa.""" import tensorflow as tf - import numpy as np import tensorflow.keras.backend as K from tensorflow.keras.metrics import Metric @@ -28,43 +27,39 @@ @tf.keras.utils.register_keras_serializable(package="Addons") class CohenKappa(Metric): """Computes Kappa score between two raters. + The score lies in the range [-1, 1]. A score of -1 represents complete disagreement between two raters whereas a score of 1 represents complete agreement between the two raters. A score of 0 means agreement by chance. + Note: As of now, this implementation considers all labels while calculating the Cohen's Kappa score. Usage: - >>> actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32) - >>> preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) - >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) - >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds) - - >>> m.result().numpy() - 0.61904764 - >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds, sample_weight=weights) - - >>> m.result().numpy() - 0.37209308 + ```python + actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32) + preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) + weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) + + m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + m.update_state(actuals, preds) + print('Final result: ', m.result().numpy()) # Result: 0.61904764 + + # To use this with weights, sample_weight argument can be used. + m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + m.update_state(actuals, preds, sample_weight=weights) + print('Final result: ', m.result().numpy()) # Result: 0.37209308 + ``` Usage with tf.keras API: + + ```python model = tf.keras.models.Model(inputs, outputs) model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) model.compile('sgd', loss='mse') + ``` """ @typechecked diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index d54c2350aa..e6afa53f61 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -42,13 +42,18 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): ((TP + FP) * (TP + FN) * (TN + FP ) * (TN + FN))^(1/2) Usage: - - >>> actuals = tf.constant([[1.0], [1.0], [1.0], [0.0]], dtype=tf.float32) - >>> preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], dtype=tf.float32) - >>> mcc = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) - >>> mcc.update_state(actuals, preds) - >>> mcc.result() - + ```python + actuals = tf.constant([[1.0], [1.0], [1.0], [0.0]], + dtype=tf.float32) + preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], + dtype=tf.float32) + # Matthews correlation coefficient + mcc = MatthewsCorrelationCoefficient(num_classes=1) + mcc.update_state(actuals, preds) + print('Matthews correlation coefficient is:', + mcc.result().numpy()) + # Matthews correlation coefficient is : -0.33333334 + ``` """ @typechecked diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 291e6a091a..d13a355b33 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -16,11 +16,10 @@ import warnings -import numpy as np import tensorflow as tf - from tensorflow.keras import backend as K from tensorflow.keras.metrics import Metric +import numpy as np from typeguard import typechecked from tensorflow_addons.utils.types import AcceptableDTypes, FloatTensorLike @@ -47,36 +46,30 @@ class MultiLabelConfusionMatrix(Metric): - false negatives for class i in M(1,0) - true positives for class i in M(1,1) - Usage: - - >>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]], dtype=tf.int32) - >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], dtype=tf.int32) - >>> output1 = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> output1.update_state(y_true, y_pred) - >>> output1.result() - - [[1., 0.], - [0., 1.]], - - [[0., 1.], - [1., 0.]]], dtype=float32)> - >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], dtype=tf.int32) - >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], dtype=tf.int32) - >>> output2 = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> output2.update_state(y_true, y_pred) - >>> output2.result() - - [[1., 0.], - [1., 0.]], - - [[1., 1.], - [0., 0.]]], dtype=float32)> + ```python + # multilabel confusion matrix + y_true = tf.constant([[1, 0, 1], [0, 1, 0]], + dtype=tf.int32) + y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], + dtype=tf.int32) + output = MultiLabelConfusionMatrix(num_classes=3) + output.update_state(y_true, y_pred) + print('Confusion matrix:', output.result().numpy()) + + # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] + [[0 1] [1 0]]] + + # if multiclass input is provided + y_true = tf.constant([[1, 0, 0], [0, 1, 0]], + dtype=tf.int32) + y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], + dtype=tf.int32) + output = MultiLabelConfusionMatrix(num_classes=3) + output.update_state(y_true, y_pred) + print('Confusion matrix:', output.result().numpy()) + + # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] + ``` """ @typechecked diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 8e660edcec..8ae78fc543 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -16,7 +16,6 @@ from typing import Tuple import tensorflow as tf - from tensorflow.keras import backend as K from tensorflow.keras.metrics import Metric from tensorflow.python.ops import weights_broadcast_ops @@ -62,13 +61,13 @@ class RSquare(Metric): of the same metric. Usage: - - >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) - >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> ans = tfa.metrics.RSquare() - >>> ans.update_state(actuals, preds) - >>> ans.result() - + ```python + actuals = tf.constant([1, 4, 3], dtype=tf.float32) + preds = tf.constant([2, 4, 4], dtype=tf.float32) + result = tf.keras.metrics.RSquare() + result.update_state(actuals, preds) + print('R^2 score is: ', r1.result().numpy()) # 0.57142866 + ``` """ @typechecked diff --git a/tensorflow_addons/optimizers/lookahead.py b/tensorflow_addons/optimizers/lookahead.py index fe043d7aaa..8f96dc9c62 100644 --- a/tensorflow_addons/optimizers/lookahead.py +++ b/tensorflow_addons/optimizers/lookahead.py @@ -34,8 +34,10 @@ class Lookahead(tf.keras.optimizers.Optimizer): Example of usage: - >>> opt = tf.keras.optimizers.SGD(learning_rate=0.01) - >>> opt = tfa.optimizers.Lookahead(opt) + ```python + opt = tf.keras.optimizers.SGD(learning_rate) + opt = tfa.optimizers.Lookahead(opt) + ``` """ @typechecked diff --git a/tensorflow_addons/optimizers/moving_average.py b/tensorflow_addons/optimizers/moving_average.py index cc8df1b6c5..7e92186ad9 100644 --- a/tensorflow_addons/optimizers/moving_average.py +++ b/tensorflow_addons/optimizers/moving_average.py @@ -34,9 +34,11 @@ class MovingAverage(AveragedOptimizerWrapper): Example of usage: - >>> opt = tf.keras.optimizers.SGD(learning_rate=0.01) - >>> opt = tfa.optimizers.MovingAverage(opt) + ```python + opt = tf.keras.optimizers.SGD(learning_rate) + opt = tfa.optimizers.MovingAverage(opt) + ``` """ @typechecked diff --git a/tensorflow_addons/optimizers/weight_decay_optimizers.py b/tensorflow_addons/optimizers/weight_decay_optimizers.py index 614a78df2b..a30dfa67b8 100644 --- a/tensorflow_addons/optimizers/weight_decay_optimizers.py +++ b/tensorflow_addons/optimizers/weight_decay_optimizers.py @@ -15,7 +15,6 @@ """Base class to make optimizers weight decay ready.""" import tensorflow as tf - from tensorflow_addons.utils.types import FloatTensorLike from typeguard import typechecked @@ -57,14 +56,18 @@ def __init__(self, weight_decay, *args, **kwargs): Note: when applying a decay to the learning rate, be sure to manually apply the decay to the `weight_decay` as well. For example: - Usage: + ```python + step = tf.Variable(0, trainable=False) + schedule = tf.optimizers.schedules.PiecewiseConstantDecay( + [10000, 15000], [1e-0, 1e-1, 1e-2]) + # lr and wd can be a function or a tensor + lr = 1e-1 * schedule(step) + wd = lambda: 1e-4 * schedule(step) + + # ... - >>> step = tf.Variable(0, trainable=False) - >>> schedule = tf.optimizers.schedules.PiecewiseConstantDecay( - ... [10000, 15000], [1e-0, 1e-1, 1e-2]) - >>> lr = 1e-1 * schedule(step) - >>> wd = lambda: 1e-4 * schedule(step) - >>> optimizer = tfa.optimizers.AdamW(learning_rate=lr, weight_decay=wd) + optimizer = tfa.optimizers.AdamW(learning_rate=lr, weight_decay=wd) + ``` """ @typechecked @@ -255,15 +258,18 @@ def extend_with_decoupled_weight_decay( Note: when applying a decay to the learning rate, be sure to manually apply the decay to the `weight_decay` as well. For example: - Usage: + ```python + step = tf.Variable(0, trainable=False) + schedule = tf.optimizers.schedules.PiecewiseConstantDecay( + [10000, 15000], [1e-0, 1e-1, 1e-2]) + # lr and wd can be a function or a tensor + lr = 1e-1 * schedule(step) + wd = lambda: 1e-4 * schedule(step) - >>> step = tf.Variable(0, trainable=False) - >>> schedule = tf.optimizers.schedules.PiecewiseConstantDecay( - ... [10000, 15000], [1e-0, 1e-1, 1e-2]) - >>> lr = 1e-1 * schedule(step) - >>> wd = lambda: 1e-4 * schedule(step) - >>> optimizer = tfa.optimizers.AdamW(learning_rate=lr, weight_decay=wd) + # ... + optimizer = tfa.optimizers.AdamW(learning_rate=lr, weight_decay=wd) + ``` Note: you might want to register your own custom optimizer using `tf.keras.utils.get_custom_objects()`. @@ -329,15 +335,19 @@ class SGDW(DecoupledWeightDecayExtension, tf.keras.optimizers.SGD): Note: when applying a decay to the learning rate, be sure to manually apply the decay to the `weight_decay` as well. For example: - Usage: + ```python + step = tf.Variable(0, trainable=False) + schedule = tf.optimizers.schedules.PiecewiseConstantDecay( + [10000, 15000], [1e-0, 1e-1, 1e-2]) + # lr and wd can be a function or a tensor + lr = 1e-1 * schedule(step) + wd = lambda: 1e-4 * schedule(step) + + # ... - >>> step = tf.Variable(0, trainable=False) - >>> schedule = tf.optimizers.schedules.PiecewiseConstantDecay( - ... [10000, 15000], [1e-0, 1e-1, 1e-2]) - >>> lr = 1e-1 * schedule(step) - >>> wd = lambda: 1e-4 * schedule(step) - >>> optimizer = tfa.optimizers.SGDW( - ... learning_rate=lr, weight_decay=wd, momentum=0.9) + optimizer = tfa.optimizers.SGDW( + learning_rate=lr, weight_decay=wd, momentum=0.9) + ``` """ @typechecked @@ -404,14 +414,18 @@ class AdamW(DecoupledWeightDecayExtension, tf.keras.optimizers.Adam): Note: when applying a decay to the learning rate, be sure to manually apply the decay to the `weight_decay` as well. For example: - Usage: + ```python + step = tf.Variable(0, trainable=False) + schedule = tf.optimizers.schedules.PiecewiseConstantDecay( + [10000, 15000], [1e-0, 1e-1, 1e-2]) + # lr and wd can be a function or a tensor + lr = 1e-1 * schedule(step) + wd = lambda: 1e-4 * schedule(step) + + # ... - >>> step = tf.Variable(0, trainable=False) - >>> schedule = tf.optimizers.schedules.PiecewiseConstantDecay( - ... [10000, 15000], [1e-0, 1e-1, 1e-2]) - >>> lr = 1e-1 * schedule(step) - >>> wd = lambda: 1e-4 * schedule(step) - >>> optimizer = tfa.optimizers.AdamW(learning_rate=lr, weight_decay=wd) + optimizer = tfa.optimizers.AdamW(learning_rate=lr, weight_decay=wd) + ``` """ @typechecked diff --git a/tools/docker/sanity_check.Dockerfile b/tools/docker/sanity_check.Dockerfile index 4b2a4127bf..6ccaf1eec7 100644 --- a/tools/docker/sanity_check.Dockerfile +++ b/tools/docker/sanity_check.Dockerfile @@ -118,8 +118,7 @@ RUN python configure.py RUN --mount=type=cache,id=cache_bazel,target=/root/.cache/bazel \ bash tools/install_so_files.sh RUN pip install --no-deps -e . -RUN pytest -v -n auto ./tensorflow_addons/activations \ - --doctest-modules tensorflow_addons/ --ignore-glob=*_test.py +RUN pytest -v -n auto ./tensorflow_addons/activations RUN touch /ok.txt # -------------------------------