tensorflow::ops::FixedUnigramCandidateSampler

#include <candidate_sampling_ops.h>

Generates labels for candidate sampling with a learned unigram distribution.

Summary

A unigram sampler could use a fixed unigram distribution read from a file or passed in as an in-memory array instead of building up the distribution from data on the fly. There is also an option to skew the distribution by applying a distortion power to the weights.

The vocabulary file should be in CSV-like format, with the last field being the weight associated with the word.

For each batch, this op picks a single set of sampled candidate labels.

The advantages of sampling candidates per-batch are simplicity and the possibility of efficient dense matrix multiplication. The disadvantage is that the sampled candidates must be chosen independently of the context and of the true labels.

Arguments:

  • scope: A Scope object
  • true_classes: A batch_size * num_true matrix, in which each row contains the IDs of the num_true target_classes in the corresponding original label.
  • num_true: Number of true labels per context.
  • num_sampled: Number of candidates to randomly sample per batch.
  • unique: If unique is true, we sample with rejection, so that all sampled candidates in a batch are unique. This requires some approximation to estimate the post-rejection sampling probabilities.
  • range_max: The sampler will sample integers from the interval [0, range_max).

Optional attributes (see Attrs):

  • vocab_file: Each valid line in this file (which should have a CSV-like format) corresponds to a valid word ID. IDs are in sequential order, starting from num_reserved_ids. The last entry in each line is expected to be a value corresponding to the count or relative probability. Exactly one of vocab_file and unigrams needs to be passed to this op.
  • distortion: The distortion is used to skew the unigram probability distribution. Each weight is first raised to the distortion's power before adding to the internal unigram distribution. As a result, distortion = 1.0 gives regular unigram sampling (as defined by the vocab file), and distortion = 0.0 gives a uniform distribution.
  • num_reserved_ids: Optionally some reserved IDs can be added in the range [0, ..., num_reserved_ids) by the users. One use case is that a special unknown word token is used as ID 0. These IDs will have a sampling probability of 0.
  • num_shards: A sampler can be used to sample from a subset of the original range in order to speed up the whole computation through parallelism. This parameter (together with 'shard') indicates the number of partitions that are being used in the overall computation.
  • shard: A sampler can be used to sample from a subset of the original range in order to speed up the whole computation through parallelism. This parameter (together with 'num_shards') indicates the particular partition number of a sampler op, when partitioning is being used.
  • unigrams: A list of unigram counts or probabilities, one per ID in sequential order. Exactly one of vocab_file and unigrams should be passed to this op.
  • seed: If either seed or seed2 are set to be non-zero, the random number generator is seeded by the given seed. Otherwise, it is seeded by a random seed.
  • seed2: An second seed to avoid seed collision.

Returns:

  • Output sampled_candidates: A vector of length num_sampled, in which each element is the ID of a sampled candidate.
  • Output true_expected_count: A batch_size * num_true matrix, representing the number of times each candidate is expected to occur in a batch of sampled candidates. If unique=true, then this is a probability.
  • Output sampled_expected_count: A vector of length num_sampled, for each sampled candidate representing the number of times the candidate is expected to occur in a batch of sampled candidates. If unique=true, then this is a probability.

Constructors and Destructors

FixedUnigramCandidateSampler(const ::tensorflow::Scope & scope, ::tensorflow::Input true_classes, int64 num_true, int64 num_sampled, bool unique, int64 range_max)
FixedUnigramCandidateSampler(const ::tensorflow::Scope & scope, ::tensorflow::Input true_classes, int64 num_true, int64 num_sampled, bool unique, int64 range_max, const FixedUnigramCandidateSampler::Attrs & attrs)

Public attributes

sampled_candidates
sampled_expected_count
true_expected_count

Public static functions

Distortion(float x)
NumReservedIds(int64 x)
NumShards(int64 x)
Seed(int64 x)
Seed2(int64 x)
Shard(int64 x)
Unigrams(const gtl::ArraySlice< float > & x)
VocabFile(StringPiece x)

Structs

tensorflow::ops::FixedUnigramCandidateSampler::Attrs

Optional attribute setters for FixedUnigramCandidateSampler.

Public attributes

sampled_candidates

::tensorflow::Output sampled_candidates

sampled_expected_count

::tensorflow::Output sampled_expected_count

true_expected_count

::tensorflow::Output true_expected_count

Public functions

FixedUnigramCandidateSampler

 FixedUnigramCandidateSampler(
  const ::tensorflow::Scope & scope,
  ::tensorflow::Input true_classes,
  int64 num_true,
  int64 num_sampled,
  bool unique,
  int64 range_max
)

FixedUnigramCandidateSampler

 FixedUnigramCandidateSampler(
  const ::tensorflow::Scope & scope,
  ::tensorflow::Input true_classes,
  int64 num_true,
  int64 num_sampled,
  bool unique,
  int64 range_max,
  const FixedUnigramCandidateSampler::Attrs & attrs
)

Public static functions

Distortion

Attrs Distortion(
  float x
)

NumReservedIds

Attrs NumReservedIds(
  int64 x
)

NumShards

Attrs NumShards(
  int64 x
)

Seed

Attrs Seed(
  int64 x
)

Seed2

Attrs Seed2(
  int64 x
)

Shard

Attrs Shard(
  int64 x
)

Unigrams

Attrs Unigrams(
  const gtl::ArraySlice< float > & x
)

VocabFile

Attrs VocabFile(
  StringPiece x
)