def DiscretizedMixtureLogisticLoss(outputs, targets, hparams, lengths=None, mask=None, max_len=None): if lengths is None and mask is None: raise RuntimeError('Please provide either lengths or mask') #[batch_size, time_length, 1] if mask is None: mask = sequence_mask(lengths, max_len, True) #[batch_size, time_length, dimension] ones = tf.ones( [tf.shape(mask)[0], tf.shape(mask)[1], tf.shape(targets)[-1]], tf.float32) mask_ = mask * ones losses = discretized_mix_logistic_loss( outputs, targets, num_classes=hparams.quantize_channels, log_scale_min=hparams.log_scale_min, reduce=False) with tf.control_dependencies( [tf.assert_equal(tf.shape(losses), tf.shape(targets))]): return tf.reduce_sum(losses * mask_) / tf.reduce_sum(mask_)
def GaussianMaximumLikelihoodEstimation(outputs, targets, hparams, lengths=None, mask=None, max_len=None): if lengths is None and mask is None: raise RuntimeError('Please provide either lengths or mask') #[batch_size, time_length, 1] if mask is None: mask = sequence_mask(lengths, max_len, True) #[batch_size, time_length, dimension] ones = tf.ones( [tf.shape(mask)[0], tf.shape(mask)[1], tf.shape(targets)[-1]], tf.float32) mask_ = mask * ones losses = gaussian_maximum_likelihood_estimation_loss( outputs, targets, log_scale_min_gauss=hparams.log_scale_min_gauss, reduce=False) with tf.control_dependencies( [tf.assert_equal(tf.shape(losses), tf.shape(targets))]): return tf.reduce_sum(losses * mask_) / tf.reduce_sum(mask_)
def MaskedCrossEntropyLoss(outputs, targets, lengths=None, mask=None, max_len=None): if lengths is None and mask is None: raise RuntimeError('Please provide either lengths or mask') #[batch_size, time_length] if mask is None: mask = sequence_mask(lengths, max_len, False) #One hot encode targets (outputs.shape[-1] = hparams.quantize_channels) targets_ = tf.one_hot(targets, depth=tf.shape(outputs)[-1]) with tf.control_dependencies( [tf.assert_equal(tf.shape(outputs), tf.shape(targets_))]): losses = tf.nn.softmax_cross_entropy_with_logits_v2(logits=outputs, labels=targets_) with tf.control_dependencies( [tf.assert_equal(tf.shape(mask), tf.shape(losses))]): masked_loss = losses * mask return tf.reduce_sum(masked_loss) / tf.count_nonzero(masked_loss, dtype=tf.float32)
def GaussianMaximumLikelihoodEstimation(outputs, targets, hparams, lengths=None, mask=None, max_len=None): if lengths is None and mask is None: raise RuntimeError('Please provide either lengths or mask') #[batch_size, time_length, 1] if mask is None: mask = sequence_mask(lengths, max_len, True) #[batch_size, time_length, dimension] ones = tf.ones( [tf.shape(mask)[0], tf.shape(mask)[1], tf.shape(targets)[-1]], tf.float32) mask_ = mask * ones ## as the return value of gaussian_maximum_likelihood_estimation_loss(), lossses could be a number (if reduce=True) #or could be a tensor with last element is [1] (if reduce=False) losses = gaussian_maximum_likelihood_estimation_loss( outputs, targets, log_scale_min_gauss=hparams.log_scale_min_gauss, reduce=False) print( 'losses in modules.py, gaussianMaximumlikehoodestimation function. {}'. format(losses)) with tf.control_dependencies( [tf.assert_equal(tf.shape(losses), tf.shape(targets))]): return tf.reduce_sum(losses * mask_) / tf.reduce_sum(mask_)
def MaskedMeanSquaredError(outputs, targets, lengths=None, mask=None, max_len=None): if lengths is None and mask is None: raise RuntimeError('Please provide either lengths or mask') #[batch_size, frames, 1] if mask is None: mask = sequence_mask(lengths, max_len, True) #[batch_size, frames, freq] ones = tf.ones([tf.shape(mask)[0], tf.shape(mask)[1], tf.shape(targets)[-1]], tf.float32) mask_ = mask * ones with tf.control_dependencies([tf.assert_equal(tf.shape(targets), tf.shape(mask_))]): return tf.losses.mean_squared_error(labels=targets, predictions=outputs, weights=mask_)
def DiscretizedMixtureLogisticLoss(outputs, targets, hparams, lengths=None, mask=None, max_len=None): if lengths is None and mask is None: raise RuntimeError('Please provide either lengths or mask') #[batch_size, time_length, 1] if mask is None: mask = sequence_mask(lengths, max_len, True) #[batch_size, time_length, dimension] ones = tf.ones([tf.shape(mask)[0], tf.shape(mask)[1], tf.shape(targets)[-1]], tf.float32) mask_ = mask * ones losses = discretized_mix_logistic_loss( outputs, targets, num_classes=hparams.quantize_channels, log_scale_min=hparams.log_scale_min, reduce=False) with tf.control_dependencies([tf.assert_equal(tf.shape(losses), tf.shape(targets))]): return tf.reduce_sum(losses * mask_) / tf.reduce_sum(mask_)
def MaskedCrossEntropyLoss(outputs, targets, lengths=None, mask=None, max_len=None): if lengths is None and mask is None: raise RuntimeError('Please provide either lengths or mask') #[batch_size, time_length] if mask is None: mask = sequence_mask(lengths, max_len, False) #One hot encode targets (outputs.shape[-1] = hparams.quantize_channels) targets_ = tf.one_hot(targets, depth=tf.shape(outputs)[-1]) with tf.control_dependencies([tf.assert_equal(tf.shape(outputs), tf.shape(targets_))]): losses = tf.nn.softmax_cross_entropy_with_logits_v2(logits=outputs, labels=targets_) with tf.control_dependencies([tf.assert_equal(tf.shape(mask), tf.shape(losses))]): masked_loss = losses * mask return tf.reduce_sum(masked_loss) / tf.count_nonzero(masked_loss, dtype=tf.float32)