gpt4 book ai didi

machine-learning - Keras 中的无监督损失函数

转载 作者:行者123 更新时间:2023-11-30 09:19:39 25 4
gpt4 key购买 nike

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 tutorials page 中.

关于machine-learning - Keras 中的无监督损失函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44761122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com