交叉熵 Keras 中的自定义参数

Custom parameters in cross-entropy Keras

我需要构建自定义分类交叉熵损失函数,我应该在其中比较 y_trueQ*y_pred 而不仅仅是 y_predQ 是一个矩阵。 问题是批量大小不能等于 1。所以,维度有问题。 如何构建适用于 batch_size=200?

的分类交叉熵损失函数

例如,这是自定义分类交叉熵损失函数,它可以正常工作,但适用于 batch_size = 1。 我有 3 个 类,所以 y_pred 的形状是 (batch_size, 3, 1)Q 的形状是 (3,3).
我还尝试使用 shape = (batch_size, 3, 3) 传输多维 numpy 数组,但它没有用。

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)) ))

由于您使用的是 TensorFlow 后端,这可能有效:

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)