ChatGPT解决这个技术问题 Extra ChatGPT

How to interpret loss and accuracy for a machine learning model [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. This question does not appear to be about programming within the scope defined in the help center. Closed last year. Improve this question

When I trained my neural network with Theano or Tensorflow, they will report a variable called "loss" per epoch.

How should I interpret this variable? Higher loss is better or worse, or what does it mean for the final performance (accuracy) of my neural network?


V
VansFannel

The lower the loss, the better a model (unless the model has over-fitted to the training data). The loss is calculated on training and validation and its interperation is how well the model is doing for these two sets. Unlike accuracy, loss is not a percentage. It is a summation of the errors made for each example in training or validation sets.

In the case of neural networks, the loss is usually negative log-likelihood and residual sum of squares for classification and regression respectively. Then naturally, the main objective in a learning model is to reduce (minimize) the loss function's value with respect to the model's parameters by changing the weight vector values through different optimization methods, such as backpropagation in neural networks.

Loss value implies how well or poorly a certain model behaves after each iteration of optimization. Ideally, one would expect the reduction of loss after each, or several, iteration(s).

The accuracy of a model is usually determined after the model parameters are learned and fixed and no learning is taking place. Then the test samples are fed to the model and the number of mistakes (zero-one loss) the model makes are recorded, after comparison to the true targets. Then the percentage of misclassification is calculated.

For example, if the number of test samples is 1000 and model classifies 952 of those correctly, then the model's accuracy is 95.2%.

https://i.stack.imgur.com/Vnf0p.png

There are also some subtleties while reducing the loss value. For instance, you may run into the problem of over-fitting in which the model "memorizes" the training examples and becomes kind of ineffective for the test set. Over-fitting also occurs in cases where you do not employ a regularization, you have a very complex model (the number of free parameters W is large) or the number of data points N is very low.


Hi @Amir, thanks for your very details explanation. However, I have a problem: in my Neural Network, loss always decrease when I trained (when the epochs increase), however the accuracy is not better.
@mamatv As long as the cost is decreasing you should be good to go. Although cost and accuracy normally have a inverse proportionality relationship, but you may note that accuracy is a summation of zero-one errors whereas cost is a summation of floating point numbers. Therefore, 0.001% decrease in the cost does not necessarily mean 0.001% increase in the accuracy. Increasing the accuracy is much harder when the decrement in cost is intangible (the cost is very close to a local minima)
@mamatv I should have said as long as the cost for both training and validation is decreasing you should be good to go. You may also check validation accuracy on each epoch. If it starts going up, then your model might have started to over-fit and you should stop training it.
Why not train the model to increase accuracy rather than to minimise loss?
@bikashg accuracy is not differentiable and therefore you can't backprop on it.
T
TheMechanic

They are two different metrics to evaluate your model's performance usually being used in different phases.

Loss is often used in the training process to find the "best" parameter values for your model (e.g. weights in neural network). It is what you try to optimize in the training by updating weights.

Accuracy is more from an applied perspective. Once you find the optimized parameters above, you use this metrics to evaluate how accurate your model's prediction is compared to the true data.

Let us use a toy classification example. You want to predict gender from one's weight and height. You have 3 data, they are as follows:(0 stands for male, 1 stands for female)

y1 = 0, x1_w = 50kg, x2_h = 160cm;

y2 = 0, x2_w = 60kg, x2_h = 170cm;

y3 = 1, x3_w = 55kg, x3_h = 175cm;

You use a simple logistic regression model that is y = 1/(1+exp-(b1*x_w+b2*x_h))

How do you find b1 and b2? you define a loss first and use optimization method to minimize the loss in an iterative way by updating b1 and b2.

In our example, a typical loss for this binary classification problem can be: (a minus sign should be added in front of the summation sign)

https://i.stack.imgur.com/sVSxH.gif

We don't know what b1 and b2 should be. Let us make a random guess say b1 = 0.1 and b2 = -0.03. Then what is our loss now?

https://latex.codecogs.com/gif.latex?%5Chat%7By%7D_1%20%3D%20%5Cfrac%7B1%7D%7B%201%20+%20e%5E%7B%20-%280.1%20%5Ccdot%2050%20-%200.03%20%5Ccdot%20160%29%20%7D%20%7D%20%3D%200.549834%20%3D%200.55

https://latex.codecogs.com/gif.latex?%5Chat%7By%7D_2%20%3D%20%5Cfrac%7B1%7D%7B%201%20+%20e%5E%7B%20-%280.1%20%5Ccdot%2060%20-%200.03%20%5Ccdot%20170%29%20%7D%20%7D%20%3D%200.7109495%20%3D%200.71

https://latex.codecogs.com/gif.latex?%5Chat%7By%7D_3%20%3D%20%5Cfrac%7B1%7D%7B%201%20+%20e%5E%7B%20-%280.1%20%5Ccdot%2055%20-%200.03%20%5Ccdot%20175%29%20%7D%20%7D%20%3D%200.5621765%20%3D%200.56

so the loss is

https://latex.codecogs.com/gif.latex?-%5Clog%281-0.55%29%20-%5Clog%281-0.71%29%20-%20%5Clog%280.56%29%20%5Csimeq%202.6162

Then you learning algorithm (e.g. gradient descent) will find a way to update b1 and b2 to decrease the loss.

What if b1=0.1 and b2=-0.03 is the final b1 and b2 (output from gradient descent), what is the accuracy now?

Let's assume if y_hat >= 0.5, we decide our prediction is female(1). otherwise it would be 0. Therefore, our algorithm predict y1 = 1, y2 = 1 and y3 = 1. What is our accuracy? We make wrong prediction on y1 and y2 and make correct one on y3. So now our accuracy is 1/3 = 33.33%

PS: In Amir's answer, back-propagation is said to be an optimization method in NN. I think it would be treated as a way to find gradient for weights in NN. Common optimization method in NN are GradientDescent and Adam.


thank you for the math. it helped clarifying the concept.
Your math equations should be converted to mathjax they are rendering very strangely.
d
desertnaut

Just to clarify the Training/Validation/Test data sets: The training set is used to perform the initial training of the model, initializing the weights of the neural network.

The validation set is used after the neural network has been trained. It is used for tuning the network's hyperparameters, and comparing how changes to them affect the predictive accuracy of the model. Whereas the training set can be thought of as being used to build the neural network's gate weights, the validation set allows fine tuning of the parameters or architecture of the neural network model. It's useful as it allows repeatable comparison of these different parameters/architectures against the same data and networks weights, to observe how parameter/architecture changes affect the predictive power of the network.

Then the test set is used only to test the predictive accuracy of the trained neural network on previously unseen data, after training and parameter/architecture selection with the training and validation data sets.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now