After devoting good time, finally I found how to implement the solution in keras/tensorflow library with the regard to previous useful answers to my question.
First if we want to implement a costume keras loss function with some parameters and also accessing to inputs we have to define:
def custom_loss(alpha):
def loss(data, y_pred):
y_true = tf.reshape(data[:, 0], (-1, 1))
input = tf.reshape(data[:, 1], (-1, 1))
diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true), K.epsilon(),
None))
return 100. * K.mean(diff, axis=-1) +
K.mean(alpha*tf.keras.activations.relu(input - y_pred))
here I padded inputs into the right side of the output tensor and then inside the function I repacked it to access inputs. here I used mean absolute percentage error as the base loss function and then added desired condition with the aid of alpha parameter as regularization parameter and the Relu function. be aware of using right column of your input data in this function
then if we want to build neural network model the following codes has to be used.
first we pad input to our output easily as follow:
output_train = np.append(y_train, x_train, axis =1)
output_valid = np.append(y_valid, x_valid, axis =1)
in the compile function:
model.compile(loss = custom_loss(alpha=10000))
here I used 10000 as the alpha and it's obvious that can be changed based on the case.
now we can fit model on our data. but there is another problem when we want to load the saved model. if we want to load the model we have to use the following code
model = keras.models.load_model(model_save_address, custom_objects={'loss': custom_loss(10000)})
now everything is fine and we can run our model and train and test it easily on our data.
NOTE: First I thank all people helped me solve the issue. I think its worth noting that before solving the issue despite having the good model that performs well with very low error on my data, in the 50% of the cases my desired condition had been violating and that was a problem for me. but after implementing this solution, only in 0.5% cases the condition wont be satisfied and I hope to find another solution to reduce it furthermore.