如何使用神经网络同时预测期望值和方差? [英] How can I predict the expected value and the variance simultaneously with a neural network?

查看:489
本文介绍了如何使用神经网络同时预测期望值和方差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用神经网络来预测标量值,该标量值是输入值和随机值(我假设是高斯分布)的函数之和,方差也取决于输入值.现在,我想拥有一个具有两个输出的神经网络-第一个输出应近似确定部分-函数,第二个输出应近似随机部分的方差,具体取决于输入值.训练这样的网络需要什么损失功能?

I'd like to use a neural network to predict a scalar value which is the sum of a function of the input values and a random value (I'm assuming gaussian distribution) whose variance also depends on the input values. Now I'd like to have a neural network that has two outputs - the first output should approximate the deterministic part - the function, and the second output should approximate the variance of the random part, depending on the input values. What loss function do I need to train such a network?

(如果有一个Tensorflow的Python示例会很好,但是我也对通用答案感兴趣.我也不太清楚如何用Python代码编写类似的东西-我都没有示例到目前为止发现的结果显示了如何处理损失函数的单个输出.)

(It would be nice if there was an example with Python for Tensorflow, but I'm also interested in general answers. I'm also not quite clear how I could write something like in Python code - none of the examples I found so far show how to address individual outputs from the loss function.)

推荐答案

由于我发现没有简单的实现方法,因此我自己写了一些模型,该模型明确地表示:

Since I've found nothing simple to implement, I wrote something myself, that models that explicitly: here is a custom loss function that tries to predict mean and variance. It seems to work but I'm not quite sure how well that works out in practice, and I'd appreciate feedback. This is my loss function:

def meanAndVariance(y_true: tf.Tensor , y_pred: tf.Tensor) -> tf.Tensor :
  """Loss function that has the values of the last axis in y_true 
  approximate the mean and variance of each value in the last axis of y_pred."""
  y_pred = tf.convert_to_tensor(y_pred)
  y_true = math_ops.cast(y_true, y_pred.dtype)
  mean = y_pred[..., 0::2]
  variance = y_pred[..., 1::2]
  res = K.square(mean - y_true) + K.square(variance - K.square(mean - y_true))
  return K.mean(res, axis=-1)

输出尺寸是标签尺寸的两倍-标签中每个值的均值和方差.损失函数由两部分组成:均方误差,其均值近似于标签值的均值;方差,其近似值与预测均值之差.

The output dimension is twice the label dimension - mean and variance of each value in the label. The loss function consists of two parts: a mean squared error that has the mean approximate the mean of the label value, and the variance that approximates the difference of the value from the predicted mean.

这篇关于如何使用神经网络同时预测期望值和方差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆