{
  "id": 65638,
  "title": "F2 metric with tensorflow",
  "url": "/competitions/airbus-ship-detection/discussion/65638",
  "author_name": "",
  "post_date": "2018-09-13T04:56:15.817529500Z",
  "votes": null,
  "comment_count": 2,
  "views": 0,
  "content": "<p>Anyone using tensorflow knows how to implement F2 metric in tensorflow? The problem is the shape of the placeholder is <strong>None</strong>. I don't know how to iterate over the images in each batch given by the placeholder, I mean I couldn't do \n<code>\nfor i in tf.shape(y)[0]\n</code>\n, but we have to compute the f2 for each image and then take the average. </p>",
  "messages": [
    {
      "id": "386544",
      "postDate": "09/13/2018 04:56:15",
      "content": "<p>Anyone using tensorflow knows how to implement F2 metric in tensorflow? The problem is the shape of the placeholder is <strong>None</strong>. I don't know how to iterate over the images in each batch given by the placeholder, I mean I couldn't do \n<code>\nfor i in tf.shape(y)[0]\n</code>\n, but we have to compute the f2 for each image and then take the average. </p>",
      "rawMarkdown": "Anyone using tensorflow knows how to implement F2 metric in tensorflow? The problem is the shape of the placeholder is **None**. I don't know how to iterate over the images in each batch given by the placeholder, I mean I couldn't do \n``` \nfor i in tf.shape(y)[0]\n```\n, but we have to compute the f2 for each image and then take the average.",
      "votes": null
    },
    {
      "id": "386801",
      "postDate": "09/13/2018 16:38:31",
      "content": "<p>How about tf.map_fn?</p>\n\n<pre><code>import tensorflow as tf\nimport numpy as np\n\nsess = tf.InteractiveSession()\n\n# numpy\na = np.random.randn(2,5,5,3)\ndef func_sum(b):\n  return b.sum()\n\nfor i in range(a.shape[0]):\n  print(func_sum(a[i]))\n\n# tensorflow\nta = tf.stack(a)\ndef func_tfsum(b):\n  return tf.reduce_sum(b)\n\ntb = tf.map_fn(func_tfsum, ta)\nsess.run(tb)\n</code></pre>",
      "rawMarkdown": "How about tf.map_fn?\n\n    import tensorflow as tf\n    import numpy as np\n    \n    sess = tf.InteractiveSession()\n    \n    # numpy\n    a = np.random.randn(2,5,5,3)\n    def func_sum(b):\n      return b.sum()\n    \n    for i in range(a.shape[0]):\n      print(func_sum(a[i]))\n    \n    # tensorflow\n    ta = tf.stack(a)\n    def func_tfsum(b):\n      return tf.reduce_sum(b)\n    \n    tb = tf.map_fn(func_tfsum, ta)\n    sess.run(tb)",
      "votes": null
    },
    {
      "id": "386879",
      "postDate": "09/13/2018 19:43:06",
      "content": "<p>Thanks a lot. It works! \nJust in case anyone is interested in the case with two arguments, I copied the code that worked for me below:</p>\n\n<pre><code>def func_tfsum(a,b):\n    return tf.reduce_sum(a) + tf.reduce_sum(b)\n\nsess = tf.InteractiveSession()\nfunc = lambda x:(func_tfsum(*x), 0.0)  \n# somehow we have to return two values, so the input/ouput have the same shape\n# https://stackoverflow.com/questions/47984876/tensorflow-tf-map-fn-parameters\n# I will keep only the first returned value\n\nfd = tf.map_fn(func, (ta,tb))\nsess.run(fd[0],feed_dict={inputs:x, labels:y}) \n</code></pre>",
      "rawMarkdown": "Thanks a lot. It works! \nJust in case anyone is interested in the case with two arguments, I copied the code that worked for me below:\n\n    def func_tfsum(a,b):\n        return tf.reduce_sum(a) + tf.reduce_sum(b)\n\n    sess = tf.InteractiveSession()\n    func = lambda x:(func_tfsum(*x), 0.0)  \n    # somehow we have to return two values, so the input/ouput have the same shape\n    # https://stackoverflow.com/questions/47984876/tensorflow-tf-map-fn-parameters\n    # I will keep only the first returned value\n\n    fd = tf.map_fn(func, (ta,tb))\n    sess.run(fd[0],feed_dict={inputs:x, labels:y})",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 386801,
      "author_name": "momi64",
      "author_url": "",
      "post_date": "09/13/2018 16:38:31",
      "content": "<p>How about tf.map_fn?</p>\n\n<pre><code>import tensorflow as tf\nimport numpy as np\n\nsess = tf.InteractiveSession()\n\n# numpy\na = np.random.randn(2,5,5,3)\ndef func_sum(b):\n  return b.sum()\n\nfor i in range(a.shape[0]):\n  print(func_sum(a[i]))\n\n# tensorflow\nta = tf.stack(a)\ndef func_tfsum(b):\n  return tf.reduce_sum(b)\n\ntb = tf.map_fn(func_tfsum, ta)\nsess.run(tb)\n</code></pre>",
      "votes": null,
      "replies": [
        {
          "id": 386879,
          "author_name": "zhengrui315",
          "author_url": "",
          "post_date": "09/13/2018 19:43:06",
          "content": "<p>Thanks a lot. It works! \nJust in case anyone is interested in the case with two arguments, I copied the code that worked for me below:</p>\n\n<pre><code>def func_tfsum(a,b):\n    return tf.reduce_sum(a) + tf.reduce_sum(b)\n\nsess = tf.InteractiveSession()\nfunc = lambda x:(func_tfsum(*x), 0.0)  \n# somehow we have to return two values, so the input/ouput have the same shape\n# https://stackoverflow.com/questions/47984876/tensorflow-tf-map-fn-parameters\n# I will keep only the first returned value\n\nfd = tf.map_fn(func, (ta,tb))\nsess.run(fd[0],feed_dict={inputs:x, labels:y}) \n</code></pre>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "386544": "Anyone using tensorflow knows how to implement F2 metric in tensorflow? The problem is the shape of the placeholder is **None**. I don't know how to iterate over the images in each batch given by the placeholder, I mean I couldn't do \n``` \nfor i in tf.shape(y)[0]\n```\n, but we have to compute the f2 for each image and then take the average.",
    "386801": "How about tf.map_fn?\n\n    import tensorflow as tf\n    import numpy as np\n    \n    sess = tf.InteractiveSession()\n    \n    # numpy\n    a = np.random.randn(2,5,5,3)\n    def func_sum(b):\n      return b.sum()\n    \n    for i in range(a.shape[0]):\n      print(func_sum(a[i]))\n    \n    # tensorflow\n    ta = tf.stack(a)\n    def func_tfsum(b):\n      return tf.reduce_sum(b)\n    \n    tb = tf.map_fn(func_tfsum, ta)\n    sess.run(tb)",
    "386879": "Thanks a lot. It works! \nJust in case anyone is interested in the case with two arguments, I copied the code that worked for me below:\n\n    def func_tfsum(a,b):\n        return tf.reduce_sum(a) + tf.reduce_sum(b)\n\n    sess = tf.InteractiveSession()\n    func = lambda x:(func_tfsum(*x), 0.0)  \n    # somehow we have to return two values, so the input/ouput have the same shape\n    # https://stackoverflow.com/questions/47984876/tensorflow-tf-map-fn-parameters\n    # I will keep only the first returned value\n\n    fd = tf.map_fn(func, (ta,tb))\n    sess.run(fd[0],feed_dict={inputs:x, labels:y})"
  },
  "source": "meta"
}