tf.sparse.softmax
Stay organized with collections
Save and categorize content based on your preferences.
Applies softmax to a batched N-D SparseTensor
.
tf.sparse.softmax(
sp_input, name=None
)
The inputs represent an N-D SparseTensor with logical shape [..., B, C]
(where N >= 2
), and with indices sorted in the canonical lexicographic
order.
This op is equivalent to applying the normal tf.nn.softmax()
to each
innermost logical submatrix with shape [B, C]
, but with the catch that the
implicitly zero elements do not participate. Specifically, the algorithm is
equivalent to:
(1) Applies tf.nn.softmax()
to a densified view of each innermost
submatrix with shape [B, C]
, along the size-C dimension;
(2) Masks out the original implicitly-zero locations;
(3) Renormalizes the remaining elements.
Hence, the SparseTensor
result has exactly the same non-zero indices and
shape.
Example:
# First batch:
# [? e.]
# [1. ? ]
# Second batch:
# [e ? ]
# [e e ]
shape = [2, 2, 2] # 3-D SparseTensor
values = np.asarray([[[0., np.e], [1., 0.]], [[np.e, 0.], [np.e, np.e]]])
indices = np.vstack(np.where(values)).astype(np.int64).T
result = tf.sparse.softmax(tf.SparseTensor(indices, values, shape))
# ...returning a 3-D SparseTensor, equivalent to:
# [? 1.] [1 ?]
# [1. ? ] and [.5 .5]
# where ? means implicitly zero.
Args |
sp_input
|
N-D SparseTensor , where N >= 2 .
|
name
|
optional name of the operation.
|
Returns |
output
|
N-D SparseTensor representing the results.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.sparse.softmax\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 2 version](/api_docs/python/tf/sparse/softmax) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/sparse_ops.py#L2408-L2461) |\n\nApplies softmax to a batched N-D `SparseTensor`.\n\n#### View aliases\n\n\n**Main aliases**\n\n\\`tf.sparse_softmax\\`\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.sparse.softmax`](/api_docs/python/tf/sparse/softmax), [`tf.compat.v1.sparse_softmax`](/api_docs/python/tf/sparse/softmax), \\`tf.compat.v2.sparse.softmax\\`\n\n\u003cbr /\u003e\n\n tf.sparse.softmax(\n sp_input, name=None\n )\n\nThe inputs represent an N-D SparseTensor with logical shape `[..., B, C]`\n(where `N \u003e= 2`), and with indices sorted in the canonical lexicographic\norder.\n\nThis op is equivalent to applying the normal [`tf.nn.softmax()`](../../tf/nn/softmax) to each\ninnermost logical submatrix with shape `[B, C]`, but with the catch that *the\nimplicitly zero elements do not participate*. Specifically, the algorithm is\nequivalent to:\n\n(1) Applies [`tf.nn.softmax()`](../../tf/nn/softmax) to a densified view of each innermost\nsubmatrix with shape `[B, C]`, along the size-C dimension;\n(2) Masks out the original implicitly-zero locations;\n(3) Renormalizes the remaining elements.\n\nHence, the `SparseTensor` result has exactly the same non-zero indices and\nshape.\n\n#### Example:\n\n # First batch:\n # [? e.]\n # [1. ? ]\n # Second batch:\n # [e ? ]\n # [e e ]\n shape = [2, 2, 2] # 3-D SparseTensor\n values = np.asarray([[[0., np.e], [1., 0.]], [[np.e, 0.], [np.e, np.e]]])\n indices = np.vstack(np.where(values)).astype(np.int64).T\n\n result = tf.sparse.softmax(tf.SparseTensor(indices, values, shape))\n # ...returning a 3-D SparseTensor, equivalent to:\n # [? 1.] [1 ?]\n # [1. ? ] and [.5 .5]\n # where ? means implicitly zero.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------|-------------------------------------|\n| `sp_input` | N-D `SparseTensor`, where `N \u003e= 2`. |\n| `name` | optional name of the operation. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|----------|----------------------------------------------|\n| `output` | N-D `SparseTensor` representing the results. |\n\n\u003cbr /\u003e"]]