使您的损失函数变得重要

    科技2022-07-12  131

    Some errors are more costly than others; the way your model learns should reflect that

    一些错误比其他错误代价更高。 您的模型学习方式应反映出

    George Orwell’s novella Animal Farm includes the memorable line…

    乔治·奥威尔(George Orwell)的中篇小说《动物农场》(Animal Farm)包括令人难忘的系列……

    all animals are equal, but some animals are more equal than others ¹

    所有动物都是平等的,但有些动物比其他动物更平等¹

    Orwell may have been referring to hypocrisy, power, and privilege in society, but if you replace the word animals with errors, it starts to become very relevant to machine learning.

    奥威尔(Orwell)可能一直在指社会中的虚伪,权力和特权,但是如果您用错误替换动物一词,它就开始与机器学习变得非常相关。

    Now that I’ve finished pretending to be well-read, let’s get more specific.

    既然我已经假装好了阅读能力,那么让我们更具体些。

    了解您的错误 (Get to know your errors)

    Explaining the concept of false positives and negatives is a popular interview question because they are so important when applying a classification algorithm in practice.

    解释假阳性和阴性的概念是一个普遍的访谈问题,因为在实践中应用分类算法时,它们是如此重要。

    I still sometimes find myself hastily consulting Wikipedia just before a meeting to triple-check that I’m using terms like precision and recall the right way round. For this article, you should only need to remember…

    我有时仍会发现自己在开会之前匆忙咨询Wikipedia ,以进行三次检查,以确保我使用的是诸如精确度之类的术语,并以正确的方式回想。 对于本文,您只需要记住...

    False positives are when the model predicts that a condition exists, but it does not, in reality (e.g. a fire alarm going off when there is no fire).

    误报是指模型预测存在某种情况,但实际上并不存在(例如,当没有火灾时,火警警报会响起)。

    False negatives are when the model predicts that the condition is not present, but in actual fact, it is (e.g. a smoke alarm fails to go off when there is a fire).

    假阴性是指模型预测不存在这种情况,但实际上是这样(例如,发生火灾时烟雾警报器无法发出警报)。

    With the definitions out of the way, let’s imagine that you work for a bank and your boss has asked you to build a model to identify fraudulent customers. Before any modelling takes place, it would be important to ask…

    有了这些定义,让我们假设您在一家银行工作,而老板要求您建立一个模型来识别欺诈客户。 在进行任何建模之前,务必要问……

    What happens if my model gets it wrong?

    如果我的模型弄错了怎么办?

    I don’t mean asking this from a statistical point of view, but instead, really understanding what it means for the business.

    我并不是要从统计学的角度提出这个问题,而是要真正理解它对企业的意义。

    The answer you receive will determine how you evaluate and deploy your model.

    您收到的答案将决定您如何评估和部署模型。

    Two scenarios representing how the output of a fraud-detection model could be used 两种情况表示如何使用欺诈检测模型的输出

    In Scenario 1, your model does not need to be as confident when marking a customer as fraudulent given that a more detailed human review will follow, and you would expect false positives to be filtered out by the fraud team.

    在方案1中,在将客户标记为欺诈者时,您的模型不需要那么自信,因为将进行更详细的人工审查,并且您期望欺诈团队会过滤掉误报。

    Conversely, in Scenario 2, it is a riskier outcome when a customer is flagged because they are automatically suspended. If too many false positives slip through, then you are suspending innocent customers, giving them a nasty experience, and generating bad press.

    相反,在方案2中,如果将客户标记为自动挂起客户,则结果风险较高。 如果漏报太多,那么您就是在吊销无辜的客户,给他们带来讨厌的体验,并产生不良的新闻报道。

    Identifying the sweet spot for how strict or relaxed your model should be is quite a balancing act because targeting a reduction in false positives inherently leads to an increase in false negatives and vice versa.

    确定模型应该严格或放松的最佳位置是一个平衡的举动,因为针对减少误报的目标固有地会导致误报的增加,反之亦然。

    找到合适的平衡 (Finding the right balance)

    Photo by Nick Fewings on Unsplash Nick Fewings在 Unsplash上 拍摄的照片

    Given the trade-off between false positive and negative rates, a model’s output can be used more creatively.

    给定假正率和负率之间的权衡,可以更富有创意地使用模型的输出。

    Binary classification models often classify anything that has over 50% probability, but if you are concerned with the false positive rate being too high, you could use a higher threshold e.g. only classify fraudulent customers if their predicted probability exceeds 80%.

    二进制分类模型通常对概率超过50%的任何事物进行分类,但是如果您担心误报率过高,则可以使用较高的阈值,例如,仅当欺诈客户的预测概率超过80%时才对其分类。

    Or the business could follow a hybrid approach between Scenario 1 and 2, where customers with a predicted probability of 80% or above are automatically suspended, and anyone between 50% and 80% is shared with the fraud team for more rigorous review.

    或者,企业可以采用方案1与方案2之间的混合方法 ,其中自动将具有80%或更高的预测概率的客户暂停,并将50%至80%之间的任何人与欺诈团队共享以进行更严格的审查。

    This kind of strategy often works well enough, but ultimately they involve training a machine learning algorithm that assumes all types of error are equally important and then massaging its output to correct that assumption.

    这种策略通常效果很好,但是最终它们涉及到训练一种机器学习算法,该算法假设所有类型的错误都同样重要,然后按摩其输出以纠正该假设。

    Fortunately, it is possible to directly influence how a variety of machine learning algorithms learn, and place more importance on a specific error type. Some of the techniques for handling class imbalance can also serve a similar purpose of targeting false positives or negatives, but this article focuses on achieving this goal via the loss function.

    幸运的是,有可能直接影响各种机器学习算法的学习方式,并更加重视特定的错误类型。 处理类别失衡的某些技术也可以达到针对假阳性或阴性的类似目的,但本文着重于通过损失函数实现这一目标。

    蒙受损失 (Coming to terms with loss)

    Photo by Tom Pumford on Unsplash Tom Pumford在 Unsplash上 拍摄的照片

    When it comes to deep learning, the loss function determines what the algorithm is trying to minimise as it learns and iteratively improves. Loss functions often default to mean squared error (regression) and cross-entropy loss (classification), but there is plenty of funkier options which can work better in certain use cases.

    当涉及深度学习时,损失函数将确定算法在学习和迭代式改进时要尽量减少的内容。 损失函数通常默认为均方误差(回归)和交叉熵损失(分类),但是有很多funkier选项可以在某些用例中更好地发挥作用。

    Regardless of the function itself, however, they all share a purpose in that they provide a feedback mechanism for the model to check how well it is performing, find the gradient of the loss function, and use that gradient to update the network’s weights to reduce loss as quickly as possible.

    但是,无论函数本身如何,它们都有一个共同的目的,即为模型提供一种反馈机制,以检查模型的性能如何,找到损失函数的梯度,并使用该梯度来更新网络权重以降低尽快损失 。

    Even though we’re now talking about deep learning and much more complex and powerful models, they would still fail to distinguish between the type of error they are committing.

    即使我们现在谈论的是深度学习和更复杂,更强大的模型,它们仍然无法区分所犯的错误类型。

    Fortunately, TensorFlow allows you to take control of this.

    幸运的是,TensorFlow允许您对此进行控制。

    适应机器学习 (Getting machine-learning fit)

    Just before we get to the code, let’s take a new scenario (I promise this is the last one).

    在开始编写代码之前,让我们采用一个新方案(我保证这是最后一个方案)。

    You are working for a leisure centre where customers pay a monthly subscription, but they have the option of changing which price tier they subscribe to at the end of each month.

    您正在一个休闲中心工作,客户每月支付订阅费,但是他们可以选择在每个月底更改其订阅的价格等级。

    Gym membership, monthly price plans 健身房会员资格,每月价格计划

    As with most businesses, it’s helpful to know whether customers are spending more or less over time, and forecast what they are likely to do in the future.

    与大多数企业一样,了解客户随着时间的推移会花费更多还是更少,并预测他们将来可能会做什么会很有帮助。

    To offer that capability, you are asked to build a model which can learn from a customer’s characteristics and predict whether they will trade up, down, or stay on the same price plan.

    为了提供这种功能,要求您构建一个模型,该模型可以从客户的特征中学习并预测他们是要升,降还是维持相同的价格计划。

    By taking a snapshot of past behaviour i.e. whether a customer was using the gym more or less vs the last month and how long they had spent on the same plan, a model could be trained to predict one of three classes for what the customer will do next month:

    通过对过去的行为进行快照,即客户与上个月相比是否使用健身房,以及他们在同一计划上花费了多长时间,可以训练一个模型来预测客户将要做什么的三个类别中的一个下个月:

    Spend more and trade Up

    花更多钱并换购

    Stay on the same plan and remain Stable

    保持相同的计划并保持稳定

    Spend less and trade Down

    少花钱, 降格以求

    Features and the target variable 功能和目标变量

    Naturally, you would like the model to be as accurate as possible across the board. Yet the model’s output might be used by the Finance Team to determine how much revenue will be coming in and how much they can afford to reinvest.

    自然,您希望模型在各个方面都尽可能准确。 但是,财务团队可能会使用模型的输出来确定收入多少以及他们有能力负担再投资。

    In this context, it is far more damaging to overestimate how many customers will trade-up, as the business will be expecting more revenue than it achieves. Whilst it isn’t great to overestimate customers trading down, at least it leaves the Finance Team in a nicer position of having more money coming in than expected.

    在这种情况下,高估要交换多少客户的损害更大,因为企业期望获得的收益超过其实现的收益。 虽然高估客户的交易量不是很好,但是至少这会使财务团队处于更好的位置,可以收到比预期更多的资金。

    The risk associated with when the model gets it right or wrong 模型正确与错误相关的风险

    Armed with this commercial context, you can directly manipulate how the model trains to reflect the cautiousness the Finance Team desires.

    有了这种商业环境,您就可以直接操纵模型的训练方式,以反映财务团队所希望的谨慎性。

    将TensorFlow弯曲到您的意愿 (Bending TensorFlow to your will)

    Photo by Jonathan Bean on Unsplash 乔纳森·比恩 ( Jonathan Bean) 摄于 Unsplash

    We shall use TensorFlow and the Keras API to demonstrate how this can be achieved. If you wish to follow along and execute the code yourself, you can follow the instructions here.

    我们将使用TensorFlow和Keras API演示如何实现。 如果您希望自己继续执行并执行代码,则可以按照此处的说明进行操作。

    Have a read of the code below to get a taste of Tensorflow’s flexibility.

    阅读以下代码,以品尝Tensorflow的灵活性。

    永远不会忘记 (Never forget)

    Hopefully, this has helped demonstrate the situations in which you may want a custom loss function, and how you go about implementing it.

    希望这有助于说明您可能需要自定义损失函数的情况,以及如何实现它。

    But more importantly, never forget the key question…

    但更重要的是,永远不要忘记关键问题……

    What happens if my model gets it wrong?

    如果我的模型弄错了怎么办?

    Keep this top of mind and you will transform yourself from a Data Scientist who works well in theory to one who works well in practice.

    紧记这一点,您将把自己从理论上出色的数据科学家转变为实践上出色的数据科学家。

    参考资料 (REFERENCES)

    [1] G. Orwell. Animal farm: A fairy story (2018). London: Penguin Books.

    [1] G.奥威尔。 动物农场:童话故事(2018)。 伦敦:企鹅图书。

    翻译自: https://medium.com/@kieran_95138/making-your-loss-function-count-8659cf65feb1

    Processed: 0.008, SQL: 8