<p>Methods like these are not easy to implement, but there is a trick. Define the loss like this:</p>

<pre><code>import keras.backend as K

def regression_nll_loss(sigma_sq, epsilon = 1e-6):
    def nll_loss(y_true, y_pred):
        return 0.5 * K.mean(K.log(sigma_sq + epsilon) + K.square(y_true - y_pred) / (sigma_sq + epsilon))

    return nll_loss
</code></pre>

<p>Then define a model with two outputs, one for the mean and another for the variance:</p>

<pre><code>from keras.models import Model
from keras.layers import Dense, Input

inp = Input(shape=(1,))
x = Dense(32, activation="relu")(inp)
x = Dense(32, activation="relu")(x)
mean = Dense(1, activation="linear")(x)
var = Dense(1, activation="softplus")(x)

train_model = Model(inp, mean)
pred_model = Model(inp, [mean, var])

train_model.compile(loss=regression_nll_loss(var), optimizer="adam")

train_model.fit(x, y, ...)

mean, var = pred_model.predict(some_input)
</code></pre>

<p>The trick is to explicitly pass the tensor for the variance to the loss, so it only needs two inputs, and supervision is only performed for the mean. Then you need to define two models that share weights, one for training, and another for testing/inference. This latter model returns both the mean and variance.</p>

<p>Remember to use a softplus activation for the variance to keep it positive. I have implemented this loss for use with <a href="https://arxiv.org/abs/1612.01474" rel="nofollow noreferrer">Deep Ensembles</a>, you can find an example <a href="https://github.com/mvaldenegro/keras-uncertainty/blob/master/examples/regression_deep-ensemble.py" rel="nofollow noreferrer">here</a>.</p>
Neural Network prediction intervals - MVE method
<p>I want to train a neural network which also returns prediction intervals, so that I can have some idea of my confidence in a prediction. There seems to be four main methods of achieving this, which are summarized in the paper "Comprehensive Review of Neural Network-Based Prediction Intervals and New
Advances": <a href="https://ieeexplore.ieee.org/document/5966350" rel="nofollow noreferrer">https://ieeexplore.ieee.org/document/5966350</a></p>

<p>I am interested in the mean-variance estimation (MVE) method because it seems to be the simplest to understand. However I am struggling to get my head around exactly how this would be implemented in Keras. </p>

<p>I would guess the loss function would be defined by:</p>

<pre><code>def mve_cost(y_true, y_pred, var_pred):
  loss = 0.5*tf.reduce_sum(tf.log(var_pred) + tf.divide((tf.square(y_true - y_pred)),(tf.square(var_pred)))  )
  return loss
</code></pre>

<p>But can a loss function in Keras take three inputs? I have never seen this before. Also, the target for the variance-NN is not known beforehand and takes into account the predictions made by the mean-NN. I suppose this will need some of the more flexible capabilities of the Keras Functional API but I'm confused about how it would be put together. </p>

<ul>
<li>How do you define the loss function properly for the MVE method?</li>
<li>How can the tricky relationship between the two NNs be implemented in the Keras functional API?</li>
<li>Does anyone know of an implementation of this method already online?</li>
<li>Is there another method of generating prediction intervals for NNs that is more easily understood/implemented in Keras?</li>
</ul>