gpt4 book ai didi

python - 交叉熵 Keras 中的自定义参数

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:16 25 4
gpt4 key购买 nike

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

关于python - 交叉熵 Keras 中的自定义参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55936202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com