最大化keras mod的MSE

2024-10-01 02:27:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个生成的对抗网络,在这种网络中,鉴频器被MSE最小化,生成器应该得到最大化。因为他们都是追求相反目标的对手。

generator = Sequential()
generator.add(Dense(units=50, activation='sigmoid', input_shape=(15,)))
generator.add(Dense(units=1, activation='sigmoid'))
generator.compile(loss='mse', optimizer='adam')

generator.train_on_batch(x_data, y_data)

我需要适应什么,才能得到一个从高MSE值中获利的发电机模型?


Tags: 网络add目标inputdatageneratoractivationdense
2条回答

这个问题我不太清楚。我想你想最大化而不是最小化,同时使用最小均方误差准则。在

您可以实现自己的自定义损耗函数,该函数计算-MSE;翻转损耗符号,从而实现梯度下降方向的翻转。在

def negative_mse(y,yhat): 
    return - K.mean(K.sum(K.square(y-yhat)))

model.compile(loss=negative_mse, optimizer='adam')

另一个选择是简单地提供一个消极的学习步骤-但我不确定Keras是否允许您这样做。值得一试。在

更新:

原始的MSE实现如下所示:

def mean_squared_error(y_true, y_pred):
    if not K.is_tensor(y_pred):
        y_pred = K.constant(y_pred)
    y_true = K.cast(y_true, y_pred.dtype)
    return K.mean(K.square(y_pred - y_true), axis=-1)

我认为正确的最大化损失函数:

^{pr2}$

这样我们总是得到一个正的损耗值,就像MSE函数的情况一样,但是有相反的效果。在

更新2: 最初我写道,由于优化方法的基本概念(您可以阅读一篇有趣的讨论here),简单地否定损失的直觉第一思想将而不是给出我们预期的结果(您可以阅读一篇有趣的讨论here)。 在我仔细检查了两种方法在特定学习任务中的结果后(注意:我没有做全面测试),两种方法都给出了损失最大化,尽管-loss方法收敛得更快。我不确定它是否总是给出最佳解决方案或任何解决方案,因为所描述的可能问题是here。 如果有人有其他经验,请告诉我。在

因此,如果有人想尝试一下-loss也:

^{3}$


其他详细信息:

作者写道:

I have a generative adversarial networks, where the discriminator gets minimized with the MSE and the generator should get maximized. Because both are opponents who pursue the opposite goal.

来自Ibragil提供的链接:

Meanwhile, the generator is creating new, synthetic images that it passes to the discriminator. It does so in the hopes that they, too, will be deemed authentic, even though they are fake. The goal of the generator is to generate passable hand-written digits: to lie without being caught. The goal of the discriminator is to identify images coming from the generator as fake.


所以这是一个不适定问题:

GAN中,我们的最终目标是训练我们的两个对手,即鉴别器发生器尽可能地相互竞争。这意味着,两种基学习算法的任务不同,但获得最优解的损失函数是相同的,即binary_crossentropy,因此模型的任务是使损失最小化。在

A鉴别器模型的编译方法:

self.discriminator.compile(loss='binary_crossentropy', optimizer=optimizer)

A生成器模型的编译方法:

self.generator.compile(loss='binary_crossentropy', optimizer=optimizer)

这就像两个跑步者的目标一样,尽可能缩短到达终点的时间,即使他们是这项任务的竞争对手。在

所以“相反的目标”并不意味着相反的任务,即最小化损失(即在跑步者示例中最小化时间)。

我希望有帮助。在

相关问题 更多 >