Source code for pysummarization.computabledistance.kl_divergence

# -*- coding: utf-8 -*-
import numpy as np
from pysummarization.computable_distance import ComputableDistance

[docs]class KLDivergence(ComputableDistance): ''' Compute Kullback-Leibler divergence(KLD) between two vectors. This class considers KLD as a kind of distance. '''
[docs] def compute(self, x_arr, y_arr): ''' Compute distance. Args: x_arr: `np.ndarray` of vectors. y_arr: `np.ndarray` of vectors. Retruns: `np.ndarray` of distances. ''' y_arr += 1e-08 return np.sum(x_arr * np.log(x_arr / y_arr), axis=-1)