简体   繁体   English

tf.keras.model 的 add_loss 方法的文档

[英]Documentation for add_loss method of tf.keras.model

Whenever I've worked with tensorflow's keras api in the past, I've specified the loss function for a model with model.compile. Whenever I've worked with tensorflow's keras api in the past, I've specified the loss function for a model with model.compile. I'm currently working on a repo which uses 'add_loss' to specify the loss function inside of model.call.我目前正在开发一个使用“add_loss”来指定 model.call 内部的损失 function 的存储库。 Or at least, that's what I am assuming is happening, because I can't find any documentation for this method (ie none on https://www.tensorflow.org/api_docs/python/tf/keras/Model ), and I can't find any tutorials that use this method either.或者至少,这就是我假设正在发生的事情,因为我找不到这种方法的任何文档(即https://www.tensorflow.org/api_docs/python/tf/keras/Model上没有),我也找不到任何使用此方法的教程。 What's more, I can't even figure out where it's defined in the source code.更重要的是,我什至无法弄清楚它在源代码中的定义。

class TRPO(Model):
    def __init__(self, obs_dim, act_dim, hid1_mult, kl_targ, init_logvar, eta, **kwargs):
        super(TRPO, self).__init__(**kwargs)
        self.kl_targ = kl_targ
        self.eta = eta
        self.beta = self.add_weight('beta', initializer='zeros', trainable=False)
        self.policy = PolicyNN(obs_dim, act_dim, hid1_mult, init_logvar)
        self.logprob = LogProb()
        self.kl_entropy = KLEntropy()

    def call(self, inputs):
        obs, act, adv, old_means, old_logvars, old_logp = inputs
        new_means, new_logvars = self.policy(obs)
        new_logp = self.logprob([act, new_means, new_logvars])
        kl, entropy = self.kl_entropy([old_means, old_logvars,
                                       new_means, new_logvars])
        loss1 = -K.mean(adv * K.exp(new_logp - old_logp))
        loss2 = K.mean(self.beta * kl)
        # TODO - Take mean before or after hinge loss?
        loss3 = self.eta * K.square(K.maximum(0.0, K.mean(kl) - 2.0 * self.kl_targ))
        self.add_loss(loss1 + loss2 + loss3)

        return [kl, entropy]

Anyone have experience with using add_loss and can point out how it works?任何人都有使用 add_loss 的经验并且可以指出它是如何工作的? And explain why you wouldn't just write your own loss function and pass that in model.compile?并解释为什么你不只写自己的损失 function 并将其传递给 model.compile ?

You can find the official documentation of add_loss here .你可以在这里找到add_loss的官方文档。 Add loss tensor(s), potentially dependent on layer inputs.添加损失张量,可能取决于层输入。 This method can be used inside a subclassed layer or model's call function, in which case losses should be a Tensor or list of Tensors.此方法可以在子类层或模型的调用 function 中使用,在这种情况下,损失应该是张量或张量列表。 There are few example in the documentation to explain the add_loss .文档中很少有示例来解释add_loss

You can find the source code of add_loss in tf.keras.layers.Layer .您可以在add_loss中找到add_loss的源代码。 This is the class from which all layers inherit.这是所有层都继承的 class。 Click on "View source on GitHub" and search for add_loss .单击“在 GitHub 上查看源代码”并搜索add_loss

Coming to your questions -来回答你的问题 -

Anyone have experience with using add_loss and can point out how it works?任何人都有使用 add_loss 的经验并且可以指出它是如何工作的?

You can find good example using add_losshere and here with explanations.您可以在此处此处找到使用 add_loss 的好示例以及说明。

And explain why you wouldn't just write your own loss function and pass that in model.compile?并解释为什么你不只写自己的损失 function 并将其传递给 model.compile ?

model.compile() loss functions in Tensorflow always take two parameters y_true and y_pred . model.compile()损失函数始终采用两个参数y_truey_pred Using model.add_loss() has no such restriction and allows you to write much more complex losses that depend on many other tensors, but it has the inconvenience of being more dependent on the model, whereas the standard loss functions work with just any model.使用model.add_loss()没有这样的限制,并且允许您编写依赖于许多其他张量的更复杂的损失,但它有更多依赖于 model 的不便,而标准损失函数仅适用于任何 Z49D8F35E680FDC3

Hope this answers your question.希望这能回答你的问题。 Happy Learning.快乐学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM