Home > database >  custom metric function with additional parameter
custom metric function with additional parameter

Time:03-12

def custom_metric(y_prem):
   def score_func(y_true, y_pred):
      diff = y_pred - y_true
      return tf.reduce_sum(diff[y_prem>=y_pred])
   return score_func

model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(32, input_shape=[len(X_train[0, :])], activation='tanh'),
    tf.keras.layers.Dense(8, input_shape=[len(X_train[0, :])], activation='linear'),
    tf.keras.layers.Dense(4, input_shape=[len(X_train[0, :])], activation='tanh'),
    tf.keras.layers.Dense(1, activation='relu'),
])
model.compile(optimizer='adam', loss='mean_squared_error', metrics=[custom_metric(y_prem)])
model.summary()
model.fit(X_train_minmax, y_train, epochs=30, batch_size=len(y_train))

y_prem and y_train are both the same size(50646)

I have tried to define this custom metric function where y_prem is a vector in the size of the prediction. I want to sum the diff between the pred and the true only on the indexes where the pred is lower than y_prem but when I trained the model I received an error message:

File "C:/Users/zehavi kelman/PycharmProjects/Accident_predicting/simpego_test.py", line 61, in score_func  *
        return K.sum(diff[y_prem>=y_pred])

    ValueError: Shapes (50646, 1) and (50646, 50646) are incompatible

How can I fix that?

CodePudding user response:

I am not sure of what you want to do but I implemented a reproducible example that do not output an error message (pay attention to the x and y shapes):

import tensorflow as tf

x = tf.random.uniform(shape=[50646, 5], minval=0, maxval=1)
y = tf.random.uniform(shape=[50646, 1], minval=0, maxval=1)
y_prem = tf.random.uniform(shape=[50646, 1], minval=0, maxval=1)


def custom_metric(y_prem):
    def score_func(y_true, y_pred):
        diff = y_pred - y_true
        return tf.reduce_sum(diff[y_prem>=y_pred])
    return score_func

model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(32, input_shape=[len(x[0, :])], activation='tanh'),
    tf.keras.layers.Dense(8, activation='linear'),
    tf.keras.layers.Dense(4, activation='tanh'),
    tf.keras.layers.Dense(1, activation='relu'),
])
model.compile(optimizer='adam', loss='mean_squared_error', metrics=[custom_metric(y_prem)])
model.summary()
model.fit(x, y, epochs=30, batch_size=len(y))
  • Related