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

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

问题描述

我还不清楚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函数是什么.通过减少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.

A 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. 使用不可微分函数来衡量网络的性能:例如准确性是无与伦比的(甚至不是连续的),因此您无法直接优化您的网络做到这一点.但是,您可以使用它来选择精度最高的模型.

  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.

当您的最终损失是其中一些的组合时,获得不同损失函数的值:让我们假设您的损失具有一个正则化项,用于衡量您的权重与,以及用于衡量模型适用性的术语.在这种情况下,您可以使用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或模型参数来计算各种指标.此外,它不仅可以以时间为单位进行计算,还可以以批次为单位进行计算或以训练为单位进行计算.这样做的代价是计算速度较慢,逻辑更加复杂-因为您需要自己定义指标.

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.

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

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