{"id":13478580,"url":"https://github.com/julvo/reloading","last_synced_at":"2025-05-15T12:03:49.314Z","repository":{"id":41203216,"uuid":"196635946","full_name":"julvo/reloading","owner":"julvo","description":"Change Python code while it's running without losing state","archived":false,"fork":false,"pushed_at":"2024-06-15T18:50:01.000Z","size":630,"stargazers_count":1103,"open_issues_count":12,"forks_count":33,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-10T21:46:44.485Z","etag":null,"topics":["deep-learning","hot-reload","hot-reloading","interactive","keras","machine-learning","python","pytorch","utility"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/julvo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-12T19:39:49.000Z","updated_at":"2025-05-07T18:49:54.000Z","dependencies_parsed_at":"2024-10-30T11:43:08.562Z","dependency_job_id":null,"html_url":"https://github.com/julvo/reloading","commit_stats":{"total_commits":47,"total_committers":6,"mean_commits":7.833333333333333,"dds":0.6170212765957447,"last_synced_commit":"541ca988de7abed975dd31f5e3477be3f4827072"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/julvo%2Freloading","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/julvo%2Freloading/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/julvo%2Freloading/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/julvo%2Freloading/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/julvo","download_url":"https://codeload.github.com/julvo/reloading/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254337612,"owners_count":22054253,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","hot-reload","hot-reloading","interactive","keras","machine-learning","python","pytorch","utility"],"created_at":"2024-07-31T16:01:58.960Z","updated_at":"2025-05-15T12:03:49.283Z","avatar_url":"https://github.com/julvo.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# reloading\n[![pypi badge](https://img.shields.io/pypi/v/reloading?color=%230c0)](https://pypi.org/project/reloading/)\n\nA Python utility to reload a loop body from source on each iteration without\nlosing state\n\nUseful for editing source code during training of deep learning models. This lets\nyou e.g. add logging, print statistics or save the model without restarting the\ntraining and, therefore, without losing the training progress.\n\n![Demo](https://github.com/julvo/reloading/blob/master/examples/demo/demo.gif)\n\n## Install\n```\npip install reloading\n```\n\n## Usage\n\nTo reload the body of a `for` loop from source before each iteration, simply \nwrap the iterator with `reloading`, e.g.\n```python\nfrom reloading import reloading\n\nfor i in reloading(range(10)):\n    # this code will be reloaded before each iteration\n    print(i)\n\n```\n\nTo reload a function from source before each execution, decorate the function\ndefinition with `@reloading`, e.g.\n```python\nfrom reloading import reloading\n\n@reloading\ndef some_function():\n    # this code will be reloaded before each invocation\n    pass\n```\n\n## Additional Options\n\nPass the keyword argument `every` to reload only on every n-th invocation or iteration. E.g.\n```python\nfor i in reloading(range(1000), every=10):\n    # this code will only be reloaded before every 10th iteration\n    # this can help to speed-up tight loops\n    pass\n\n@reloading(every=10)\ndef some_function():\n    # this code with only be reloaded before every 10th invocation\n    pass\n```\n\nPass `forever=True` instead of an iterable to create an endless reloading loop, e.g. \n```python\nfor i in reloading(forever=True):\n    # this code will loop forever and reload from source before each iteration\n    pass\n```\n\n## Examples\n\nHere are the short snippets of how to use reloading with your favourite library.\nFor complete examples, check out the [examples folder](https://github.com/julvo/reloading/blob/master/examples).\n\n### PyTorch\n```python\nfor epoch in reloading(range(NB_EPOCHS)):\n    # the code inside this outer loop will be reloaded before each epoch\n\n    for images, targets in dataloader:\n        optimiser.zero_grad()\n        predictions = model(images)\n        loss = F.cross_entropy(predictions, targets)\n        loss.backward()\n        optimiser.step()\n```\n[Here](https://github.com/julvo/reloading/blob/master/examples/pytorch/train.py) is a full PyTorch example.\n\n### fastai\n```python\n@reloading\ndef update_learner(learner):\n    # this function will be reloaded from source before each epoch so that you\n    # can make changes to the learner while the training is running\n    pass\n\nclass LearnerUpdater(LearnerCallback):\n    def on_epoch_begin(self, **kwargs):\n        update_learner(self.learn)\n\npath = untar_data(URLs.MNIST_SAMPLE)\ndata = ImageDataBunch.from_folder(path)\nlearn = cnn_learner(data, models.resnet18, metrics=accuracy, \n                    callback_fns=[LearnerUpdater])\nlearn.fit(10)\n```\n[Here](https://github.com/julvo/reloading/blob/master/examples/fastai/train.py) is a full fastai example.\n\n### Keras\n```python\n@reloading\ndef update_model(model):\n    # this function will be reloaded from source before each epoch so that you\n    # can make changes to the model while the training is running using\n    # K.set_value()\n    pass\n\nclass ModelUpdater(Callback):\n    def on_epoch_begin(self, epoch, logs=None):\n        update_model(self.model)\n\nmodel = Sequential()\nmodel.add(Dense(64, activation='relu', input_dim=20))\nmodel.add(Dense(10, activation='softmax'))\n\nsgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)\nmodel.compile(loss='categorical_crossentropy',\n              optimizer=sgd,\n              metrics=['accuracy'])\n\nmodel.fit(x_train, y_train,\n          epochs=200,\n          batch_size=128,\n          callbacks=[ModelUpdater()])\n```\n[Here](https://github.com/julvo/reloading/blob/master/examples/keras/train.py) is a full Keras example.\n\n### TensorFlow\n```python\nfor epoch in reloading(range(NB_EPOCHS)):\n    # the code inside this outer loop will be reloaded from source\n    # before each epoch so that you can change it during training\n  \n    train_loss.reset_states()\n    train_accuracy.reset_states()\n    test_loss.reset_states()\n    test_accuracy.reset_states()\n  \n    for images, labels in tqdm(train_ds):\n      train_step(images, labels)\n  \n    for test_images, test_labels in tqdm(test_ds):\n      test_step(test_images, test_labels)\n```\n[Here](https://github.com/julvo/reloading/blob/master/examples/tensorflow/train.py) is a full TensorFlow example.\n\n## Testing\n\nMake sure you have `python` and `python3` available in your path, then run:\n```\n$ python3 reloading/test_reloading.py\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjulvo%2Freloading","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjulvo%2Freloading","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjulvo%2Freloading/lists"}