I created a neural network for Quickest Detection. The input is a list of 10 observation and the output is the change time predicted. I want to modify the Probability of false alarms using a Weighted 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).
How can I implement it?
CodePudding user response:
You can achieve this by creating a custom loss 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. You can use it like this:
model.compile(optimizer = 'Adam', loss = 'msle')