Пример #1
0
def gumbel_loss(x, x_hat):
    q_y = K.reshape(logits_y, (-1, N, M))
    q_y = softmax(q_y)
    log_q_y = K.log(q_y + 1e-20)
    kl_tmp = q_y * (log_q_y - K.log(1.0/M))
    KL = K.sum(kl_tmp, axis=(1, 2))
    elbo = latent_dim * bce(x, x_hat) - KL
    # elbo = latent_dim * mse(x, x_hat) - KL 
    return elbo
Пример #2
0
def vae_loss(x, x_mean):
    xent_loss = img_dim * bce(x_s, x_mean)

    kl_z_loss = mvn_kl(z_mean_en, z_logvar_en)
    kl_a_loss = mvn_kl(a_mean_en, a_logvar_en, a_mean_de, a_logvar_de)
    kl_loss = kl_z_loss + kl_a_loss

    # return kl_loss + xent_loss
    return kl_weight * kl_loss + xent_loss
Пример #3
0
def vae_label_loss(x, x_prob_de):
	x_loss = img_dim * bce(x, x_prob_de)
	y_loss = - K.log(nb_classes) 
	kl_a_loss = mvn_kl(a_mean_en, a_logvar_en, a_mean_de, a_logvar_de)
	kl_z_loss = mvn_kl(z_mean_en, z_logvar_en)

	rec_loss = x_loss + y_loss
	kl_loss = kl_a_loss + kl_z_loss
	total_loss = rec_loss + kl_weight * kl_loss

	return total_loss 
Пример #4
0
def vae_unlabel_loss(x_u, x_u_prob_de):
	for i in xrange(nb_classes):
		y_i_prob = y_u_probs_en[:, i]
		x_u_prob_de_y = x_u_prob_de[i]
		a_u_mean_de_y = a_u_mean_de_y[i] 
		a_u_logvar_de_y = a_u_logvar_de_y[i] 
		z_u_mean_en_y = z_u_mean_en_y[i] 
		z_u_logvar_en_y = z_u_logvar_en_y[i] 

		x_u_loss = img_dim * bce(x_u, x_u_prob_de_y)
		kl_y_loss = K.log(nb_classes * y_i_prob)
		kl_a_u_loss = mvn_kl(a_u_mean_en, a_u_logvar_en,
			a_u_mean_de_y, a_u_logvar_de_y)
		kl_z_u_loss = mvn_kl(z_u_mean_en_y, z_u_logvar_en_y)

		if i==0:
			rec_loss = y_i_prob * x_u_loss
			kl_loss = y_i_prob * (kl_y_loss + kl_a_u_loss + kl_z_u_loss)
		else:
			rec_loss += y_i_prob * x_u_loss
			kl_loss += y_i_prob * (kl_y_loss + kl_a_u_loss + kl_z_u_loss)

	total_loss = rec_loss + kl_weight * kl_loss		
	return total_loss
Пример #5
0
def bcce(true, pred):
    if K.ndim(true) == 3:
        return cce(K.clip(true, 1e-8, 1-1e-8), K.clip(pred, 1e-8, 1-1e-8))
    else:
        return bce(K.clip(true, 1e-8, 1-1e-8), K.clip(pred, 1e-8, 1-1e-8))
Пример #6
0
def BCE(x, y):
    return bce(K.batch_flatten(x),
               K.batch_flatten(y))