{
  "id": 112032,
  "title": "Keras steps_per_epoch functionality in Fastai ",
  "url": "/competitions/rsna-intracranial-hemorrhage-detection/discussion/112032",
  "author_name": "neongen",
  "post_date": "2019-10-10T13:10:20.914000",
  "votes": 3,
  "comment_count": 0,
  "views": 0,
  "content": "<p>The Callback below simulates Keras steps_per_epoch functionality in Fastai\n```</p>\n\n<h1><a href=\"https://github.com/fastai/fastai/blob/96c5927648ecf83f0bc9ab601f672d3c0ffe0059/fastai/callbacks/misc.py\">https://github.com/fastai/fastai/blob/96c5927648ecf83f0bc9ab601f672d3c0ffe0059/fastai/callbacks/misc.py</a> (Borrowed-Modified)</h1>\n\n<p>class StopEpochAfterNBatches(Callback):\n    \"Stop epoch after n batches.\"\n    def <strong>init</strong>(self, n_batches:int=2):\n        self.stop,self.n_batches = False,n_batches-1 # iteration starts from 0</p>\n\n<pre><code>def on_batch_end(self, iteration, **kwargs):\n    if (iteration % self.n_batches == 0) and (iteration!=0):\n        return {'stop_epoch': True, 'stop_training': False, 'skip_validate': False}\n</code></pre>\n\n<p><code>\nUsage:\n</code>\nn_bs = number_of_images_per_epoch // batch_size\nlearn.fit(num_epochs, lr, callbacks=[StopEpochAfterNBatches(n_bs)]\n```\nThis can be of use when we need to train on large dataset like this one.\nHappy kaggling!</p>",
  "messages": [
    {
      "id": 645765,
      "postDate": "2019-10-10T13:10:20.913Z",
      "content": "<p>The Callback below simulates Keras steps_per_epoch functionality in Fastai\n```</p>\n\n<h1><a href=\"https://github.com/fastai/fastai/blob/96c5927648ecf83f0bc9ab601f672d3c0ffe0059/fastai/callbacks/misc.py\">https://github.com/fastai/fastai/blob/96c5927648ecf83f0bc9ab601f672d3c0ffe0059/fastai/callbacks/misc.py</a> (Borrowed-Modified)</h1>\n\n<p>class StopEpochAfterNBatches(Callback):\n    \"Stop epoch after n batches.\"\n    def <strong>init</strong>(self, n_batches:int=2):\n        self.stop,self.n_batches = False,n_batches-1 # iteration starts from 0</p>\n\n<pre><code>def on_batch_end(self, iteration, **kwargs):\n    if (iteration % self.n_batches == 0) and (iteration!=0):\n        return {'stop_epoch': True, 'stop_training': False, 'skip_validate': False}\n</code></pre>\n\n<p><code>\nUsage:\n</code>\nn_bs = number_of_images_per_epoch // batch_size\nlearn.fit(num_epochs, lr, callbacks=[StopEpochAfterNBatches(n_bs)]\n```\nThis can be of use when we need to train on large dataset like this one.\nHappy kaggling!</p>",
      "rawMarkdown": "The Callback below simulates Keras steps_per_epoch functionality in Fastai\n```\n#https://github.com/fastai/fastai/blob/96c5927648ecf83f0bc9ab601f672d3c0ffe0059/fastai/callbacks/misc.py (Borrowed-Modified)\nclass StopEpochAfterNBatches(Callback):\n    \"Stop epoch after n batches.\"\n    def __init__(self, n_batches:int=2):\n        self.stop,self.n_batches = False,n_batches-1 # iteration starts from 0\n\n    def on_batch_end(self, iteration, **kwargs):\n        if (iteration % self.n_batches == 0) and (iteration!=0):\n            return {'stop_epoch': True, 'stop_training': False, 'skip_validate': False}\n```\nUsage:\n```\nn_bs = number_of_images_per_epoch // batch_size\nlearn.fit(num_epochs, lr, callbacks=[StopEpochAfterNBatches(n_bs)]\n```\nThis can be of use when we need to train on large dataset like this one.\nHappy kaggling!",
      "votes": 3
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "645765": "The Callback below simulates Keras steps_per_epoch functionality in Fastai\n```\n#https://github.com/fastai/fastai/blob/96c5927648ecf83f0bc9ab601f672d3c0ffe0059/fastai/callbacks/misc.py (Borrowed-Modified)\nclass StopEpochAfterNBatches(Callback):\n    \"Stop epoch after n batches.\"\n    def __init__(self, n_batches:int=2):\n        self.stop,self.n_batches = False,n_batches-1 # iteration starts from 0\n\n    def on_batch_end(self, iteration, **kwargs):\n        if (iteration % self.n_batches == 0) and (iteration!=0):\n            return {'stop_epoch': True, 'stop_training': False, 'skip_validate': False}\n```\nUsage:\n```\nn_bs = number_of_images_per_epoch // batch_size\nlearn.fit(num_epochs, lr, callbacks=[StopEpochAfterNBatches(n_bs)]\n```\nThis can be of use when we need to train on large dataset like this one.\nHappy kaggling!"
  }
}