deepxde icon indicating copy to clipboard operation
deepxde copied to clipboard

Custom loss in the form of Gaussian

Open JerolSOIBAM opened this issue 1 year ago • 3 comments

Hey @lululxvi, Is it possible that we can define a custom loss in deepxde so that the output has both mean variance like the code shown below?

Thank you in advance!

def gaussian_nll(y_true, y_pred):
    # y_true is the true labels
    # y_pred is the predicted labels, which includes both the mean and standard deviation
    y_pred_mean, y_pred_std = tf.split(y_pred, 2, axis=-1)
    y_pred_std = tf.nn.softplus(y_pred_std)  # ensure standard deviation is positive
    mse = 0.5 * tf.square((y_true - y_pred_mean) / y_pred_std)
    log_likelihood = mse + tf.math.log(y_pred_std) + 0.5 * np.log(2.0 * np.pi)
    return tf.reduce_mean(log_likelihood)

JerolSOIBAM avatar May 10 '23 09:05 JerolSOIBAM

Yes.

lululxvi avatar May 18 '23 21:05 lululxvi

Hi @lululxvi, Could you please guide me how may i address this?

JerolSOIBAM avatar Jun 13 '23 09:06 JerolSOIBAM

See https://deepxde.readthedocs.io/en/stable/modules/deepxde.html#deepxde.model.Model.compile. loss can be a python loss function.

lululxvi avatar Jun 22 '23 18:06 lululxvi