交叉熵Keras中的自定义参数

问题描述 投票:1回答:1

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

python keras loss cross-entropy
1个回答
0
投票

由于您使用的是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)
© www.soinside.com 2019 - 2024. All rights reserved.