tf.contrib.nn.sampled_sparse_softmax_loss
Stay organized with collections
Save and categorize content based on your preferences.
Computes and returns the sampled sparse softmax training loss.
tf.contrib.nn.sampled_sparse_softmax_loss(
weights, biases, labels, inputs, num_sampled, num_classes, sampled_values=None,
remove_accidental_hits=True, partition_strategy='mod',
name='sampled_sparse_softmax_loss'
)
This is a faster way to train a softmax classifier over a huge number of
classes.
This operation is for training only. It is generally an underestimate of
the full softmax loss.
A common use case is to use this method for training, and calculate the full
softmax loss for evaluation or inference. In this case, you must set
partition_strategy="div"
for the two losses to be consistent, as in the
following example:
if mode == "train":
loss = tf.nn.sampled_sparse_softmax_loss(
weights=weights,
biases=biases,
labels=labels,
inputs=inputs,
...,
partition_strategy="div")
elif mode == "eval":
logits = tf.matmul(inputs, tf.transpose(weights))
logits = tf.nn.bias_add(logits, biases)
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=tf.squeeze(labels),
logits=logits)
See our Candidate Sampling Algorithms Reference
Also see Section 3 of Jean et al., 2014
(pdf) for the math.
Args |
weights
|
A Tensor of shape [num_classes, dim] , or a list of Tensor
objects whose concatenation along dimension 0 has shape
[num_classes, dim]. The (possibly-sharded) class embeddings.
|
biases
|
A Tensor of shape [num_classes] . The class biases.
|
labels
|
A Tensor of type int64 and shape [batch_size, 1] .
The index of the single target class for each row of logits. Note that
this format differs from the labels argument of
nn.sparse_softmax_cross_entropy_with_logits .
|
inputs
|
A Tensor of shape [batch_size, dim] . The forward
activations of the input network.
|
num_sampled
|
An int . The number of classes to randomly sample per batch.
|
num_classes
|
An int . The number of possible classes.
|
sampled_values
|
a tuple of (sampled_candidates , true_expected_count ,
sampled_expected_count ) returned by a *_candidate_sampler function.
(if None, we default to log_uniform_candidate_sampler )
|
remove_accidental_hits
|
A bool . whether to remove "accidental hits"
where a sampled class equals one of the target classes. Default is
True.
|
partition_strategy
|
A string specifying the partitioning strategy, relevant
if len(weights) > 1 . Currently "div" and "mod" are supported.
Default is "mod" . See tf.nn.embedding_lookup for more details.
|
name
|
A name for the operation (optional).
|
Returns |
A batch_size 1-D tensor of per-example sampled softmax losses.
|
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.contrib.nn.sampled_sparse_softmax_loss\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/nn/python/ops/sampling_ops.py#L247-L342) |\n\nComputes and returns the sampled sparse softmax training loss. \n\n tf.contrib.nn.sampled_sparse_softmax_loss(\n weights, biases, labels, inputs, num_sampled, num_classes, sampled_values=None,\n remove_accidental_hits=True, partition_strategy='mod',\n name='sampled_sparse_softmax_loss'\n )\n\nThis is a faster way to train a softmax classifier over a huge number of\nclasses.\n\nThis operation is for training only. It is generally an underestimate of\nthe full softmax loss.\n\nA common use case is to use this method for training, and calculate the full\nsoftmax loss for evaluation or inference. In this case, you must set\n`partition_strategy=\"div\"` for the two losses to be consistent, as in the\nfollowing example: \n\n if mode == \"train\":\n loss = tf.nn.sampled_sparse_softmax_loss(\n weights=weights,\n biases=biases,\n labels=labels,\n inputs=inputs,\n ...,\n partition_strategy=\"div\")\n elif mode == \"eval\":\n logits = tf.matmul(inputs, tf.transpose(weights))\n logits = tf.nn.bias_add(logits, biases)\n loss = tf.nn.sparse_softmax_cross_entropy_with_logits(\n labels=tf.squeeze(labels),\n logits=logits)\n\nSee our [Candidate Sampling Algorithms Reference](https://www.tensorflow.org/extras/candidate_sampling.pdf)\n\nAlso see Section 3 of [Jean et al., 2014](http://arxiv.org/abs/1412.2007)\n([pdf](http://arxiv.org/pdf/1412.2007.pdf)) for the math.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `weights` | A `Tensor` of shape `[num_classes, dim]`, or a list of `Tensor` objects whose concatenation along dimension 0 has shape \\[num_classes, dim\\]. The (possibly-sharded) class embeddings. |\n| `biases` | A `Tensor` of shape `[num_classes]`. The class biases. |\n| `labels` | A `Tensor` of type `int64` and shape `[batch_size, 1]`. The index of the single target class for each row of logits. Note that this format differs from the `labels` argument of [`nn.sparse_softmax_cross_entropy_with_logits`](/api_docs/python/tf/nn/sparse_softmax_cross_entropy_with_logits). |\n| `inputs` | A `Tensor` of shape `[batch_size, dim]`. The forward activations of the input network. |\n| `num_sampled` | An `int`. The number of classes to randomly sample per batch. |\n| `num_classes` | An `int`. The number of possible classes. |\n| `sampled_values` | a tuple of (`sampled_candidates`, `true_expected_count`, `sampled_expected_count`) returned by a `*_candidate_sampler` function. (if None, we default to `log_uniform_candidate_sampler`) |\n| `remove_accidental_hits` | A `bool`. whether to remove \"accidental hits\" where a sampled class equals one of the target classes. Default is True. |\n| `partition_strategy` | A string specifying the partitioning strategy, relevant if `len(weights) \u003e 1`. Currently `\"div\"` and `\"mod\"` are supported. Default is `\"mod\"`. See `tf.nn.embedding_lookup` for more details. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `batch_size` 1-D tensor of per-example sampled softmax losses. ||\n\n\u003cbr /\u003e"]]