Last active
October 28, 2025 12:31
-
-
Save sergeyprokudin/4a50bf9b75e0559c1fcd2cae860b879e to your computer and use it in GitHub Desktop.
Multivariate Gaussian Negative LogLikelihood Loss Keras
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Hi,
Why do you use sum in this piece of code
sigma_trace = -K.sum(logsigma, axis=1)
?
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]."
Hello, I'd like to ask if the variance value you get here is the logarithm of the variance directly obtained. Then how do you design your variance prediction network? Do you take the logarithmic variance in the code after the prediction?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Okay, now I understand. My doubts were clarified. Thank you very much for the information.
Best Regards.