Machine learning Keras中的无监督损失函数

Machine learning Keras中的无监督损失函数,machine-learning,keras,unsupervised-learning,Machine Learning,Keras,Unsupervised Learning,Keras中是否有任何方法可以指定不需要传递目标数据的损失函数 我试图指定一个丢失函数,它省略了y\u 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

Keras中是否有任何方法可以指定不需要传递目标数据的损失函数

我试图指定一个丢失函数,它省略了
y\u 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)
但似乎不传递任何目标数据会导致错误:

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'
回溯(最近一次呼叫最后一次):
文件“siamese.py”,第264行,在
模型拟合(x=[x\u序列,x\u序列扭曲,仿射],批量大小=bs,历代=1)
文件“/usr/local/lib/python2.7/dist-packages/keras/engine/training.py”,第1435行,适合
批次大小=批次大小)
文件“/usr/local/lib/python2.7/dist-packages/keras/engine/training.py”,第1322行,在用户数据中
在zip模式下(y、样本重量、类别重量、自进料重量、样本重量模式)]
文件“/usr/local/lib/python2.7/dist-packages/keras/engine/training.py”,第577行,in\u-standarized\u-weights
返回np.one((y.shape[0],),dtype=K.floatx()
AttributeError:“非类型”对象没有属性“形状”

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

编写损失函数,就像它有两个参数一样:

  • y\u true
  • y\u pred
  • 如果您没有
    y_true
    ,那很好,您不需要在内部使用它来计算损失,而是在函数原型中留下一个占位符,这样keras就不会抱怨了

    def自定义丢失(y_真,y_pred):
    #与你一起做事
    回波损耗
    

    添加自定义参数

    您可能还需要在loss函数中使用另一个参数,如
    margin
    ,即使这样,您的自定义函数也应该只接受这两个参数。但是有一个解决方法,使用lambda函数

    def自定义损失(y\u pred,保证金):
    #与你一起做事
    回波损耗
    
    但是像这样使用它

    model.compile(loss=lambda y_true,y_pred:custom_loss(y_pred,margin),…)
    
    我认为最好的解决方案是定制培训,而不是使用
    model.fit
    方法


    完整的演练发布在。

    我认为您没有抓住要点,您在无监督的情况下的损失会如何?什么是精确的计算?我试图比较两个不同的神经网络输出的相似性。它们越相似,损失就越小。更具体地说,我试图重新实现本文中描述的神经网络,我认为您应该使用虚拟数据。。。。是的…它很难看,我也不喜欢它。。。但是我看不到解决方案。第二个错误与输入/输出数据有关,您需要使用
    numpy.array
    。您可以使用
    x\u train
    作为目标。
    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'