### Keras中无监督损失函数

在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中指定无监督损失函数的简单方法是我遗漏了吗?


回答:

我认为最佳解决方案是自定义训练,而不是使用model.fit方法。

完整的演练指南已发布在Tensorflow教程页面上。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注