我需要构建自定义的分类交叉熵损失函数,在这里我应该比较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)

10-08 00:30