Answers.Body: <p>After each epoch, the model is applied to the features of the validation set (x_val) to predict the probability of the labels.  Then, the probabilities are thresholded to generate the predicted label (y_pred).  Finally, the accuracy is calculated as follows:</p>
  <p><span class="math-container">$$ Accuracy\ =\ \frac{Concordant\ samples}{Total\ samples} $$</span></p>
  <p>, where concordant samples are those for which the predicted (y_pred) and true labels (y_val) agree.</p>
  <p>Assume we have a binary classification task and 5 validation samples.  If <code>y_pred = [0, 1, 1, 0, 1]</code> and <code>y_val = [0, 1, 1, 1, 0]</code>, then there are <code>3</code> concordant samples, giving an accuracy of <code>3/5 = 60%</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-11-07T18:28:25.687Z
Answers.Text: Thanks for your reply. But the thing is that I need a more deep explanation as I am writing a custom loss function that requires to optimized toward a value in each epoch. Do you have any idea?
Answers.UserDisplayName: David
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-11-07T20:52:49.487Z
Answers.Text: Based on the information that you have provided, this is as deep an explanation as anyone would be able to provide you.
Answers.UserDisplayName: Snehal Patel
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-11-06T20:48:17.667Z
Answers.LastActivityDate: 2022-11-07T17:18:24.163Z
Answers.LastEditDate: 2022-11-07T17:18:24.163Z
Answers.UserDisplayName: Snehal Patel
Body: <p>After each epoch, Keras provides the following evaluations (depending on how the model is compiled):</p>
  <ol>
  <li>train_accuracy</li>
  <li>train_loss</li>
  <li>validation_loss</li>
  <li>validation_accuracy</li>
  </ol>
  <p>Keras evaluates the performance of the model using the validation set at the end of each epoch.  But how does Keras do this?  Assume that we are performing binary classification and using a binary loss function.  Assume that there are 100 images in the validation set.</p>
  <p><strong>QUESTION:</strong>  Can the accuracy can be found by simply calculating accuracy for each item and then average them (/100)?</p>
  <p><strong>CONTEXT:</strong>  I want to implement a custom loss function that calculates the loss towards reducing validation loss (increasing validation accuracy). Therefore, I need to understand how the accuracy is calculated for the validation set.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-08-16T09:25:32.580Z
Comments.Text: Keras is a powerful tool that does many things. Some more automated than others. It would be best to read and understand the code, referring to the documentation if necessary (I presume you're using someone else's code?). In any case, we would need more information about what functions are being used before we can tell.
Comments.UserDisplayName: Martino
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-08T08:03:49.423Z
Comments.Text: From what I understand, the reason for your question is that are trying to calculate a loss from the validation set. Please note that if you were to do such a thing, that is no longer a validation set, but a second train set. A validation set is specifically used to monitor the performance without learning from that set of data.
Comments.UserDisplayName: Kroshtan
Tags: <machine-learning><deep-learning><tensorflow><keras>
Title: How exactly does Keras calculate the validation accuracy?
UserDisplayName: David
ThreadText: # How exactly does Keras calculate the validation accuracy?
  
  (Asked by: username_0 on 2022-08-15)
  
  After each epoch, Keras provides the following evaluations (depending on how the model is compiled):
  
  1. train\_accuracy
  2. train\_loss
  3. validation\_loss
  4. validation\_accuracy
  
  Keras evaluates the performance of the model using the validation set at the end of each epoch. But how does Keras do this? Assume that we are performing binary classification and using a binary loss function. Assume that there are 100 images in the validation set.
  
  **QUESTION:** Can the accuracy can be found by simply calculating accuracy for each item and then average them (/100)?
  
  **CONTEXT:** I want to implement a custom loss function that calculates the loss towards reducing validation loss (increasing validation accuracy). Therefore, I need to understand how the accuracy is calculated for the validation set.
  
  **Tags:** machine-learning, deep-learning, tensorflow, keras
  > **Comments:**
  > **username_1**: Keras is a powerful tool that does many things. Some more automated than others. It would be best to read and understand the code, referring to the documentation if necessary (I presume you're using someone else's code?). In any case, we would need more information about what functions are being used before we can tell.  
  > **username_2**: From what I understand, the reason for your question is that are trying to calculate a loss from the validation set. Please note that if you were to do such a thing, that is no longer a validation set, but a second train set. A validation set is specifically used to monitor the performance without learning from that set of data.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2022-11-06. Score: 2
  
  After each epoch, the model is applied to the features of the validation set (x\_val) to predict the probability of the labels. Then, the probabilities are thresholded to generate the predicted label (y\_pred). Finally, the accuracy is calculated as follows:
  
  $$ Accuracy\ =\ \frac{Concordant\ samples}{Total\ samples} $$
  
  , where concordant samples are those for which the predicted (y\_pred) and true labels (y\_val) agree.
  
  Assume we have a binary classification task and 5 validation samples. If `y_pred = [0, 1, 1, 0, 1]` and `y_val = [0, 1, 1, 1, 0]`, then there are `3` concordant samples, giving an accuracy of `3/5 = 60%`.
  
  > **Comments:**
  > **username_0**: Thanks for your reply. But the thing is that I need a more deep explanation as I am writing a custom loss function that requires to optimized toward a value in each epoch. Do you have any idea?  
  > **username_3**: Based on the information that you have provided, this is as deep an explanation as anyone would be able to provide you.  
  
  ---
  
  