def kld(self, params): ks = [] for i, (low, high) in enumerate( zip(self.action_space.low, self.action_space.high)): if low + 1 == high: k = kullback_leibler_divergence( self.ps[i], params[:, :, :1]) + kullback_leibler_divergence( 1 - self.ps[i], 1 - params[:, :, :1]) params = params[:, :, 1:] else: n = high - low + 1 k = kullback_leibler_divergence(self.ps[i], params[:, :, :n]) params = params[:, :, n:] ks.append(k) return maybe_merge(ks, mode="sum"), params
def vae_loss(x, x_decoded_mean): x = K.flatten(x) x_decoded_mean = K.flatten(x_decoded_mean) # xent_loss = max_length * objectives.binary_crossentropy(x, x_decoded_mean) # xent_loss = max_length * objectives.categorical_crossentropy(x, x_decoded_mean) xent_loss = objectives.kullback_leibler_divergence( x, x_decoded_mean) # xent_loss = cos_distance(x, x_decoded_mean) kl_loss = -0.5 * K.mean( 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1) return xent_loss + kl_loss
def kl_loss(y_true, y_pred): """Calculates Kullback-Leibler divergence between prior and posterior distributions Args: :param y_true: (tf.tensor) true distribution of data :param y_pred: (tf.tensor) predicted distribution of data Returns: Average relative entropy between prior and posterior distributions """ KLdiv = kullback_leibler_divergence(y_true, y_pred) # PREDICTION_FETCHES.update({'KL_divergence': KLdiv}) KLloss = tf.reduce_mean(KLdiv) # DeltaKL = KLdiv - KLloss # TRAIN_FETCHES.update({'DeltaKL': DeltaKL}) return KLloss
def kld(self, params): return kullback_leibler_divergence( self.p, params[:, :, :self.action_space.n]), params[:, :, self.action_space.n:]
padding='valid',name='featuresConv') featFlat = Flatten() featDense = Dense(50,name='features',activation='softmax') features_mutant = featDense(featFlat(featConv(mutant))) # mutant features features_nonmutant = featDense(featFlat(featConv(inp))) # non-mutant features disc_acc = (tf.reduce_mean(1-p_mutant) + tf.reduce_mean(p_nonmutant))/2 mutator_loss = tf.reduce_mean(inp*tf.log(K.clip(mutant, K.epsilon(), 1-K.epsilon()))) # sum of squared errors between feature maps of mutant and non-mutant inputs # featurematch_loss = tf.reduce_sum(tf.square(tf.subtract(features_nonmutant,features_mutant))) # K-L divergence between feature maps of mutant and non-mutant inputs featurematch_loss = tf.reduce_mean(kullback_leibler_divergence(features_nonmutant,features_mutant)) D_loss = - tf.reduce_mean(tf.log(p_nonmutant)) - tf.reduce_mean(tf.log(1-p_mutant)) M_loss = featurematch_loss # -tf.reduce_mean(tf.log(p_mutant)) #+ mutator_loss # gradient clipping # theta_D is list of D's params theta_D = [x for x in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) if 'discriminator' in x.name] theta_M = [x for x in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) if 'mutator' in x.name] clip_D = [p.assign(tf.clip_by_value(p, -0.01, 0.01)) for p in theta_D] D_solver = (tf.train.AdamOptimizer(learning_rate=1e-3) .minimize(D_loss, var_list=theta_D)) M_solver = (tf.train.AdamOptimizer(learning_rate=1e-3)
def cost(p, q): kl = kullback_leibler_divergence(p, q) return similar * kl + (1 - similar) * K.relu(margin - kl)