Hello learners. I want to define a costume loss function rather than using the predefined loss function that Keras provides and want the loss function to use my input training examples X_tain besides the default inputs y_pred and y_true. To do so, I wrote a loss function that itself has an inner loss function. However, the model does not work; I receive an error in the first iteration of the fitting stage. I suspect maybe this is because of the shape or type of the X_train (it is a NumPy matrix), since the code works when I delete terms associated with X_train. I tried to change the shape and format of X_train to a tensor because I use TensorFlow as the backend but still does not work. I was wondering if you had a similar experience or have suggestions.
Here is my code:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
def costum_loss(X_loss):
def loss(y_true, y_pred):
q1 = y_pred[:, 1] + y_pred[:, 4] - X_loss [:,0]
q2 = y_pred[:, 2] + y_pred[:, 3] + y_pred[:, 7] + y_pred[:, 17] + y_pred[:, 22] - X_loss [:,1]
q3 = y_pred[:, 4] + y_pred[:, 5] + y_pred[:, 7] + y_pred[:, 8] + y_pred[:, 9] + y_pred[:, 19] - X_loss [:,2]
q4 = y_pred[:, 3] + y_pred[:, 10] + y_pred[:, 11] + y_pred[:, 13]
q5 = y_pred[:, 14]
q6 = y_pred[:, 5] + y_pred[:, 7] + y_pred[:, 10] + y_pred[:, 15] + y_pred[:, 17] + y_pred[:, 20] + y_pred[:, 22]
q7 = y_pred[:, 11] + y_pred[:, 16] + y_pred[:, 19] + y_pred[:, 21]
q8 = y_pred[:, 5] + y_pred[:, 7] + y_pred[:, 8] + y_pred[:, 20] + y_pred[:, 21] + y_pred[:, 22] + y_pred[:, 23]
loss_t = tf.reduce_mean(tf.square(y_true - y_pred)) + 1000 * tf.reduce_mean(tf.square(q1)) + \
1000 * tf.reduce_mean(tf.square(q2)) + 1000 * tf.reduce_mean(tf.square(q3)) + \
1000 * tf.reduce_mean(tf.square(q4)) + 1000 * tf.reduce_mean(tf.square(q5)) + \
1000 * tf.reduce_mean(tf.square(q6)) + 1000 * tf.reduce_mean(tf.square(q7)) + \
1000 * tf.reduce_mean(tf.square(q8))
return loss_t
return loss
model = Sequential([
tf.keras.Input(shape=(n2,)),
Dense(16, activation='relu'),
Dense(64, activation='relu'),
Dense(128, activation='relu'),
Dense(64, activation='relu'),
Dense(25, activation='relu')
], name="Model")
# Compile the model with the custom loss function
model.compile(loss = costum_loss(X_train), optimizer=tf.keras.optimizers.Adam(0.001))
# Train the model
model.fit(X_train, Y_train, epochs=5000)