Skip to content

Instantly share code, notes, and snippets.

@sergeyprokudin
Last active January 18, 2024 00:58
Show Gist options
  • Save sergeyprokudin/4a50bf9b75e0559c1fcd2cae860b879e to your computer and use it in GitHub Desktop.
Save sergeyprokudin/4a50bf9b75e0559c1fcd2cae860b879e to your computer and use it in GitHub Desktop.
Multivariate Gaussian Negative LogLikelihood Loss Keras
import keras.backend as K
import numpy as np
def gaussian_nll(ytrue, ypreds):
"""Keras implmementation of multivariate Gaussian negative loglikelihood loss function.
This implementation implies diagonal covariance matrix.
Parameters
----------
ytrue: tf.tensor of shape [n_samples, n_dims]
ground truth values
ypreds: tf.tensor of shape [n_samples, n_dims*2]
predicted mu and logsigma values (e.g. by your neural network)
Returns
-------
neg_log_likelihood: float
negative loglikelihood averaged over samples
This loss can then be used as a target loss for any keras model, e.g.:
model.compile(loss=gaussian_nll, optimizer='Adam')
"""
n_dims = int(int(ypreds.shape[1])/2)
mu = ypreds[:, 0:n_dims]
logsigma = ypreds[:, n_dims:]
mse = -0.5*K.sum(K.square((ytrue-mu)/K.exp(logsigma)),axis=1)
sigma_trace = -K.sum(logsigma, axis=1)
log2pi = -0.5*n_dims*np.log(2*np.pi)
log_likelihood = mse+sigma_trace+log2pi
return K.mean(-log_likelihood)
@aangius
Copy link

aangius commented Jun 4, 2021

Hi,
Why do you use sum in this piece of code
sigma_trace = -K.sum(logsigma, axis=1)
?

@lingleong981130
Copy link

Hi, may I know how to solve this error??
"ValueError: Dimensions must be equal, but are 128 and 64 for '{{node gaussian_nll/sub}} = Sub[T=DT_FLOAT](Cast, gaussian_nll/strided_slice)' with input shapes: [?,128,128,3], [?,64,128,3]."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment