Search code examples
pythonkeraslosscross-entropy

Custom parameters in cross-entropy Keras


I need to build custom categorical cross entropy loss function where I should compare y_true and Q*y_pred instead of just y_pred. Q is a matrix. The problem is that the batch size must not be equal to 1. So, there is a problem with dimensions. How to built categorical cross entropy loss function which works with batch_size=200?

For example, this is the custom categorical cross entropy loss function which works correctly but for batch_size = 1. I have 3 classes, so, the shape of y_pred is (batch_size, 3, 1) and the shape of Q is (3,3).
I also tried to transfer a multidimensional numpy array with shape = (batch_size, 3, 3) but it did not work.

Q=np.matrix([[0, 0.7,0.2], [0,0,0.8],[1,0.3,0]])

def alpha_loss(y_true, y_pred):         
    return K.categorical_crossentropy(y_true,K.dot(tf.convert_to_tensor(Q,dtype=tf.float32 ),K.reshape(y_pred,(3,1)) ))


Solution

  • Since you are using TensorFlow back end, this may work:

    Q=np.matrix([[0, 0.7,0.2], [0,0,0.8],[1,0.3,0]])
    
    def alpha_loss(y_true, y_pred):
       # Edit: from the comments below it appears that y_pred has dim (batch_size, 3), so reshape it to have (batch_size, 3, 1)
       y_pred = tf.expand_dims(y_pred, axis=-1)
    
       q_tf = tf.convert_to_tensor(Q,dtype=tf.float32)
    
       # Changing the shape of Q from (3,3) to (batch_size, 3, 3)
       q_expanded = tf.tile(tf.expand_dims(q_tf, axis=0), multiples=[tf.shape(y_pred)[0], 1,1])
    
       # Calculate the matrix multiplication of Q and y_pred, gives a tensor of shape (batch_size, 3, 1)
       qy_pred = tf.matmul(q_expanded, y_pred)
    
       return K.categorical_crossentropy(y_true, qy_pred)