psylos
psylos

Reputation: 19

Custom loss function keras for both numerical and categorical variables

I have created an autoencoder model with a dataset having 5 features.
From these features, the first 3 are numerical and the last 2 binary categorical. What I would like to do is create a custom loss function that takes into account both these types of data. What I have tried:

def custom_loss(y_true, y_pred):
  return tf.math.add(
      tf.keras.metrics.mean_squared_error(y_true[:,0:3], y_pred[:,0:3]) + 
      tf.keras.metrics.BinaryCrossentropy(y_true[:,3:5], y_pred[:,3:5])
      )

However, this gets me this error:

OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.

this also does not work without tf.math.add().

The custom loss is given here:

autoencoder.compile(optimizer='adam', loss = custom_loss)

How could this be implemented?

Upvotes: 1

Views: 179

Answers (1)

Innat
Innat

Reputation: 17229

There is an issue in your loss function implementation. Try as follows

def custom_loss(y_true, y_pred):
  return tf.math.add(
      tf.keras.losses.mean_squared_error(y_true[:,0:3], y_pred[:,0:3]), 
      tf.keras.losses.binary_crossentropy(y_true[:,0:3], y_pred[:,0:3])
  )

Note, BinaryCrossentropy is class and binary_crossentropy is function. Here is a dummy example

# data 
img = tf.random.normal([20, 32], 0, 1, tf.float32)
tar = np.random.randint(2, size=(20, 1))

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

def custom_loss(y_true, y_pred):
  return tf.math.add(
      tf.keras.losses.mean_squared_error(y_true[:,0:3], y_pred[:,0:3]), 
      tf.keras.losses.binary_crossentropy(y_true[:,0:3], y_pred[:,0:3])
  )

model.compile(loss=custom_loss, 
              optimizer='adam', metrics=['accuracy'])
model.fit(img, tar, epochs=2, verbose=2)
Epoch 1/2
1/1 - 1s - loss: 1.4021 - accuracy: 0.4000 - 550ms/epoch - 550ms/step
Epoch 2/2
1/1 - 0s - loss: 1.3967 - accuracy: 0.4500 - 8ms/epoch - 8ms/step
<keras.callbacks.History at 0x7f9c40880790>

Upvotes: 2

Related Questions