def batch_cca_loss(y_true, y_pred): """ Return Sum of the diagonal - Sum of upper and lower triangles """ trace = TN.trace(y_true[0, :, :]) triu_sum = K.sum(K.abs(TB.triu(y_true[0, :, :], k=1))) tril_sum = K.sum(K.abs(TB.tril(y_true[0, :, :], k=-1))) return trace - tril_sum - triu_sum
def self_similarity_cosine(x): """ :param x: shape (T,D) :returns cosine similarity matrix, shape (T,T), zeroed at diagonal and upper triangle """ assert x.ndim == 2 x2 = T.sqrt(T.clip(T.sum(T.sqr(x), axis=1), numpy.float32(1.e-20), numpy.float32(1.e20))) assert x2.ndim == 1 x_ = x / x2.dimshuffle(0, 'x') xx = T.dot(x_, x_.T) assert xx.ndim == 2 from theano.tensor.basic import tril return tril(xx, 1)
def self_similarity_cosine(x): """ :param x: shape (T,D) :returns cosine similarity matrix, shape (T,T), zeroed at diagonal and upper triangle """ assert x.ndim == 2 x2 = T.sqrt( T.clip(T.sum(T.sqr(x), axis=1), numpy.float32(1.e-20), numpy.float32(1.e20))) assert x2.ndim == 1 x_ = x / x2.dimshuffle(0, 'x') xx = T.dot(x_, x_.T) assert xx.ndim == 2 from theano.tensor.basic import tril return tril(xx, 1)