Keras自定义损失函数

Keras自定义损失函数

本文介绍了Keras自定义损失函数:形状为batch_size(y_true)的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Keras中实现自定义损失函数时,我需要一个tf.Variable,其形状为我的输入数据(y_true, y_pred)的批处理大小.

When implementing a custom loss function in Keras, I require a tf.Variable with the shape of the batch size of my input data (y_true, y_pred).

def custom_loss(y_true, y_pred):

    counter = tf.Variable(tf.zeros(K.shape(y_true)[0], dtype=tf.float32))
    ...

但是,这会产生错误:

You must feed a value for placeholder tensor 'dense_17_target' with dtype float and shape [?,?]

如果我将batch_size固定为一个值:

If I fix the batch_size to a value:

def custom_loss(y_true, y_pred):

    counter = tf.Variable(tf.zeros(batch_size, dtype=tf.float32))
    ...

,使得|training_set| % batch_size|val_set| % batch_size等于零,一切正常.

such that |training_set| % batch_size and |val_set| % batch_size are equal to zero, everything works fine.

有什么建议,为什么基于输入形状(y_truey_pred)的批次大小的变量分配不起作用?

Are there any suggestions, why the assignment of the variable with batch size based on the shape of the input (y_true and y_pred) does not work?

解决方案

我找到了令人满意的解决方案.我以最大的batch_size可能值初始化了变量(在模型构建期间指定),并且仅将K.shape(y_true)[0]用于切片变量.这样,它可以完美地工作.这里的代码:

I found a satisfying solution that works.I initialized the variable with the max batch_size possible (specified during model build time) and used the K.shape(y_true)[0] only for slicing the variable. That way it works perfectly. Here the code:

def custom_loss(y_true, y_pred):
    counter = tf.Variable(tf.zeros(batch_size, dtype=tf.float32))
    ...
    true_counter = counter[:K.shape(y_true)[0]]
    ...

推荐答案

它不起作用,因为K.shape返回的符号形状是张量本身,而不是int值的元组.要从张量获取值,您必须在会话中对其进行评估.有关此信息,请参见文档.要在评估之前获得真实价值,请使用K.int_shape: https://keras.io/backend/#int_shape

It does not work because K.shape returns you a symbolic shape, which is a tensor itself, not a tuple of int values. To get the value from a tensor, you have to evaluate it under a session. See documentation for this. To get a real value prior to evaluation time, use K.int_shape: https://keras.io/backend/#int_shape

但是,K.int_shape在这里也不起作用,因为它只是一些静态元数据,通常不会反映当前的批处理大小,但是具有占位符值None.

However, K.int_shape also not gonna work here, as it is just some static metadata and won't normally reflect the current batch size, but has a placeholder value None.

您找到的解决方案(可以控制批次大小并在损失范围内使用它)确实是一个很好的解决方案.

The solution you found (have a control over the batch size and use it inside the loss) is indeed a good one.

我相信问题是因为您需要在定义时知道批量大小以构建变量,但是只有在会话运行时才能知道.

I believe the problem is because you need to know the batch size at the definition time to build the Variable, but it will be known only during the session run time.

如果您像使用张量一样使用它,应该没问题,请参见示例.

If you were working with it as with a tensor, it should be ok, see this example.

这篇关于Keras自定义损失函数:形状为batch_size(y_true)的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 20:33