简体   繁体   English

TensorFlow 中的加权均方误差

[英]Weighted Mean Squared Error in TensorFlow

I created a neural.network for Quickest Detection.我为最快检测创建了一个神经网络。 The input is a list of 10 observation and the output is the change time predicted.输入是一个包含 10 个观察值的列表,output 是预测的变化时间。 I want to modify the Probability of false alarms using a Weighted MSE.我想使用加权 MSE 修改误报概率。

I created this neural.network:我创建了这个神经网络:

model = k.Sequential(\[
k.layers.Dense(window_size, activation = k.activations.relu, input_shape=\[window_size\]),
k.layers.Dense(window_size, activation = k.activations.relu),
k.layers.Dense(window_size, activation = k.activations.relu),
k.layers.Dense(window_size, activation = k.activations.relu),
k.layers.Dense(1, activation = k.activations.relu),
\])
model.compile(optimizer = 'Adam', loss = 'mse')

\#training
history = model.fit(x = X, y=y, epochs = 50)

I have to modify this model introducing a weighted MSE that prevent False Alarms (a false alarms occurs when value predicted - true label < 0).我必须修改此 model,引入加权 MSE 以防止误报(当预测值出现误报 - 真 label < 0)。

How can I implement it?我该如何实施?

You can achieve this by creating a custom loss function:您可以通过创建自定义损失 function 来实现:

  def custom_loss(y_true, y_pred):
      loss = k.mean(k.square(y_true - y_pred), axis=-1) # MSE
      loss = k.where((y_pred - y_true) < 0.0, loss, loss * 0.5) # higher loss for false alarms
      return loss
  model.compile(optimizer = 'Adam', loss = custom_loss)

However, I would recommend using a different loss function. For example, you could use the MSLE (Mean Squared Logarithmic Error) loss function. This loss function is penalized more for underestimating, which is what you want to achieve, because its exactly the case when predicted smaller than true value.但是,我建议使用不同的损失 function。例如,您可以使用 MSLE(均方对数误差)损失 function。损失 function 因低估而受到更多惩罚,这正是您想要实现的,因为它正是这种情况当预测值小于真实值时。 You can use it like this:你可以像这样使用它:

  model.compile(optimizer = 'Adam', loss = 'msle')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM