Answers.Body: <p>Common loss functions, like the cross-entropy or mean squared error, are chosen because, <strong>if you minimize them, you are actually maximizing the likelihood of the parameters given the observed data</strong>. In other words, you are trying to find the probability distribution <span class="math-container">$p(y \mid x; \theta)$</span>, parametrized by <span class="math-container">$\theta$</span> (the parameters of your model), which is most consistent with your data <span class="math-container">$D = \{(x_i, y_i) \}_{i=1}^N$</span>. In the case of binary classification problems, for instance, you assume that <span class="math-container">$p(y \mid x; \theta)$</span> is a Bernoulli distribution or a family of Bernoulli distributions (i.e. the likelihood) parametrized by <span class="math-container">$\theta$</span> (see more details <a href="https://ai.stackexchange.com/a/33956/2444">here</a>). Similarly, in the case of regression problems, you can assume that <span class="math-container">$p(y \mid x; \theta)$</span> is a Gaussian. There are other examples of loss functions that are mathematically motivated, for instance, the ELBO or the GAN objective function. The same can be said, for example, for loss functions with regularisation, like the MSE with weight decay. These are mathematically motivated (in this case, for instance, you can derive the MSE with weight decay as a MAP estimate rather than an MLE).</p>
  <p>So, loss functions are not chosen arbitrarily, or we don't choose the binary cross-entropy for binary classification problems just because everyone else does (well, ideally, this shouldn't be the reason, but I guess many people don't know the details that I've just mentioned above). We often model our problem with <strong>probability theory</strong> and we try to <strong>find a probability distribution</strong> (or the mean/mode or even variance of such a probability distribution) that most likely generated our data.</p>
  <p>Now, if you train a neural network with gradient descent and backpropagation, it's assumed that you can also find the derivative of your loss function with respect to the parameters of your model. So, this is one of the properties that a loss function is expected to have in this training regime, i.e. it needs to be <a href="https://ai.stackexchange.com/q/28877/2444"><strong>differentiable</strong></a>.</p>
  <p>One of the most commonly used metrics to assess the quality of a classifier is the <strong>accuracy</strong>, which I think is not differentiable (although I don't have the proof and I didn't think too much about it). So, that's probably one of the reasons why people wouldn't use the accuracy as a loss function anyway. Now, keep in mind that the accuracy can actually be a very bad metric to assess the quality of your model because, if your dataset is unbalanced (and you didn't check that and the true distribution is not), your classifier could simply always predict the most common class and get a high accuracy, but this would be misleading.</p>
  <p>So, in summary, a function is <em>often</em> used as a loss function (in the context of training neural networks with GD) if</p>
  <ol>
  <li>it is differentiable, and,</li>
  <li>if you minimize it, you're actually trying to learn a probability distribution</li>
  </ol>
  <p>Note that the second property may not always be a requirement, but all the loss functions that I can think of right now have these two properties.</p>
  <p>On the other hand, a function is used as a metric if you care about what that specific function evaluates. For example, if you use the accuracy, you want to know, on average, how many mistakes the model makes, but you don't care whether it makes more mistakes for one class than the other. However, you can also use other functions, like precision and recall, which evaluate other behaviors, like how many mistakes you make for one class rather than the other. This can make sense, for example, in the context of detecting a disease.</p>
  <p>In principle, you could use a loss function also as a metric. However, is that really what you want? For instance, if you used the MSE as a metric, it's not clear how small it must be in order to have a &quot;good&quot; model. It all depends on which behavior of the model that you want to evaluate. You can evaluate different behaviors of the model, so that's why you often have more than one metric.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-05T22:48:39.150Z
Answers.LastActivityDate: 2022-01-05T22:48:39.150Z
Answers.UserDisplayName: nbro
Body: <p>A neural network model needs a loss function for training. The neural network needs to minimize the loss function.</p>
  <p>A neural network is evaluated after training using a metric. The neural network needs to either minimize or maximize the metric depending on the context.</p>
  <p>Suppose <span class="math-container">$L$</span> is the loss function used and <span class="math-container">$M$</span> is the metric/evaluation function. Assume the metric needs to be minimized and is calculated based on the output of the neural network. We can use <span class="math-container">$L+M$</span> as the loss function. It looks to me that it may be up to the choice of the designer to use certain function <span class="math-container">$f$</span> for either <span class="math-container">$L$</span> or <span class="math-container">$M$</span> as they try to quantify how good/bad the model is working.</p>
  <p>But, in the literature, if we observe, there are some fixed loss functions and fixed metrics for evaluation depending on the underlying task.</p>
  <p>With this context, what is the inherent quality of the function that makes it treated as either loss or evaluation metric?</p>
  
Tags: <neural-networks><objective-functions><metric><evaluation-functions>
Title: What inherent quality of a function makes it treated as either loss or evaluation metric?
UserDisplayName: hanugm
ThreadText: # What inherent quality of a function makes it treated as either loss or evaluation metric?
  
  (Asked by: username_0 on 2021-12-29)
  
  A neural network model needs a loss function for training. The neural network needs to minimize the loss function.
  
  A neural network is evaluated after training using a metric. The neural network needs to either minimize or maximize the metric depending on the context.
  
  Suppose $L$ is the loss function used and $M$ is the metric/evaluation function. Assume the metric needs to be minimized and is calculated based on the output of the neural network. We can use $L+M$ as the loss function. It looks to me that it may be up to the choice of the designer to use certain function $f$ for either $L$ or $M$ as they try to quantify how good/bad the model is working.
  
  But, in the literature, if we observe, there are some fixed loss functions and fixed metrics for evaluation depending on the underlying task.
  
  With this context, what is the inherent quality of the function that makes it treated as either loss or evaluation metric?
  
  **Tags:** neural-networks, objective-functions, metric, evaluation-functions
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-01-05. Score: 1
  
  Common loss functions, like the cross-entropy or mean squared error, are chosen because, **if you minimize them, you are actually maximizing the likelihood of the parameters given the observed data**. In other words, you are trying to find the probability distribution $p(y \mid x; \theta)$, parametrized by $\theta$ (the parameters of your model), which is most consistent with your data $D = \{(x\_i, y\_i) \}\_{i=1}^N$. In the case of binary classification problems, for instance, you assume that $p(y \mid x; \theta)$ is a Bernoulli distribution or a family of Bernoulli distributions (i.e. the likelihood) parametrized by $\theta$ (see more details [here](https://ai.stackexchange.com/a/33956/2444)). Similarly, in the case of regression problems, you can assume that $p(y \mid x; \theta)$ is a Gaussian. There are other examples of loss functions that are mathematically motivated, for instance, the ELBO or the GAN objective function. The same can be said, for example, for loss functions with regularisation, like the MSE with weight decay. These are mathematically motivated (in this case, for instance, you can derive the MSE with weight decay as a MAP estimate rather than an MLE).
  
  So, loss functions are not chosen arbitrarily, or we don't choose the binary cross-entropy for binary classification problems just because everyone else does (well, ideally, this shouldn't be the reason, but I guess many people don't know the details that I've just mentioned above). We often model our problem with **probability theory** and we try to **find a probability distribution** (or the mean/mode or even variance of such a probability distribution) that most likely generated our data.
  
  Now, if you train a neural network with gradient descent and backpropagation, it's assumed that you can also find the derivative of your loss function with respect to the parameters of your model. So, this is one of the properties that a loss function is expected to have in this training regime, i.e. it needs to be [**differentiable**](https://ai.stackexchange.com/q/28877/2444).
  
  One of the most commonly used metrics to assess the quality of a classifier is the **accuracy**, which I think is not differentiable (although I don't have the proof and I didn't think too much about it). So, that's probably one of the reasons why people wouldn't use the accuracy as a loss function anyway. Now, keep in mind that the accuracy can actually be a very bad metric to assess the quality of your model because, if your dataset is unbalanced (and you didn't check that and the true distribution is not), your classifier could simply always predict the most common class and get a high accuracy, but this would be misleading.
  
  So, in summary, a function is *often* used as a loss function (in the context of training neural networks with GD) if
  
  1. it is differentiable, and,
  2. if you minimize it, you're actually trying to learn a probability distribution
  
  Note that the second property may not always be a requirement, but all the loss functions that I can think of right now have these two properties.
  
  On the other hand, a function is used as a metric if you care about what that specific function evaluates. For example, if you use the accuracy, you want to know, on average, how many mistakes the model makes, but you don't care whether it makes more mistakes for one class than the other. However, you can also use other functions, like precision and recall, which evaluate other behaviors, like how many mistakes you make for one class rather than the other. This can make sense, for example, in the context of detecting a disease.
  
  In principle, you could use a loss function also as a metric. However, is that really what you want? For instance, if you used the MSE as a metric, it's not clear how small it must be in order to have a "good" model. It all depends on which behavior of the model that you want to evaluate. You can evaluate different behaviors of the model, so that's why you often have more than one metric.
  
  
  ---
  
  