How to compute f1 in tensorflow The 2019 Stack Overflow Developer Survey Results Are InFine-tuning a model from an existing checkpoint with TensorFlow-SlimHow to improve my test accuracy using CNN in TensorflowTensorflow MLP worse than Keras(TF backend)Tensorflow regression predicting 1 for all inputsLSTM Implementation using tensorflow (anaconda)Area Under Curve with probabilityDynamic rnn for toysequence classificationTensorflow.js mapping to Tensorflow (python)Tensor Operation in TensorflowTensorflow/Keras, How to convert tf.feature_column into input tensors?

Can you compress metal and what would be the consequences?

Is bread bad for ducks?

If a sorcerer casts the Banishment spell on a PC while in Avernus, does the PC return to their home plane?

Are spiders unable to hurt humans, especially very small spiders?

Cooking pasta in a water boiler

How to type a long/em dash `—`

What is the accessibility of a package's `Private` context variables?

Why didn't the Event Horizon Telescope team mention Sagittarius A*?

Using xargs with pdftk

How to deal with speedster characters?

Multiply Two Integer Polynomials

Can one be advised by a professor who is very far away?

What do hard-Brexiteers want with respect to the Irish border?

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

Why is the Constellation's nose gear so long?

Merge two greps into single one

Loose spokes after only a few rides

Is it possible for absolutely everyone to attain enlightenment?

Did Scotland spend $250,000 for the slogan "Welcome to Scotland"?

Can we generate random numbers using irrational numbers like π and e?

Right tool to dig six foot holes?

"consumers choosing to rely" vs. "consumers to choose to rely"

Why doesn't shell automatically fix "useless use of cat"?

How much of the clove should I use when using big garlic heads?



How to compute f1 in tensorflow



The 2019 Stack Overflow Developer Survey Results Are InFine-tuning a model from an existing checkpoint with TensorFlow-SlimHow to improve my test accuracy using CNN in TensorflowTensorflow MLP worse than Keras(TF backend)Tensorflow regression predicting 1 for all inputsLSTM Implementation using tensorflow (anaconda)Area Under Curve with probabilityDynamic rnn for toysequence classificationTensorflow.js mapping to Tensorflow (python)Tensor Operation in TensorflowTensorflow/Keras, How to convert tf.feature_column into input tensors?










0












$begingroup$


I have code that computes the accuracy, but now I would like to compute the f1 score.



accuracy_1 = tf.reduce_mean(tf.cast(tf.equal(
tf.argmax(output_1, axis=-1),
tf.argmax(y_1, axis=-1)), tf.float32), name="accuracy_1")
accuracy_2 = tf.reduce_mean(tf.cast(tf.equal(
tf.argmax(output_2, axis=-1),
tf.argmax(y_2, axis=-1)), tf.float32), name="accuracy_2")


How can I compute f1 equivalent for the above code? I'm finding it difficult as I am very new to tensorflow.










share|improve this question











$endgroup$
















    0












    $begingroup$


    I have code that computes the accuracy, but now I would like to compute the f1 score.



    accuracy_1 = tf.reduce_mean(tf.cast(tf.equal(
    tf.argmax(output_1, axis=-1),
    tf.argmax(y_1, axis=-1)), tf.float32), name="accuracy_1")
    accuracy_2 = tf.reduce_mean(tf.cast(tf.equal(
    tf.argmax(output_2, axis=-1),
    tf.argmax(y_2, axis=-1)), tf.float32), name="accuracy_2")


    How can I compute f1 equivalent for the above code? I'm finding it difficult as I am very new to tensorflow.










    share|improve this question











    $endgroup$














      0












      0








      0





      $begingroup$


      I have code that computes the accuracy, but now I would like to compute the f1 score.



      accuracy_1 = tf.reduce_mean(tf.cast(tf.equal(
      tf.argmax(output_1, axis=-1),
      tf.argmax(y_1, axis=-1)), tf.float32), name="accuracy_1")
      accuracy_2 = tf.reduce_mean(tf.cast(tf.equal(
      tf.argmax(output_2, axis=-1),
      tf.argmax(y_2, axis=-1)), tf.float32), name="accuracy_2")


      How can I compute f1 equivalent for the above code? I'm finding it difficult as I am very new to tensorflow.










      share|improve this question











      $endgroup$




      I have code that computes the accuracy, but now I would like to compute the f1 score.



      accuracy_1 = tf.reduce_mean(tf.cast(tf.equal(
      tf.argmax(output_1, axis=-1),
      tf.argmax(y_1, axis=-1)), tf.float32), name="accuracy_1")
      accuracy_2 = tf.reduce_mean(tf.cast(tf.equal(
      tf.argmax(output_2, axis=-1),
      tf.argmax(y_2, axis=-1)), tf.float32), name="accuracy_2")


      How can I compute f1 equivalent for the above code? I'm finding it difficult as I am very new to tensorflow.







      machine-learning tensorflow metric






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 30 at 17:32









      Ethan

      701625




      701625










      asked Mar 30 at 6:11









      William ScottWilliam Scott

      1063




      1063




















          2 Answers
          2






          active

          oldest

          votes


















          0












          $begingroup$

          To compute f1_score, first, use this function of python sklearn library to produce confusion matrix, after that, from the confusion matrix generate TP,TN,FP,FN then use them to calculate:



          Recall = TP/TP+FN and Precision = TP/TP+FP



          and then from the above 2 metrics you can easly calculate:



          f1_score = 2 * (precision * recall) / (precision + recall)



          OR



          you can use another function of the same library here to compute f1_score directly from the generated y_true and y_pred like below:



          F1 = f1_score(y_true, y_pred, average='binary')


          finally, the library links consists of a helpful explanation, you should read them carefully.






          share|improve this answer









          $endgroup$












          • $begingroup$
            Hi, thanks a lot for the reply. I know how to compute f1, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
            $endgroup$
            – William Scott
            Mar 30 at 9:01











          • $begingroup$
            tensorflow has a tf.metrics function, see here tensorflow.org/api_docs/python/tf/metrics
            $endgroup$
            – SoK
            Mar 30 at 9:09










          • $begingroup$
            i saw that, but it doesnt' seem to work. That's the reason i posted here.
            $endgroup$
            – William Scott
            Mar 30 at 10:18


















          0












          $begingroup$

          f1 can be Defined as a custom metric. Keras will evaluate this metric on each batch/epoch as applicable.



          import keras.backend as K

          def f1_metric(y_true, y_pred):
          true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
          possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
          predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
          precision = true_positives / (predicted_positives + K.epsilon())
          recall = true_positives / (possible_positives + K.epsilon())
          f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())
          return f1_val


          model.compile(...,metrics=['accuracy', f1_metric])





          share|improve this answer









          $endgroup$












          • $begingroup$
            Hi, thanks a lot for the reply. I know how to do this in keras, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
            $endgroup$
            – William Scott
            Mar 30 at 9:01











          • $begingroup$
            TF has builtin metric for F1 . tensorflow.org/api_docs/python/tf/contrib/metrics/f1_score
            $endgroup$
            – Shamit Verma
            Mar 30 at 9:08










          • $begingroup$
            i saw that, but it doesnt seem to work. That's the reason i posted here.
            $endgroup$
            – William Scott
            Mar 30 at 10:18











          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "557"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f48246%2fhow-to-compute-f1-in-tensorflow%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0












          $begingroup$

          To compute f1_score, first, use this function of python sklearn library to produce confusion matrix, after that, from the confusion matrix generate TP,TN,FP,FN then use them to calculate:



          Recall = TP/TP+FN and Precision = TP/TP+FP



          and then from the above 2 metrics you can easly calculate:



          f1_score = 2 * (precision * recall) / (precision + recall)



          OR



          you can use another function of the same library here to compute f1_score directly from the generated y_true and y_pred like below:



          F1 = f1_score(y_true, y_pred, average='binary')


          finally, the library links consists of a helpful explanation, you should read them carefully.






          share|improve this answer









          $endgroup$












          • $begingroup$
            Hi, thanks a lot for the reply. I know how to compute f1, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
            $endgroup$
            – William Scott
            Mar 30 at 9:01











          • $begingroup$
            tensorflow has a tf.metrics function, see here tensorflow.org/api_docs/python/tf/metrics
            $endgroup$
            – SoK
            Mar 30 at 9:09










          • $begingroup$
            i saw that, but it doesnt' seem to work. That's the reason i posted here.
            $endgroup$
            – William Scott
            Mar 30 at 10:18















          0












          $begingroup$

          To compute f1_score, first, use this function of python sklearn library to produce confusion matrix, after that, from the confusion matrix generate TP,TN,FP,FN then use them to calculate:



          Recall = TP/TP+FN and Precision = TP/TP+FP



          and then from the above 2 metrics you can easly calculate:



          f1_score = 2 * (precision * recall) / (precision + recall)



          OR



          you can use another function of the same library here to compute f1_score directly from the generated y_true and y_pred like below:



          F1 = f1_score(y_true, y_pred, average='binary')


          finally, the library links consists of a helpful explanation, you should read them carefully.






          share|improve this answer









          $endgroup$












          • $begingroup$
            Hi, thanks a lot for the reply. I know how to compute f1, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
            $endgroup$
            – William Scott
            Mar 30 at 9:01











          • $begingroup$
            tensorflow has a tf.metrics function, see here tensorflow.org/api_docs/python/tf/metrics
            $endgroup$
            – SoK
            Mar 30 at 9:09










          • $begingroup$
            i saw that, but it doesnt' seem to work. That's the reason i posted here.
            $endgroup$
            – William Scott
            Mar 30 at 10:18













          0












          0








          0





          $begingroup$

          To compute f1_score, first, use this function of python sklearn library to produce confusion matrix, after that, from the confusion matrix generate TP,TN,FP,FN then use them to calculate:



          Recall = TP/TP+FN and Precision = TP/TP+FP



          and then from the above 2 metrics you can easly calculate:



          f1_score = 2 * (precision * recall) / (precision + recall)



          OR



          you can use another function of the same library here to compute f1_score directly from the generated y_true and y_pred like below:



          F1 = f1_score(y_true, y_pred, average='binary')


          finally, the library links consists of a helpful explanation, you should read them carefully.






          share|improve this answer









          $endgroup$



          To compute f1_score, first, use this function of python sklearn library to produce confusion matrix, after that, from the confusion matrix generate TP,TN,FP,FN then use them to calculate:



          Recall = TP/TP+FN and Precision = TP/TP+FP



          and then from the above 2 metrics you can easly calculate:



          f1_score = 2 * (precision * recall) / (precision + recall)



          OR



          you can use another function of the same library here to compute f1_score directly from the generated y_true and y_pred like below:



          F1 = f1_score(y_true, y_pred, average='binary')


          finally, the library links consists of a helpful explanation, you should read them carefully.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 30 at 7:17









          SoKSoK

          31814




          31814











          • $begingroup$
            Hi, thanks a lot for the reply. I know how to compute f1, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
            $endgroup$
            – William Scott
            Mar 30 at 9:01











          • $begingroup$
            tensorflow has a tf.metrics function, see here tensorflow.org/api_docs/python/tf/metrics
            $endgroup$
            – SoK
            Mar 30 at 9:09










          • $begingroup$
            i saw that, but it doesnt' seem to work. That's the reason i posted here.
            $endgroup$
            – William Scott
            Mar 30 at 10:18
















          • $begingroup$
            Hi, thanks a lot for the reply. I know how to compute f1, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
            $endgroup$
            – William Scott
            Mar 30 at 9:01











          • $begingroup$
            tensorflow has a tf.metrics function, see here tensorflow.org/api_docs/python/tf/metrics
            $endgroup$
            – SoK
            Mar 30 at 9:09










          • $begingroup$
            i saw that, but it doesnt' seem to work. That's the reason i posted here.
            $endgroup$
            – William Scott
            Mar 30 at 10:18















          $begingroup$
          Hi, thanks a lot for the reply. I know how to compute f1, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
          $endgroup$
          – William Scott
          Mar 30 at 9:01





          $begingroup$
          Hi, thanks a lot for the reply. I know how to compute f1, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
          $endgroup$
          – William Scott
          Mar 30 at 9:01













          $begingroup$
          tensorflow has a tf.metrics function, see here tensorflow.org/api_docs/python/tf/metrics
          $endgroup$
          – SoK
          Mar 30 at 9:09




          $begingroup$
          tensorflow has a tf.metrics function, see here tensorflow.org/api_docs/python/tf/metrics
          $endgroup$
          – SoK
          Mar 30 at 9:09












          $begingroup$
          i saw that, but it doesnt' seem to work. That's the reason i posted here.
          $endgroup$
          – William Scott
          Mar 30 at 10:18




          $begingroup$
          i saw that, but it doesnt' seem to work. That's the reason i posted here.
          $endgroup$
          – William Scott
          Mar 30 at 10:18











          0












          $begingroup$

          f1 can be Defined as a custom metric. Keras will evaluate this metric on each batch/epoch as applicable.



          import keras.backend as K

          def f1_metric(y_true, y_pred):
          true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
          possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
          predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
          precision = true_positives / (predicted_positives + K.epsilon())
          recall = true_positives / (possible_positives + K.epsilon())
          f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())
          return f1_val


          model.compile(...,metrics=['accuracy', f1_metric])





          share|improve this answer









          $endgroup$












          • $begingroup$
            Hi, thanks a lot for the reply. I know how to do this in keras, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
            $endgroup$
            – William Scott
            Mar 30 at 9:01











          • $begingroup$
            TF has builtin metric for F1 . tensorflow.org/api_docs/python/tf/contrib/metrics/f1_score
            $endgroup$
            – Shamit Verma
            Mar 30 at 9:08










          • $begingroup$
            i saw that, but it doesnt seem to work. That's the reason i posted here.
            $endgroup$
            – William Scott
            Mar 30 at 10:18















          0












          $begingroup$

          f1 can be Defined as a custom metric. Keras will evaluate this metric on each batch/epoch as applicable.



          import keras.backend as K

          def f1_metric(y_true, y_pred):
          true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
          possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
          predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
          precision = true_positives / (predicted_positives + K.epsilon())
          recall = true_positives / (possible_positives + K.epsilon())
          f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())
          return f1_val


          model.compile(...,metrics=['accuracy', f1_metric])





          share|improve this answer









          $endgroup$












          • $begingroup$
            Hi, thanks a lot for the reply. I know how to do this in keras, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
            $endgroup$
            – William Scott
            Mar 30 at 9:01











          • $begingroup$
            TF has builtin metric for F1 . tensorflow.org/api_docs/python/tf/contrib/metrics/f1_score
            $endgroup$
            – Shamit Verma
            Mar 30 at 9:08










          • $begingroup$
            i saw that, but it doesnt seem to work. That's the reason i posted here.
            $endgroup$
            – William Scott
            Mar 30 at 10:18













          0












          0








          0





          $begingroup$

          f1 can be Defined as a custom metric. Keras will evaluate this metric on each batch/epoch as applicable.



          import keras.backend as K

          def f1_metric(y_true, y_pred):
          true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
          possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
          predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
          precision = true_positives / (predicted_positives + K.epsilon())
          recall = true_positives / (possible_positives + K.epsilon())
          f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())
          return f1_val


          model.compile(...,metrics=['accuracy', f1_metric])





          share|improve this answer









          $endgroup$



          f1 can be Defined as a custom metric. Keras will evaluate this metric on each batch/epoch as applicable.



          import keras.backend as K

          def f1_metric(y_true, y_pred):
          true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
          possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
          predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
          precision = true_positives / (predicted_positives + K.epsilon())
          recall = true_positives / (possible_positives + K.epsilon())
          f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())
          return f1_val


          model.compile(...,metrics=['accuracy', f1_metric])






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 30 at 8:45









          Shamit VermaShamit Verma

          1,5191314




          1,5191314











          • $begingroup$
            Hi, thanks a lot for the reply. I know how to do this in keras, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
            $endgroup$
            – William Scott
            Mar 30 at 9:01











          • $begingroup$
            TF has builtin metric for F1 . tensorflow.org/api_docs/python/tf/contrib/metrics/f1_score
            $endgroup$
            – Shamit Verma
            Mar 30 at 9:08










          • $begingroup$
            i saw that, but it doesnt seem to work. That's the reason i posted here.
            $endgroup$
            – William Scott
            Mar 30 at 10:18
















          • $begingroup$
            Hi, thanks a lot for the reply. I know how to do this in keras, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
            $endgroup$
            – William Scott
            Mar 30 at 9:01











          • $begingroup$
            TF has builtin metric for F1 . tensorflow.org/api_docs/python/tf/contrib/metrics/f1_score
            $endgroup$
            – Shamit Verma
            Mar 30 at 9:08










          • $begingroup$
            i saw that, but it doesnt seem to work. That's the reason i posted here.
            $endgroup$
            – William Scott
            Mar 30 at 10:18















          $begingroup$
          Hi, thanks a lot for the reply. I know how to do this in keras, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
          $endgroup$
          – William Scott
          Mar 30 at 9:01





          $begingroup$
          Hi, thanks a lot for the reply. I know how to do this in keras, but i want it in tensorflow, i basically want to replace the above code mentioned with f1.
          $endgroup$
          – William Scott
          Mar 30 at 9:01













          $begingroup$
          TF has builtin metric for F1 . tensorflow.org/api_docs/python/tf/contrib/metrics/f1_score
          $endgroup$
          – Shamit Verma
          Mar 30 at 9:08




          $begingroup$
          TF has builtin metric for F1 . tensorflow.org/api_docs/python/tf/contrib/metrics/f1_score
          $endgroup$
          – Shamit Verma
          Mar 30 at 9:08












          $begingroup$
          i saw that, but it doesnt seem to work. That's the reason i posted here.
          $endgroup$
          – William Scott
          Mar 30 at 10:18




          $begingroup$
          i saw that, but it doesnt seem to work. That's the reason i posted here.
          $endgroup$
          – William Scott
          Mar 30 at 10:18

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Data Science Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f48246%2fhow-to-compute-f1-in-tensorflow%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

          Luettelo Yhdysvaltain laivaston lentotukialuksista Lähteet | Navigointivalikko

          Gary (muusikko) Sisällysluettelo Historia | Rockin' High | Lähteet | Aiheesta muualla | NavigointivalikkoInfobox OKTuomas "Gary" Keskinen Ancaran kitaristiksiProjekti Rockin' High