Keras中的无监督损失函数

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

Keras中是否可以指定不需要传递目标数据的损失函数?

我试图指定一个损失函数,它像这样省略了y_true参数:

def custom_loss(y_pred):

但是我收到以下错误:

Traceback (most recent call last):
  File "siamese.py", line 234, in <module>
    model.compile(loss=custom_loss,optimizer=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0))
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 911, in compile
    sample_weight, mask)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 436, in weighted
    score_array = fn(y_true, y_pred)
TypeError: custom_loss() takes exactly 1 argument (2 given)

然后我尝试在不指定任何目标数据的情况下调用fit()

 model.fit(x=[x_train,x_train_warped, affines], batch_size = bs, epochs=1)

但是似乎没有传递任何目标数据会导致错误:

Traceback (most recent call last):
  File "siamese.py", line 264, in <module>
    model.fit(x=[x_train,x_train_warped, affines], batch_size = bs, epochs=1)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1435, in fit
    batch_size=batch_size)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1322, in _standardize_user_data
    in zip(y, sample_weights, class_weights, self._feed_sample_weight_modes)]
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 577, in _standardize_weights
    return np.ones((y.shape[0],), dtype=K.floatx())
AttributeError: 'NoneType' object has no attribute 'shape'

我可以手动创建与神经网络输出相同形状的伪数据,但这看起来非常混乱。有没有一种简单的方法可以指定我所缺少的Keras中的无监督损失函数?

machine-learning keras unsupervised-learning
1个回答
0
投票

将损失函数编写为好像有两个参数:

  1. y_true
  2. y_pred

如果没有y_true,那很好,您不需要在内部使用它来计算损失,而是在函数原型中保留一个占位符,因此keras不会抱怨。

def custom_loss(y_true, y_pred):
    # do things with y_pred
    return loss

添加自定义参数

您可能还需要在损失函数内使用另一个参数,例如margin,即使您的自定义函数只应接受这两个参数。但是有一种解决方法,请使用lambda函数

def custom_loss(y_pred, margin):
    # do things with y_pred
    return loss

但使用起来却像

model.compile(loss=lambda y_true, y_pred: custom_loss(y_pred, margin), ...)
© www.soinside.com 2019 - 2024. All rights reserved.