Exemple #1
0
def create_vrnn(generative_class=base.ConditionalNormalDistribution,
                batch_size=2, data_size=3, rnn_hidden_size=4,
                latent_size=5, fcnet_hidden_size=7, encoded_data_size=9,
                encoded_latent_size=11, num_timesteps=7, data_lengths=(7, 4),
                use_tilt=False, random_seed=None):
  """Creates a VRNN and some dummy data to feed it for testing purposes.

  Args:
    generative_class: The class of the generative distribution.
    batch_size: The number of elements per batch.
    data_size: The dimension of the vectors that make up the data sequences.
    rnn_hidden_size: The hidden state dimension of the RNN that forms the
      deterministic part of this VRNN.
    latent_size: The size of the stochastic latent state of the VRNN.
    fcnet_hidden_size: The size of the hidden layer of the fully connected
      networks that parameterize the conditional probability distributions
      of the VRNN.
    encoded_data_size: The size of the output of the data encoding network.
    encoded_latent_size: The size of the output of the latent state encoding
      network.
    num_timesteps: The maximum number of timesteps in the data.
    data_lengths: A tuple of size batch_size that contains the desired lengths
      of each sequence in the dummy data.
    use_tilt: Use a tilting function.
    random_seed: A random seed to feed the VRNN, mainly useful for testing
      purposes.

  Returns:
    model: A VRNN object.
    inputs: A Tensor of shape [num_timesteps, batch_size, data_size], the inputs
      to the model, also known as the observations.
    targets: A Tensor of shape [num_timesteps, batch_size, data_size], the
      desired outputs of the model.
    lengths: A Tensor of shape [batch_size], the lengths of the sequences in the
      batch.
  """

  fcnet_hidden_sizes = [fcnet_hidden_size]
  initializers = {"w": tf.contrib.layers.xavier_initializer(seed=random_seed),
                  "b": tf.zeros_initializer()}
  model = vrnn.create_vrnn(
      data_size,
      latent_size,
      generative_class,
      rnn_hidden_size=rnn_hidden_size,
      fcnet_hidden_sizes=fcnet_hidden_sizes,
      encoded_data_size=encoded_data_size,
      encoded_latent_size=encoded_latent_size,
      use_tilt=use_tilt,
      initializers=initializers,
      random_seed=random_seed)
  inputs = tf.random_uniform([num_timesteps, batch_size, data_size],
                             seed=random_seed, dtype=tf.float32)
  targets = tf.random_uniform([num_timesteps, batch_size, data_size],
                              seed=random_seed, dtype=tf.float32)
  lengths = tf.constant(data_lengths, dtype=tf.int32)
  return model, inputs, targets, lengths
Exemple #2
0
def create_vrnn(generative_class=base.ConditionalNormalDistribution,
                batch_size=2, data_size=3, rnn_hidden_size=4,
                latent_size=5, fcnet_hidden_size=7, encoded_data_size=9,
                encoded_latent_size=11, num_timesteps=7, data_lengths=(7, 4),
                use_tilt=False, random_seed=None):
  """Creates a VRNN and some dummy data to feed it for testing purposes.

  Args:
    generative_class: The class of the generative distribution.
    batch_size: The number of elements per batch.
    data_size: The dimension of the vectors that make up the data sequences.
    rnn_hidden_size: The hidden state dimension of the RNN that forms the
      deterministic part of this VRNN.
    latent_size: The size of the stochastic latent state of the VRNN.
    fcnet_hidden_size: The size of the hidden layer of the fully connected
      networks that parameterize the conditional probability distributions
      of the VRNN.
    encoded_data_size: The size of the output of the data encoding network.
    encoded_latent_size: The size of the output of the latent state encoding
      network.
    num_timesteps: The maximum number of timesteps in the data.
    data_lengths: A tuple of size batch_size that contains the desired lengths
      of each sequence in the dummy data.
    use_tilt: Use a tilting function.
    random_seed: A random seed to feed the VRNN, mainly useful for testing
      purposes.

  Returns:
    model: A VRNN object.
    inputs: A Tensor of shape [num_timesteps, batch_size, data_size], the inputs
      to the model, also known as the observations.
    targets: A Tensor of shape [num_timesteps, batch_size, data_size], the
      desired outputs of the model.
    lengths: A Tensor of shape [batch_size], the lengths of the sequences in the
      batch.
  """

  fcnet_hidden_sizes = [fcnet_hidden_size]
  initializers = {"w": tf.contrib.layers.xavier_initializer(seed=random_seed),
                  "b": tf.zeros_initializer()}
  model = vrnn.create_vrnn(
      data_size,
      latent_size,
      generative_class,
      rnn_hidden_size=rnn_hidden_size,
      fcnet_hidden_sizes=fcnet_hidden_sizes,
      encoded_data_size=encoded_data_size,
      encoded_latent_size=encoded_latent_size,
      use_tilt=use_tilt,
      initializers=initializers,
      random_seed=random_seed)
  inputs = tf.random_uniform([num_timesteps, batch_size, data_size],
                             seed=random_seed, dtype=tf.float32)
  targets = tf.random_uniform([num_timesteps, batch_size, data_size],
                              seed=random_seed, dtype=tf.float32)
  lengths = tf.constant(data_lengths, dtype=tf.int32)
  return model, inputs, targets, lengths
Exemple #3
0
 def dummmy_dataset_and_model_fn(self, *unused_args, **unused_kwargs):
     # We ignore the arguments in the dummy but need to preserve prototype.
     batch_elements = 5
     sequence_length = 4
     data_dimensions = 3
     dataset = tf.data.Dataset.from_tensors(
         tf.zeros((sequence_length, batch_elements, data_dimensions),
                  dtype=tf.float32))
     inputs = dataset.make_one_shot_iterator().get_next()
     targets = tf.zeros_like(inputs)
     lengths = tf.constant([sequence_length] * batch_elements)
     mean = tf.constant((0.0, 0.0, 0.0))
     model = vrnn.create_vrnn(data_dimensions, 1,
                              base.ConditionalNormalDistribution)
     return inputs, targets, lengths, model, mean
def create_dataset_and_model(config, split, shuffle, repeat):
    """Creates the dataset and model for a given config.

  Args:
    config: A configuration object with config values accessible as properties.
      Most likely a FLAGS object. This function expects the properties
      batch_size, dataset_path, dataset_type, and latent_size to be defined.
    split: The dataset split to load.
    shuffle: If true, shuffle the dataset randomly.
    repeat: If true, repeat the dataset endlessly.
  Returns:
    inputs: A batch of input sequences represented as a dense Tensor of shape
      [time, batch_size, data_dimension].
    targets: A batch of target sequences represented as a dense Tensor of
      shape [time, batch_size, data_dimension].
    lens: An int Tensor of shape [batch_size] representing the lengths of each
      sequence in the batch.
    model: A vrnn.VRNNCell model object.
  Raises:
    ValueError: if the config is invalid.
  """
    sigma_min = 0.0
    if config.dataset_type == "pianoroll":
        inputs, targets, lengths, mean = datasets.create_pianoroll_dataset(
            config.dataset_path,
            split,
            config.batch_size,
            shuffle=shuffle,
            repeat=repeat)
        # Convert the mean of the training set to logit space so it can be used to
        # initialize the bias of the generative distribution.
        emission_bias_init = -tf.log(
            1. / tf.clip_by_value(mean, 0.0001, 0.9999) - 1)
        emission_distribution_class = base.ConditionalBernoulliDistribution
    elif config.dataset_type == "speech":
        inputs, targets, lengths = datasets.create_speech_dataset(
            config.dataset_path,
            config.batch_size,
            samples_per_timestep=config.data_dimension,
            prefetch_buffer_size=1,
            shuffle=False,
            repeat=False)
        # There is no bias for the generative distribution because the test set
        # is assumed to be already standardized with the training set statistics.
        mean = None
        emission_bias_init = None
        emission_distribution_class = base.ConditionalNormalDistribution
    if config.model == "vrnn":
        model = vrnn.create_vrnn(inputs.get_shape().as_list()[2],
                                 config.latent_size,
                                 emission_distribution_class,
                                 emission_bias_init=emission_bias_init,
                                 proposal_type=config.proposal_type,
                                 sigma_min=sigma_min,
                                 raw_sigma_bias=0.5,
                                 use_tilt=(config.bound == "fivo-aux"))
    elif config.model == "srnn":
        model = srnn.create_srnn(inputs.get_shape().as_list()[2],
                                 config.latent_size,
                                 emission_distribution_class,
                                 emission_bias_init=emission_bias_init,
                                 proposal_type=config.proposal_type,
                                 sigma_min=sigma_min,
                                 raw_sigma_bias=0.5,
                                 use_tilt=(config.bound == "fivo-aux"))
    else:
        raise ValueError("model flag: %s is unrecognized" % config.model)
    return inputs, targets, lengths, model, mean