{"action":"created","comment":{"url":"https://api.github.com/repos/pytorch/ignite/pulls/comments/206335209","pull_request_review_id":141732525,"id":206335209,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIwNjMzNTIwOQ==","diff_hunk":"@@ -0,0 +1,107 @@\n+# coding: utf-8\n+\n+from enum import Enum\n+import collections\n+\n+from ignite._utils import convert_tensor\n+from ignite.engine import Engine\n+\n+\n+class Tbptt_Events(Enum):\n+    \"\"\"Aditional tbptt events.\n+\n+    Additional events for truncated backpropagation throught time dedicated\n+    trainer.\n+    \"\"\"\n+\n+    TIME_ITERATION_STARTED = \"time_iteration_started\"\n+    TIME_ITERATION_COMPLETED = \"time_iteration_completed\"\n+\n+\n+def _prepare_tbptt_batch(batch, tbptt_step, dim=0, device=None):\n+    \"\"\"Prepare batch for tbptt trainer.\n+\n+    Batch come from the dataloader. It is split in chunks along the time\n+    dimension and fed to the truncated backpropagation throught time trainer.\n+    \"\"\"\n+    x, y = batch\n+    x = convert_tensor(x, device=device)\n+    y = convert_tensor(y, device=device)\n+    return zip(x.split(tbptt_step, dim=dim), y.split(tbptt_step, dim=dim))\n+\n+\n+def _detach_hidden(hidden):\n+    \"\"\"Cut backpropagation graph.\n+\n+    Auxillary function to cut the backpropagation graph by detaching the hidden\n+    vector.\n+    \"\"\"\n+    if isinstance(hidden, collections.Iterable):\n+        return tuple(h.detach() for h in hidden)\n+    else:\n+        return hidden.detach()\n+\n+\n+def create_supervised_tbptt_trainer(\n+    model,\n+    optimizer,\n+    loss_fn,\n+    tbtt_step,\n+    dim=0,\n+    device=None\n+):\n+    \"\"\"\n+    Factory function for creating a trainer for truncated backpropagation\n+    through time supervised models.\n+\n+    Args:\n+        model (`torch.nn.Module`): the model to train\n+        optimizer (`torch.optim.Optimizer`): the optimizer to use\n+        loss_fn (torch.nn loss function): the loss function to use","path":"ignite/contrib/engines.py","position":60,"original_position":60,"commit_id":"ad39c50eed9792976de968a8c362c6354f88c55c","original_commit_id":"ad39c50eed9792976de968a8c362c6354f88c55c","user":{"login":"vfdev-5","id":2459423,"node_id":"MDQ6VXNlcjI0NTk0MjM=","avatar_url":"https://avatars0.githubusercontent.com/u/2459423?v=4","gravatar_id":"","url":"https://api.github.com/users/vfdev-5","html_url":"https://github.com/vfdev-5","followers_url":"https://api.github.com/users/vfdev-5/followers","following_url":"https://api.github.com/users/vfdev-5/following{/other_user}","gists_url":"https://api.github.com/users/vfdev-5/gists{/gist_id}","starred_url":"https://api.github.com/users/vfdev-5/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vfdev-5/subscriptions","organizations_url":"https://api.github.com/users/vfdev-5/orgs","repos_url":"https://api.github.com/users/vfdev-5/repos","events_url":"https://api.github.com/users/vfdev-5/events{/privacy}","received_events_url":"https://api.github.com/users/vfdev-5/received_events","type":"User","site_admin":false},"body":"nit: maybe just a callable ?","created_at":"2018-07-30T21:55:25Z","updated_at":"2018-07-30T21:55:25Z","html_url":"https://github.com/pytorch/ignite/pull/220#discussion_r206335209","pull_request_url":"https://api.github.com/repos/pytorch/ignite/pulls/220","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/pytorch/ignite/pulls/comments/206335209"},"html":{"href":"https://github.com/pytorch/ignite/pull/220#discussion_r206335209"},"pull_request":{"href":"https://api.github.com/repos/pytorch/ignite/pulls/220"}}},"pull_request":{"url":"https://api.github.com/repos/pytorch/ignite/pulls/220","id":204606443,"node_id":"MDExOlB1bGxSZXF1ZXN0MjA0NjA2NDQz","html_url":"https://github.com/pytorch/ignite/pull/220","diff_url":"https://github.com/pytorch/ignite/pull/220.diff","patch_url":"https://github.com/pytorch/ignite/pull/220.patch","issue_url":"https://api.github.com/repos/pytorch/ignite/issues/220","number":220,"state":"open","locked":false,"title":"Custom events and engines","user":{"login":"AntoinePrv","id":11088808,"node_id":"MDQ6VXNlcjExMDg4ODA4","avatar_url":"https://avatars0.githubusercontent.com/u/11088808?v=4","gravatar_id":"","url":"https://api.github.com/users/AntoinePrv","html_url":"https://github.com/AntoinePrv","followers_url":"https://api.github.com/users/AntoinePrv/followers","following_url":"https://api.github.com/users/AntoinePrv/following{/other_user}","gists_url":"https://api.github.com/users/AntoinePrv/gists{/gist_id}","starred_url":"https://api.github.com/users/AntoinePrv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AntoinePrv/subscriptions","organizations_url":"https://api.github.com/users/AntoinePrv/orgs","repos_url":"https://api.github.com/users/AntoinePrv/repos","events_url":"https://api.github.com/users/AntoinePrv/events{/privacy}","received_events_url":"https://api.github.com/users/AntoinePrv/received_events","type":"User","site_admin":false},"body":"This is work in progress for #217 \r\n\r\n# User defined events\r\nI'm opening the PR unfinished to discuss how to actually add user define events given that `Enum` are not mutable.\r\nIn this first commit, I swap the value of `ignite.engine.engine` with a new `Enum` in the function `add_event` from `ignite.contrib.event`.\r\nHowever this does not work in the following case:\r\n\r\n```python\r\nfrom ignite.engine import Events\r\n# or ignite.engine.engine\r\nfrom ignite.contrib.events import add_events\r\n\r\nadd_events(...)\r\n# Or other function that end up adding event\r\n```\r\nThis is because now `Events` is in the namespace of the current module. The `Events` object will not be changed and the user cannot access the new events to add new handles (unless weirdly making imports).\r\n\r\nI suggest three solutions:\r\n  1. Move the `Enum` inside the `Engine` class. Swaping the `Enum` becomes a mutation of the class so works properly.\r\n  2. Use `aenum` package to make `Enum` mutable (+1 package dependency)\r\n  3. Change the `Enum` structure of `Events` for something mutable\r\n\r\nAny opinion/  suggestion welcome.\r\n\r\n# Contrib engine\r\nI added a contrib TBPTT trainer to have a example of creating custom events along the way.\r\n\r\n# TODO\r\n\r\n- [ ] Add unit tests\r\n- [ ] Find a solution to add new `Events` to the original `Enum`\r\n- [ ] Add examples\r\n- [ ] Add reference to TBPTT in docstring","created_at":"2018-07-28T19:01:04Z","updated_at":"2018-07-30T21:55:25Z","closed_at":null,"merged_at":null,"merge_commit_sha":"11510dab359e6cb50ace73bcaeb943fee4f3cb04","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/pytorch/ignite/pulls/220/commits","review_comments_url":"https://api.github.com/repos/pytorch/ignite/pulls/220/comments","review_comment_url":"https://api.github.com/repos/pytorch/ignite/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pytorch/ignite/issues/220/comments","statuses_url":"https://api.github.com/repos/pytorch/ignite/statuses/ad39c50eed9792976de968a8c362c6354f88c55c","head":{"label":"AntoinePrv:custom_events_engines","ref":"custom_events_engines","sha":"ad39c50eed9792976de968a8c362c6354f88c55c","user":{"login":"AntoinePrv","id":11088808,"node_id":"MDQ6VXNlcjExMDg4ODA4","avatar_url":"https://avatars0.githubusercontent.com/u/11088808?v=4","gravatar_id":"","url":"https://api.github.com/users/AntoinePrv","html_url":"https://github.com/AntoinePrv","followers_url":"https://api.github.com/users/AntoinePrv/followers","following_url":"https://api.github.com/users/AntoinePrv/following{/other_user}","gists_url":"https://api.github.com/users/AntoinePrv/gists{/gist_id}","starred_url":"https://api.github.com/users/AntoinePrv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AntoinePrv/subscriptions","organizations_url":"https://api.github.com/users/AntoinePrv/orgs","repos_url":"https://api.github.com/users/AntoinePrv/repos","events_url":"https://api.github.com/users/AntoinePrv/events{/privacy}","received_events_url":"https://api.github.com/users/AntoinePrv/received_events","type":"User","site_admin":false},"repo":{"id":142605664,"node_id":"MDEwOlJlcG9zaXRvcnkxNDI2MDU2NjQ=","name":"ignite","full_name":"AntoinePrv/ignite","owner":{"login":"AntoinePrv","id":11088808,"node_id":"MDQ6VXNlcjExMDg4ODA4","avatar_url":"https://avatars0.githubusercontent.com/u/11088808?v=4","gravatar_id":"","url":"https://api.github.com/users/AntoinePrv","html_url":"https://github.com/AntoinePrv","followers_url":"https://api.github.com/users/AntoinePrv/followers","following_url":"https://api.github.com/users/AntoinePrv/following{/other_user}","gists_url":"https://api.github.com/users/AntoinePrv/gists{/gist_id}","starred_url":"https://api.github.com/users/AntoinePrv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AntoinePrv/subscriptions","organizations_url":"https://api.github.com/users/AntoinePrv/orgs","repos_url":"https://api.github.com/users/AntoinePrv/repos","events_url":"https://api.github.com/users/AntoinePrv/events{/privacy}","received_events_url":"https://api.github.com/users/AntoinePrv/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/AntoinePrv/ignite","description":"High-level library to help with training neural networks in PyTorch","fork":true,"url":"https://api.github.com/repos/AntoinePrv/ignite","forks_url":"https://api.github.com/repos/AntoinePrv/ignite/forks","keys_url":"https://api.github.com/repos/AntoinePrv/ignite/keys{/key_id}","collaborators_url":"https://api.github.com/repos/AntoinePrv/ignite/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/AntoinePrv/ignite/teams","hooks_url":"https://api.github.com/repos/AntoinePrv/ignite/hooks","issue_events_url":"https://api.github.com/repos/AntoinePrv/ignite/issues/events{/number}","events_url":"https://api.github.com/repos/AntoinePrv/ignite/events","assignees_url":"https://api.github.com/repos/AntoinePrv/ignite/assignees{/user}","branches_url":"https://api.github.com/repos/AntoinePrv/ignite/branches{/branch}","tags_url":"https://api.github.com/repos/AntoinePrv/ignite/tags","blobs_url":"https://api.github.com/repos/AntoinePrv/ignite/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/AntoinePrv/ignite/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/AntoinePrv/ignite/git/refs{/sha}","trees_url":"https://api.github.com/repos/AntoinePrv/ignite/git/trees{/sha}","statuses_url":"https://api.github.com/repos/AntoinePrv/ignite/statuses/{sha}","languages_url":"https://api.github.com/repos/AntoinePrv/ignite/languages","stargazers_url":"https://api.github.com/repos/AntoinePrv/ignite/stargazers","contributors_url":"https://api.github.com/repos/AntoinePrv/ignite/contributors","subscribers_url":"https://api.github.com/repos/AntoinePrv/ignite/subscribers","subscription_url":"https://api.github.com/repos/AntoinePrv/ignite/subscription","commits_url":"https://api.github.com/repos/AntoinePrv/ignite/commits{/sha}","git_commits_url":"https://api.github.com/repos/AntoinePrv/ignite/git/commits{/sha}","comments_url":"https://api.github.com/repos/AntoinePrv/ignite/comments{/number}","issue_comment_url":"https://api.github.com/repos/AntoinePrv/ignite/issues/comments{/number}","contents_url":"https://api.github.com/repos/AntoinePrv/ignite/contents/{+path}","compare_url":"https://api.github.com/repos/AntoinePrv/ignite/compare/{base}...{head}","merges_url":"https://api.github.com/repos/AntoinePrv/ignite/merges","archive_url":"https://api.github.com/repos/AntoinePrv/ignite/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/AntoinePrv/ignite/downloads","issues_url":"https://api.github.com/repos/AntoinePrv/ignite/issues{/number}","pulls_url":"https://api.github.com/repos/AntoinePrv/ignite/pulls{/number}","milestones_url":"https://api.github.com/repos/AntoinePrv/ignite/milestones{/number}","notifications_url":"https://api.github.com/repos/AntoinePrv/ignite/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/AntoinePrv/ignite/labels{/name}","releases_url":"https://api.github.com/repos/AntoinePrv/ignite/releases{/id}","deployments_url":"https://api.github.com/repos/AntoinePrv/ignite/deployments","created_at":"2018-07-27T17:29:14Z","updated_at":"2018-07-27T17:29:16Z","pushed_at":"2018-07-30T19:11:38Z","git_url":"git://github.com/AntoinePrv/ignite.git","ssh_url":"git@github.com:AntoinePrv/ignite.git","clone_url":"https://github.com/AntoinePrv/ignite.git","svn_url":"https://github.com/AntoinePrv/ignite","homepage":"https://pytorch.org/ignite","size":1728,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"bsd-3-clause","name":"BSD 3-Clause \"New\" or \"Revised\" License","spdx_id":"BSD-3-Clause","url":"https://api.github.com/licenses/bsd-3-clause","node_id":"MDc6TGljZW5zZTU="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"pytorch:master","ref":"master","sha":"c4563ddc67bee03fc616e87b84abd9b5e17e21cc","user":{"login":"pytorch","id":21003710,"node_id":"MDEyOk9yZ2FuaXphdGlvbjIxMDAzNzEw","avatar_url":"https://avatars3.githubusercontent.com/u/21003710?v=4","gravatar_id":"","url":"https://api.github.com/users/pytorch","html_url":"https://github.com/pytorch","followers_url":"https://api.github.com/users/pytorch/followers","following_url":"https://api.github.com/users/pytorch/following{/other_user}","gists_url":"https://api.github.com/users/pytorch/gists{/gist_id}","starred_url":"https://api.github.com/users/pytorch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pytorch/subscriptions","organizations_url":"https://api.github.com/users/pytorch/orgs","repos_url":"https://api.github.com/users/pytorch/repos","events_url":"https://api.github.com/users/pytorch/events{/privacy}","received_events_url":"https://api.github.com/users/pytorch/received_events","type":"Organization","site_admin":false},"repo":{"id":111835796,"node_id":"MDEwOlJlcG9zaXRvcnkxMTE4MzU3OTY=","name":"ignite","full_name":"pytorch/ignite","owner":{"login":"pytorch","id":21003710,"node_id":"MDEyOk9yZ2FuaXphdGlvbjIxMDAzNzEw","avatar_url":"https://avatars3.githubusercontent.com/u/21003710?v=4","gravatar_id":"","url":"https://api.github.com/users/pytorch","html_url":"https://github.com/pytorch","followers_url":"https://api.github.com/users/pytorch/followers","following_url":"https://api.github.com/users/pytorch/following{/other_user}","gists_url":"https://api.github.com/users/pytorch/gists{/gist_id}","starred_url":"https://api.github.com/users/pytorch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pytorch/subscriptions","organizations_url":"https://api.github.com/users/pytorch/orgs","repos_url":"https://api.github.com/users/pytorch/repos","events_url":"https://api.github.com/users/pytorch/events{/privacy}","received_events_url":"https://api.github.com/users/pytorch/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/pytorch/ignite","description":"High-level library to help with training neural networks in PyTorch","fork":false,"url":"https://api.github.com/repos/pytorch/ignite","forks_url":"https://api.github.com/repos/pytorch/ignite/forks","keys_url":"https://api.github.com/repos/pytorch/ignite/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pytorch/ignite/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pytorch/ignite/teams","hooks_url":"https://api.github.com/repos/pytorch/ignite/hooks","issue_events_url":"https://api.github.com/repos/pytorch/ignite/issues/events{/number}","events_url":"https://api.github.com/repos/pytorch/ignite/events","assignees_url":"https://api.github.com/repos/pytorch/ignite/assignees{/user}","branches_url":"https://api.github.com/repos/pytorch/ignite/branches{/branch}","tags_url":"https://api.github.com/repos/pytorch/ignite/tags","blobs_url":"https://api.github.com/repos/pytorch/ignite/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pytorch/ignite/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pytorch/ignite/git/refs{/sha}","trees_url":"https://api.github.com/repos/pytorch/ignite/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pytorch/ignite/statuses/{sha}","languages_url":"https://api.github.com/repos/pytorch/ignite/languages","stargazers_url":"https://api.github.com/repos/pytorch/ignite/stargazers","contributors_url":"https://api.github.com/repos/pytorch/ignite/contributors","subscribers_url":"https://api.github.com/repos/pytorch/ignite/subscribers","subscription_url":"https://api.github.com/repos/pytorch/ignite/subscription","commits_url":"https://api.github.com/repos/pytorch/ignite/commits{/sha}","git_commits_url":"https://api.github.com/repos/pytorch/ignite/git/commits{/sha}","comments_url":"https://api.github.com/repos/pytorch/ignite/comments{/number}","issue_comment_url":"https://api.github.com/repos/pytorch/ignite/issues/comments{/number}","contents_url":"https://api.github.com/repos/pytorch/ignite/contents/{+path}","compare_url":"https://api.github.com/repos/pytorch/ignite/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pytorch/ignite/merges","archive_url":"https://api.github.com/repos/pytorch/ignite/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pytorch/ignite/downloads","issues_url":"https://api.github.com/repos/pytorch/ignite/issues{/number}","pulls_url":"https://api.github.com/repos/pytorch/ignite/pulls{/number}","milestones_url":"https://api.github.com/repos/pytorch/ignite/milestones{/number}","notifications_url":"https://api.github.com/repos/pytorch/ignite/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pytorch/ignite/labels{/name}","releases_url":"https://api.github.com/repos/pytorch/ignite/releases{/id}","deployments_url":"https://api.github.com/repos/pytorch/ignite/deployments","created_at":"2017-11-23T17:31:21Z","updated_at":"2018-07-30T13:41:51Z","pushed_at":"2018-07-30T21:53:30Z","git_url":"git://github.com/pytorch/ignite.git","ssh_url":"git@github.com:pytorch/ignite.git","clone_url":"https://github.com/pytorch/ignite.git","svn_url":"https://github.com/pytorch/ignite","homepage":"https://pytorch.org/ignite","size":1722,"stargazers_count":574,"watchers_count":574,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":59,"mirror_url":null,"archived":false,"open_issues_count":14,"license":{"key":"bsd-3-clause","name":"BSD 3-Clause \"New\" or \"Revised\" License","spdx_id":"BSD-3-Clause","url":"https://api.github.com/licenses/bsd-3-clause","node_id":"MDc6TGljZW5zZTU="},"forks":59,"open_issues":14,"watchers":574,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pytorch/ignite/pulls/220"},"html":{"href":"https://github.com/pytorch/ignite/pull/220"},"issue":{"href":"https://api.github.com/repos/pytorch/ignite/issues/220"},"comments":{"href":"https://api.github.com/repos/pytorch/ignite/issues/220/comments"},"review_comments":{"href":"https://api.github.com/repos/pytorch/ignite/pulls/220/comments"},"review_comment":{"href":"https://api.github.com/repos/pytorch/ignite/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pytorch/ignite/pulls/220/commits"},"statuses":{"href":"https://api.github.com/repos/pytorch/ignite/statuses/ad39c50eed9792976de968a8c362c6354f88c55c"}},"author_association":"NONE"}}
{"actor":{"display_login":"vfdev-5"}}