我正在使用中性网络进行多类分类。有 3 个不平衡的类,所以我想使用焦点损失来处理不平衡。所以我使用自定义损失函数来适应 Keras 序列模型。我尝试了在网上找到的焦点损失函数的多个版本的代码,但它们返回相同的错误消息,基本上是说输入大小是预期的浴缸大小,而预期为 1。任何人都可以看看这个问题,如果可以,请告诉我修理它?对此,我真的非常感激!!!
model = build_keras_model(x_train, name='training1')
class FocalLoss(keras.losses.Loss):
def __init__(self, gamma=2., alpha=4.,
reduction = tf.keras.losses.Reduction.AUTO, name='focal_loss'):
super(FocalLoss, self).__init__(reduction=reduction,
name=name)
self.gamma = float(gamma)
self.alpha = float(alpha)
def call(self, y_true, y_pred):
epsilon = 1.e-9
y_true = tf.convert_to_tensor(y_true, tf.float32)
y_pred = tf.convert_to_tensor(y_pred, tf.float32)
model_out = tf.add(y_pred, epsilon)
ce = tf.multiply(y_true, -tf.math.log(model_out))
weight = tf.multiply(y_true, tf.pow(
tf.subtract(1., model_out), self.gamma))
fl = tf.multiply(self.alpha, tf.multiply(weight, ce))
reduced_fl = tf.reduce_max(fl, axis=1)
return tf.reduce_mean(reduced_fl)
model.compile(optimizer = tf.keras.optimizers.Adam(0.001),
loss = FocalLoss(alpha=1),
metrics=['accuracy'])
class_weight = {0: 1.,
1: 6.,
2: 6.}
# fit the model (train for 5 epochs)
history = model.fit(x=x_train, y=y_train, batch_size=64, epochs=5,
class_weight = class_weight)
ValueError: Can not squeeze dim[0], expected a dimension of 1, got 64 for 'loss/output_1_loss/weighted_loss/Squeeze' (op: 'Squeeze') with input shapes: [64].
最佳答案
您面临的问题是,您正在利用一些专为您执行某些逻辑而设计的助手类,但不幸的是,它的文档并不是很清楚它究竟为您做什么,因此,您究竟需要对您做什么自己的。
在这种情况下,您使用 tf.keras.losses.Loss
.您需要做的就是实现 call()
(以及可选的 __init__
)。不幸的是,documentation根本没有说明它的期望call()
返回。但是因为你需要指定一个 reduction
在 __init__()
,我们可以假设 call()
预计不仅返回单个数字。否则 reduction
将是无用的。换句话说:错误告诉你 call()
返回单个数字,而预计返回 64 个数字(您的批量大小)。
因此,与其自己将批次减少为单个数字(通过调用 tf.reduce_mean(reduced_fl)
),不如让助手类为您执行此操作,然后返回 reduced_f1
直接地。当前您使用 reduction=tf.keras.losses.Reduction.AUTO
这可能是你想要的。
关于keras - Keras 中的自定义损失函数(焦点损失)输入大小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58728659/