Neural Networks – How to Impose a Condition on a Neural Network

kerasneural networkstensorflow

I am building a neural network model with TensorFlow and Keras in python. My model is performing well on unseen data in the way I desire and everything is fine. but the problem that I don't have any idea how to implement a solution for it is this:
consider my neural network has the input like this

Input = [i1, i2, i3, i4, i5]

and the output of the network is only single value and we call it

Output = O

I want that the output of the neural network be greater than specific input value. here for example I want that O > i3. despite the very good performance of my neural network on Test Data (unseen data) but in some cases the mentioned condition will be violated and this is a problem for me.

Best Answer

A dirt-simple solution is to add a regularization term, so your loss function is $\text{loss} + \lambda \text{ReLU} (i_3 - O)$. This adds a penalty whenever your inequality is violated, so the model will tend to respect the constraint.

While this solution is inexact, It will be more challenging to solve this exactly because constrained optimization is not something NN libraries are designed for.

Some related solutions:

Loss function in machine learning - how to constrain?

Related Question