TF 2.0 is out! Get hands-on practice at TF World, Oct 28-31. Use code TF20 for 20% off select passes. Register now

tfa.seq2seq.Sampler

View source on GitHub

Class Sampler

Interface for implementing sampling in seq2seq decoders.

Aliases:

Sampler instances are used by BasicDecoder. The normal usage of a sampler is like below: sampler = Sampler(init_args) (initial_finished, initial_inputs) = sampler.initialize(input_tensors) for time_step in range(time): cell_output, cell_state = cell.call(cell_input, previous_state) sample_ids = sampler.sample(time_step, cell_output, cell_state) (finished, next_inputs, next_state) = sampler.next_inputs( time_step,cell_output, cell_state)

Note that all the tensor input should not be feed to Sampler as init() parameters, instead, they should be feed by decoders via initialize().

Properties

batch_size

Batch size of tensor returned by sample.

Returns a scalar int32 tensor. The return value might not available before the invocation of initialize(), in this case, ValueError is raised.

sample_ids_dtype

DType of tensor returned by sample.

Returns a DType. The return value might not available before the invocation of initialize().

sample_ids_shape

Shape of tensor returned by sample, excluding the batch dimension.

Returns a TensorShape. The return value might not available before the invocation of initialize().

Methods

initialize

View source

initialize(
    inputs,
    **kwargs
)

initialize the sampler with the input tensors.

This method suppose to be only invoke once before the calling other methods of the Sampler.

Args:

  • inputs: A (structure of) input tensors, it could be a nested tuple or a single tensor.
  • **kwargs: Other kwargs for initialization. It could contain tensors like mask for inputs, or non tensor parameter.

Returns:

(initial_finished, initial_inputs).

next_inputs

View source

next_inputs(
    time,
    outputs,
    state,
    sample_ids
)

Returns (finished, next_inputs, next_state).

sample

View source

sample(
    time,
    outputs,
    state
)

Returns sample_ids.