{
  "id": 117823,
  "title": "What is wrong with my ensemble code?",
  "url": "/competitions/understanding_cloud_organization/discussion/117823",
  "author_name": "",
  "post_date": "2019-11-18T04:59:30.713620200Z",
  "votes": null,
  "comment_count": 7,
  "views": 0,
  "content": "<p><code>\npreds = ((model1.predict_generator(test_generator) +model2.predict_generator(test_generator) +model3.predict_generator(test_generator))/3).astype(np.float16)\n</code></p>\n\n<p>edit: turns out it wasn't a problem with the code but with one of the models</p>",
  "messages": [
    {
      "id": "675441",
      "postDate": "11/18/2019 04:59:30",
      "content": "<p><code>\npreds = ((model1.predict_generator(test_generator) +model2.predict_generator(test_generator) +model3.predict_generator(test_generator))/3).astype(np.float16)\n</code></p>\n\n<p>edit: turns out it wasn't a problem with the code but with one of the models</p>",
      "rawMarkdown": "```\npreds = ((model1.predict_generator(test_generator) +model2.predict_generator(test_generator) +model3.predict_generator(test_generator))/3).astype(np.float16)\n```\n\nedit: turns out it wasn't a problem with the code but with one of the models",
      "votes": null
    },
    {
      "id": "675445",
      "postDate": "11/18/2019 05:06:14",
      "content": "<p>Did ensembling decreased your lb?</p>",
      "rawMarkdown": "Did ensembling decreased your lb?",
      "votes": null
    },
    {
      "id": "675456",
      "postDate": "11/18/2019 05:15:45",
      "content": "<p>no the code just doesn't ensemble at all lol</p>",
      "rawMarkdown": "no the code just doesn't ensemble at all lol",
      "votes": null
    },
    {
      "id": "675457",
      "postDate": "11/18/2019 05:17:55",
      "content": "<p>It looks fine to me</p>",
      "rawMarkdown": "It looks fine to me",
      "votes": null
    },
    {
      "id": "675471",
      "postDate": "11/18/2019 05:40:58",
      "content": "<p>What goes wrong exactly? Does your notebook crash? That method uses a lot of memory. It's best to predict a few images at a time and ensemble like this</p>\n\n<pre><code>for batch in test_generator:\n    preds = mode1.predict_on_batch(batch)\n    preds += model2.predict_on_batch(batch)\n    preds += model3.predict_on_batch(batch)\n    preds /= 3.0\n    for j in range(batch.shape[0]):\n        for i in range(batch.shape[-1]):\n             # CONVERT MASKS TO RLE HERE AND SAVE \n</code></pre>",
      "rawMarkdown": "What goes wrong exactly? Does your notebook crash? That method uses a lot of memory. It's best to predict a few images at a time and ensemble like this\n\n    for batch in test_generator:\n        preds = mode1.predict_on_batch(batch)\n        preds += model2.predict_on_batch(batch)\n        preds += model3.predict_on_batch(batch)\n        preds /= 3.0\n        for j in range(batch.shape[0]):\n            for i in range(batch.shape[-1]):\n                 # CONVERT MASKS TO RLE HERE AND SAVE",
      "votes": null
    },
    {
      "id": "675573",
      "postDate": "11/18/2019 08:50:20",
      "content": "<p>Hi,Chris,I also have a question on ensembling models here,cuz when I tried Kfold today,I made temperature ensemble on a 5_Kfold model with three other single models,so what is the correct way to ensemble,can I do ensemble like this:\n<code>python\nfor batch in test_generator:\n    preds=np.sqrt(model1.predict_on_batch(batch))\n    preds+=np.sqrt(model2.predict_on_batch(batch))\n    preds+=np.sqrt(model3.predict_on_batch(batch))\n    preds+=np.sqrt((model_Fold1.predict_on_batch(batch)+model_Fold2.predict_on_batch(batch)+\\\nmodel_Fold3.predict_on_batch(batch)+model_Fold4.predict_on_batch(batch)+model_Fold5.predict_on_batch(batch))/5)\n    preds/=4.0\n</code></p>",
      "rawMarkdown": "Hi,Chris,I also have a question on ensembling models here,cuz when I tried Kfold today,I made temperature ensemble on a 5_Kfold model with three other single models,so what is the correct way to ensemble,can I do ensemble like this:\n```python\nfor batch in test_generator:\n    preds=np.sqrt(model1.predict_on_batch(batch))\n    preds+=np.sqrt(model2.predict_on_batch(batch))\n    preds+=np.sqrt(model3.predict_on_batch(batch))\n    preds+=np.sqrt((model_Fold1.predict_on_batch(batch)+model_Fold2.predict_on_batch(batch)+\\\nmodel_Fold3.predict_on_batch(batch)+model_Fold4.predict_on_batch(batch)+model_Fold5.predict_on_batch(batch))/5)\n    preds/=4.0\n```",
      "votes": null
    },
    {
      "id": "675748",
      "postDate": "11/18/2019 13:58:58",
      "content": "<p>Yeah, that works but change the \"+=\" to \"=\" in the first line. You can also put every model on its own line like below:</p>\n\n<pre><code>for batch in test_generator:\n    preds = model1_fold1.predict_on_batch(batch) \n    preds += model1_fold2.predict_on_batch(batch) \n    preds += model1_fold3.predict_on_batch(batch) \n    preds += model1_fold4.predict_on_batch(batch) \n    preds += model1_fold5.predict_on_batch(batch) \n    preds = (preds / 5.0) **0.5\n    preds += model2.predict_on_batch(batch) **0.5\n    preds += model3.predict_on_batch(batch) **0.5\n    preds += model4.predict_on_batch(batch) **0.5\n    preds /= 4.0\n    for j in range(batch.shape[0]):\n        for i in range(batch.shape[-1]):\n             # CONVERT MASKS TO RLE HERE AND SAVE \n</code></pre>",
      "rawMarkdown": "Yeah, that works but change the \"+=\" to \"=\" in the first line. You can also put every model on its own line like below:\n\n  \n    for batch in test_generator:\n        preds = model1_fold1.predict_on_batch(batch) \n        preds += model1_fold2.predict_on_batch(batch) \n        preds += model1_fold3.predict_on_batch(batch) \n        preds += model1_fold4.predict_on_batch(batch) \n        preds += model1_fold5.predict_on_batch(batch) \n        preds = (preds / 5.0) **0.5\n        preds += model2.predict_on_batch(batch) **0.5\n        preds += model3.predict_on_batch(batch) **0.5\n        preds += model4.predict_on_batch(batch) **0.5\n        preds /= 4.0\n        for j in range(batch.shape[0]):\n            for i in range(batch.shape[-1]):\n                 # CONVERT MASKS TO RLE HERE AND SAVE",
      "votes": null
    },
    {
      "id": "675800",
      "postDate": "11/18/2019 15:17:05",
      "content": "<p>Thx Chris,so glad to hear from your advice.That's my mistake in the first line,I'm gonna change it right away</p>",
      "rawMarkdown": "Thx Chris,so glad to hear from your advice.That's my mistake in the first line,I'm gonna change it right away",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 675445,
      "author_name": "ubamba98",
      "author_url": "",
      "post_date": "11/18/2019 05:06:14",
      "content": "<p>Did ensembling decreased your lb?</p>",
      "votes": null,
      "replies": [
        {
          "id": 675456,
          "author_name": "cweed28",
          "author_url": "",
          "post_date": "11/18/2019 05:15:45",
          "content": "<p>no the code just doesn't ensemble at all lol</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 675457,
          "author_name": "ubamba98",
          "author_url": "",
          "post_date": "11/18/2019 05:17:55",
          "content": "<p>It looks fine to me</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 675471,
      "author_name": "cdeotte",
      "author_url": "",
      "post_date": "11/18/2019 05:40:58",
      "content": "<p>What goes wrong exactly? Does your notebook crash? That method uses a lot of memory. It's best to predict a few images at a time and ensemble like this</p>\n\n<pre><code>for batch in test_generator:\n    preds = mode1.predict_on_batch(batch)\n    preds += model2.predict_on_batch(batch)\n    preds += model3.predict_on_batch(batch)\n    preds /= 3.0\n    for j in range(batch.shape[0]):\n        for i in range(batch.shape[-1]):\n             # CONVERT MASKS TO RLE HERE AND SAVE \n</code></pre>",
      "votes": null,
      "replies": [
        {
          "id": 675573,
          "author_name": "sj626591833",
          "author_url": "",
          "post_date": "11/18/2019 08:50:20",
          "content": "<p>Hi,Chris,I also have a question on ensembling models here,cuz when I tried Kfold today,I made temperature ensemble on a 5_Kfold model with three other single models,so what is the correct way to ensemble,can I do ensemble like this:\n<code>python\nfor batch in test_generator:\n    preds=np.sqrt(model1.predict_on_batch(batch))\n    preds+=np.sqrt(model2.predict_on_batch(batch))\n    preds+=np.sqrt(model3.predict_on_batch(batch))\n    preds+=np.sqrt((model_Fold1.predict_on_batch(batch)+model_Fold2.predict_on_batch(batch)+\\\nmodel_Fold3.predict_on_batch(batch)+model_Fold4.predict_on_batch(batch)+model_Fold5.predict_on_batch(batch))/5)\n    preds/=4.0\n</code></p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 675748,
          "author_name": "cdeotte",
          "author_url": "",
          "post_date": "11/18/2019 13:58:58",
          "content": "<p>Yeah, that works but change the \"+=\" to \"=\" in the first line. You can also put every model on its own line like below:</p>\n\n<pre><code>for batch in test_generator:\n    preds = model1_fold1.predict_on_batch(batch) \n    preds += model1_fold2.predict_on_batch(batch) \n    preds += model1_fold3.predict_on_batch(batch) \n    preds += model1_fold4.predict_on_batch(batch) \n    preds += model1_fold5.predict_on_batch(batch) \n    preds = (preds / 5.0) **0.5\n    preds += model2.predict_on_batch(batch) **0.5\n    preds += model3.predict_on_batch(batch) **0.5\n    preds += model4.predict_on_batch(batch) **0.5\n    preds /= 4.0\n    for j in range(batch.shape[0]):\n        for i in range(batch.shape[-1]):\n             # CONVERT MASKS TO RLE HERE AND SAVE \n</code></pre>",
          "votes": null,
          "replies": []
        },
        {
          "id": 675800,
          "author_name": "sj626591833",
          "author_url": "",
          "post_date": "11/18/2019 15:17:05",
          "content": "<p>Thx Chris,so glad to hear from your advice.That's my mistake in the first line,I'm gonna change it right away</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "675441": "```\npreds = ((model1.predict_generator(test_generator) +model2.predict_generator(test_generator) +model3.predict_generator(test_generator))/3).astype(np.float16)\n```\n\nedit: turns out it wasn't a problem with the code but with one of the models",
    "675445": "Did ensembling decreased your lb?",
    "675456": "no the code just doesn't ensemble at all lol",
    "675457": "It looks fine to me",
    "675471": "What goes wrong exactly? Does your notebook crash? That method uses a lot of memory. It's best to predict a few images at a time and ensemble like this\n\n    for batch in test_generator:\n        preds = mode1.predict_on_batch(batch)\n        preds += model2.predict_on_batch(batch)\n        preds += model3.predict_on_batch(batch)\n        preds /= 3.0\n        for j in range(batch.shape[0]):\n            for i in range(batch.shape[-1]):\n                 # CONVERT MASKS TO RLE HERE AND SAVE",
    "675573": "Hi,Chris,I also have a question on ensembling models here,cuz when I tried Kfold today,I made temperature ensemble on a 5_Kfold model with three other single models,so what is the correct way to ensemble,can I do ensemble like this:\n```python\nfor batch in test_generator:\n    preds=np.sqrt(model1.predict_on_batch(batch))\n    preds+=np.sqrt(model2.predict_on_batch(batch))\n    preds+=np.sqrt(model3.predict_on_batch(batch))\n    preds+=np.sqrt((model_Fold1.predict_on_batch(batch)+model_Fold2.predict_on_batch(batch)+\\\nmodel_Fold3.predict_on_batch(batch)+model_Fold4.predict_on_batch(batch)+model_Fold5.predict_on_batch(batch))/5)\n    preds/=4.0\n```",
    "675748": "Yeah, that works but change the \"+=\" to \"=\" in the first line. You can also put every model on its own line like below:\n\n  \n    for batch in test_generator:\n        preds = model1_fold1.predict_on_batch(batch) \n        preds += model1_fold2.predict_on_batch(batch) \n        preds += model1_fold3.predict_on_batch(batch) \n        preds += model1_fold4.predict_on_batch(batch) \n        preds += model1_fold5.predict_on_batch(batch) \n        preds = (preds / 5.0) **0.5\n        preds += model2.predict_on_batch(batch) **0.5\n        preds += model3.predict_on_batch(batch) **0.5\n        preds += model4.predict_on_batch(batch) **0.5\n        preds /= 4.0\n        for j in range(batch.shape[0]):\n            for i in range(batch.shape[-1]):\n                 # CONVERT MASKS TO RLE HERE AND SAVE",
    "675800": "Thx Chris,so glad to hear from your advice.That's my mistake in the first line,I'm gonna change it right away"
  },
  "source": "meta"
}