(body_output, unused_targets, model_hparams, unused_vocab_size,
                 hard=False)
return ret
Applies the Mixture Density Network on top of the LSTM outputs.

  Args:
    body_output: outputs from LSTM with shape [batch, seqlen, 1, hidden_size]
    unused_targets: what the ground truth SVG outputted should be (unused).
    model_hparams: hyper-parameters, should include num_mixture,
      mix_temperature, and gauss_temperature.
    unused_vocab_size: unused
    hard: whether to force predict mode functionality, or return all MDN
      components

  Returns:
    The MDN output. Could be shape [batch, seqlen, 1, 10] if in predict mode
      (or hard=True) or shape [batch, seqlen, 1, 4 + 6 * num_mix * 3], in train.
Applies the Mixture Density Network on top of the LSTM outputs.
Applies the Mixture Density Network on top of the LSTM outputs .
def real_svg_top(body_output, unused_targets, model_hparams, unused_vocab_size,
                 hard=False):
  """Applies the Mixture Density Network on top of the LSTM outputs.

  Args:
    body_output: outputs from LSTM with shape [batch, seqlen, 1, hidden_size]
    unused_targets: what the ground truth SVG outputted should be (unused).
    model_hparams: hyper-parameters, should include num_mixture,
      mix_temperature, and gauss_temperature.
    unused_vocab_size: unused
    hard: whether to force predict mode functionality, or return all MDN
      components

  Returns:
    The MDN output. Could be shape [batch, seqlen, 1, 10] if in predict mode
      (or hard=True) or shape [batch, seqlen, 1, 4 + 6 * num_mix * 3], in train.
  """
  # mixture of gaussians for 6 args plus 4 extra states for cmds
  num_mix = model_hparams.num_mixture
  nout = 4 + 6 * num_mix * 3

  # the 'hard' option is meant to be used if 'top' is called within body
  with tf.variable_scope('real_top', reuse=tf.AUTO_REUSE):
    ret = tf.layers.dense(body_output, nout, name='top')
    batch_size = common_layers.shape_list(ret)[0]

    if hard or model_hparams.mode == tf.estimator.ModeKeys.PREDICT:
      temperature = model_hparams.mix_temperature

      # apply temperature, do softmax
      command = tf.identity(ret[:, :, :, :4]) / temperature
      command = tf.exp(command -
                       tf.reduce_max(command, axis=[-1], keepdims=True))
      command = command / tf.reduce_sum(command, axis=[-1], keepdims=True)

      # sample from the given probs, this is the same as get_pi_idx,
      # and already returns not soft prob
      command = tf.distributions.Categorical(probs=command).sample()
      # this is now [batch, seq, 1], need to make it one_hot
      command = tf.one_hot(command, 4)

      arguments = ret[:, :, :, 4:]
      # args are [batch, seq, 1, 6*3*num_mix]. want [batch * seq * 6, 3*num_mix]
      arguments = tf.reshape(arguments, [-1, 3 * num_mix])

      out_logmix, out_mean, out_logstd = _get_mdn_coef(arguments)
      # these are [batch*seq*6, num_mix]

      # apply temp to logmix
      out_logmix = tf.identity(out_logmix) / temperature
      out_logmix = tf.exp(out_logmix -
                          tf.reduce_max(out_logmix, axis=[-1], keepdims=True))
      out_logmix = out_logmix / tf.reduce_sum(
          out_logmix, axis=[-1], keepdims=True)
      # get_pi_idx
      out_logmix = tf.distributions.Categorical(probs=out_logmix).sample()
      # should now be [batch*seq*6, 1]
      out_logmix = tf.cast(out_logmix, tf.int32)
      out_logmix = tf.reshape(out_logmix, [-1])
      # prepare for gather
      out_logmix = tf.stack(
          [tf.range(tf.size(out_logmix)), out_logmix], axis=-1)

      chosen_mean = tf.gather_nd(out_mean, out_logmix)
      chosen_logstd = tf.gather_nd(out_logstd, out_logmix)

      # sample!!
      rand_gaussian = (tf.random.normal(tf.shape(chosen_mean)) *
                       tf.sqrt(model_hparams.gauss_temperature))
      arguments = chosen_mean + tf.exp(chosen_logstd) * rand_gaussian
      arguments = tf.reshape(arguments, [batch_size, -1, 1, 6])

      # concat with the command we picked!
      ret = tf.concat([command, arguments], axis=-1)

  return ret
def real_svg_top ( body_output , unused_targets , model_hparams , unused_vocab_size , hard = False ) : # mixture of gaussians for 6 args plus 4 extra states for cmds num_mix = model_hparams . num_mixture nout = 4 + 6 * num_mix * 3 # the 'hard' option is meant to be used if 'top' is called within body with tf . variable_scope ( 'real_top' , reuse = tf . AUTO_REUSE ) : ret = tf . layers . dense ( body_output , nout , name = 'top' ) batch_size = common_layers . shape_list ( ret ) [ 0 ] if hard or model_hparams . mode == tf . estimator . ModeKeys . PREDICT : temperature = model_hparams . mix_temperature # apply temperature, do softmax command = tf . identity ( ret [ : , : , : , : 4 ] ) / temperature command = tf . exp ( command - tf . reduce_max ( command , axis = [ - 1 ] , keepdims = True ) ) command = command / tf . reduce_sum ( command , axis = [ - 1 ] , keepdims = True ) # sample from the given probs, this is the same as get_pi_idx, # and already returns not soft prob command = tf . distributions . Categorical ( probs = command ) . sample ( ) # this is now [batch, seq, 1], need to make it one_hot command = tf . one_hot ( command , 4 ) arguments = ret [ : , : , : , 4 : ] # args are [batch, seq, 1, 6*3*num_mix]. want [batch * seq * 6, 3*num_mix] arguments = tf . reshape ( arguments , [ - 1 , 3 * num_mix ] ) out_logmix , out_mean , out_logstd = _get_mdn_coef ( arguments ) # these are [batch*seq*6, num_mix] # apply temp to logmix out_logmix = tf . identity ( out_logmix ) / temperature out_logmix = tf . exp ( out_logmix - tf . reduce_max ( out_logmix , axis = [ - 1 ] , keepdims = True ) ) out_logmix = out_logmix / tf . reduce_sum ( out_logmix , axis = [ - 1 ] , keepdims = True ) # get_pi_idx out_logmix = tf . distributions . Categorical ( probs = out_logmix ) . sample ( ) # should now be [batch*seq*6, 1] out_logmix = tf . cast ( out_logmix , tf . int32 ) out_logmix = tf . reshape ( out_logmix , [ - 1 ] ) # prepare for gather out_logmix = tf . stack ( [ tf . range ( tf . size ( out_logmix ) ) , out_logmix ] , axis = - 1 ) chosen_mean = tf . gather_nd ( out_mean , out_logmix ) chosen_logstd = tf . gather_nd ( out_logstd , out_logmix ) # sample!! rand_gaussian = ( tf . random . normal ( tf . shape ( chosen_mean ) ) * tf . sqrt ( model_hparams . gauss_temperature ) ) arguments = chosen_mean + tf . exp ( chosen_logstd ) * rand_gaussian arguments = tf . reshape ( arguments , [ batch_size , - 1 , 1 , 6 ] ) # concat with the command we picked! ret = tf . concat ( [ command , arguments ] , axis = - 1 ) return ret