什么是“指标"?在凯拉斯? [英] What is "metrics" in Keras?

查看:35
本文介绍了什么是“指标"?在凯拉斯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还不清楚 metrics 是什么(如下面的代码所示).他们究竟在评估什么?为什么我们需要在model中定义它们?为什么我们可以在一个模型中拥有多个指标?更重要的是,这一切背后的机制是什么?任何科学参考也值得赞赏.

It is not yet clear for me what metrics are (as given in the code below). What exactly are they evaluating? Why do we need to define them in the model? Why we can have multiple metrics in one model? And more importantly what is the mechanics behind all this? Any scientific reference is also appreciated.

model.compile(loss='mean_squared_error',
              optimizer='sgd',
              metrics=['mae', 'acc'])

推荐答案

所以为了理解什么是metrics,最好先了解什么是loss函数是.神经网络大多使用梯度方法通过减少损失函数的迭代过程进行训练.

So in order to understand what metrics are, it's good to start by understanding what a loss function is. Neural networks are mostly trained using gradient methods by an iterative process of decreasing a loss function.

loss 被设计为具有两个关键属性 - 首先,它的值越小,您的模型就越适合您的数据,其次,它应该是可微的.因此,知道了这一点,我们就可以完全定义 metric 是什么:它是一个函数,根据示例中的预测值和真实值,为您提供模型适应度"的标量度量, 到您拥有的数据.因此,如您所见,loss 函数是一个指标,但反之并不总是成立.要了解这些差异,让我们看一下 metrics 用法的最常见示例:

A loss is designed to have two crucial properties - first, the smaller its value is, the better your model fits your data, and second, it should be differentiable. So, knowing this, we could fully define what a metric is: it's a function that, given predicted values and ground truth values from examples, provides you with a scalar measure of a "fitness" of your model, to the data you have. So, as you may see, a loss function is a metric, but the opposite doesn't always hold. To understand these differences, let's look at the most common examples of metrics usage:

  1. 使用不可微函数衡量网络的性能:例如准确度是不可微的(甚至不是连续的),所以你不能直接优化你的网络 w.r.t.到它.但是,您可以使用它来选择最准确的模型.

  1. Measure a performance of your network using non-differentiable functions: e.g. accuracy is not differentiable (not even continuous) so you cannot directly optimize your network w.r.t. to it. However, you could use it in order to choose the model with the best accuracy.

当您的最终损失是几个损失函数的组合时,获取不同损失函数的值:假设您的损失有一个正则化项,用于衡量您的权重与 0,以及衡量模型适应度的术语.在这种情况下,您可以使用 metrics 来单独跟踪模型的适应度如何跨时期变化.

Obtain values of different loss functions when your final loss is a combination of a few of them: Let's assume that your loss has a regularization term which measures how your weights differ from 0, and a term which measures the fitness of your model. In this case, you could use metrics in order to have a separate track of how the fitness of your model changes across epochs.

跟踪一个您不想直接优化模型的度量:所以 - 假设您正在解决一个多维回归问题,而您最关心的问题mse,但同时您对解决方案的 cosine-distance 如何随时间变化感兴趣.那么,最好使用metrics.

Track a measure with respect to which you don't want to directly optimize your model: so - let's assume that you are solving a multidimensional regression problem where you are mostly concerned about mse, but at the same time you are interested in how a cosine-distance of your solution is changing in time. Then, it's the best to use metrics.

我希望上面的解释清楚地说明了指标的用途,以及为什么可以在一个模型中使用多个指标.那么现在,让我们就它们在 keras 中的使用机制说几句.训练时有两种计算方法:

I hope that the explanation presented above made obvious what metrics are used for, and why you could use multiple metrics in one model. So now, let's say a few words about mechanics of their usage in keras. There are two ways of computing them while training:

  1. 使用编译时定义的metrics:这是您直接询问的.在这种情况下,keras 为您定义的每个指标定义一个单独的张量,以便在训练时计算它.这通常会使计算速度更快,但这需要额外的编译,并且指标应该根据 keras.backend 函数定义.

  1. Using metrics defined while compilation: this is what you directly asked. In this case, keras is defining a separate tensor for each metric you defined, to have it computed while training. This usually makes computation faster, but this comes at a cost of additional compilations, and the fact that metrics should be defined in terms of keras.backend functions.

使用 keras.callback:很高兴您可以使用 回调 以计算您的指标.由于每个回调都有一个默认属性 model,您可以在训练时使用 model.predict 或模型参数计算各种指标.此外,它使得计算它成为可能,不仅是 epoch-wise,而且是 batch-wise 或 training-wise.这样做的代价是计算速度变慢,逻辑更复杂 - 因为您需要自己定义指标.

Using keras.callback: It is nice that you can use Callbacks in order to compute your metrics. As each callback has a default attribute of model, you could compute a variety of metrics using model.predict or model parameters while training. Moreover, it makes it possible to compute it, not only epoch-wise, but also batch-wise, or training-wise. This comes at a cost of slower computations, and more complicated logic - as you need to define metrics on your own.

此处您可以找到可用指标的列表,以及有关如何定义您的指标的示例自己的.

Here you can find a list of available metrics, as well as an example on how you could define your own.

这篇关于什么是“指标"?在凯拉斯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆