{"action":"created","comment":{"url":"https://api.github.com/repos/Theano/Theano/pulls/comments/51598713","id":51598713,"diff_hunk":"@@ -3231,6 +3233,124 @@ def local_adv_sub1_adv_inc_sub1(node):\n     return [r]\n \n \n+@register_canonicalize\n+@register_stabilize\n+@gof.local_optimizer([AdvancedSubtensor])\n+def local_adv_sub(node):\n+    \"\"\"Optimize the AdvancedSubtensor with advanced indexing.\n+\n+    \"\"\"\n+    if not isinstance(node.op, AdvancedSubtensor):\n+        return\n+    x = node.inputs[0]\n+    idx = node.inputs[1:]\n+\n+    assert len(idx) >= x.ndim\n+    dims = len(idx)\n+    #step 1: find smallest index\n+    for k, i in enumerate(idx):\n+        if isinstance(i.type, T.TensorType):\n+            start = k\n+            break\n+    for k, i in enumerate(idx[::-1]):\n+        if isinstance(i.type, T.TensorType):\n+            end = len(idx) - k\n+            break\n+    #step 2: transpose\n+    def get_indices(a, b, ind):\n+        \"\"\"\n+        Get real indices for a list of indices excluding `None`.\n+\n+        \"\"\"\n+\n+        s_idx = []\n+        n_idx = []\n+        for i in range(a, b):\n+            if isinstance(ind[i].type, NoneTypeT):\n+                n_idx.append(i)\n+            if isinstance(ind[i].type, SliceType):\n+                s_idx.append(i)\n+        p = a\n+        v_idx = []\n+        v_s_idx = []\n+        if s_idx or n_idx:\n+            for i in range(a, b):\n+                if i in s_idx:\n+                    v_s_idx.append(p)\n+                    p += 1\n+                    continue\n+                if i in n_idx:\n+                    continue\n+                else:\n+                    v_idx.append(p)\n+                    p += 1\n+        else:\n+            v_idx = range(a, b)\n+        return n_idx, s_idx, v_idx, v_s_idx\n+\n+    def get_dim_indices(a, b, idx1, idx2):\n+        \"\"\"\n+        Get dimshuffle information for a list of indices.\n+\n+        \"\"\"\n+\n+        idx = []\n+        k = 0\n+        for i in range(a, b):\n+            if i in idx1:\n+                idx.append('x')\n+            else:\n+                idx.append(idx2[k])\n+                k += 1\n+        return idx\n+\n+    nx_idx, s_idx, va_idx, va_s_idx = get_indices(start, end, idx)\n+    pre_nx_idx, pre_s_idx, _, pre_va_s_idx = get_indices(0, start, idx)\n+    post_nx_idx, post_s_idx, _, post_va_s_idx = get_indices(end, len(idx),\n+                                                            idx)\n+    va_dim_idx = ['x'] * len(nx_idx) + va_idx + va_s_idx\n+    pre_dim_idx = get_dim_indices(0, start, pre_nx_idx, pre_s_idx)\n+    post_dim_idx = get_dim_indices(end, len(idx), post_nx_idx, post_s_idx)\n+\n+    #get complete dimshuffle information\n+    dimshuffle_idx = pre_dim_idx + va_dim_idx + post_dim_idx\n+    x = x.dimshuffle(*dimshuffle_idx)\n+    end_va = end - len(va_s_idx)\n+    start_va = start + len(nx_idx)\n+\n+    #step 3: partial flattening\n+    shape = x.shape[: start_va] +\n+            (T.prod(x.shape[start_va: end_va]),) +\n+            x.shape[end_va:]","path":"theano/tensor/opt.py","position":108,"original_position":108,"commit_id":"fd2b86567301afca1a30b41ca64593c167eafa4f","original_commit_id":"fd2b86567301afca1a30b41ca64593c167eafa4f","user":{"login":"lamblin","id":178249,"avatar_url":"https://avatars.githubusercontent.com/u/178249?v=3","gravatar_id":"","url":"https://api.github.com/users/lamblin","html_url":"https://github.com/lamblin","followers_url":"https://api.github.com/users/lamblin/followers","following_url":"https://api.github.com/users/lamblin/following{/other_user}","gists_url":"https://api.github.com/users/lamblin/gists{/gist_id}","starred_url":"https://api.github.com/users/lamblin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lamblin/subscriptions","organizations_url":"https://api.github.com/users/lamblin/orgs","repos_url":"https://api.github.com/users/lamblin/repos","events_url":"https://api.github.com/users/lamblin/events{/privacy}","received_events_url":"https://api.github.com/users/lamblin/received_events","type":"User","site_admin":false},"body":"You need parentheses around this expression if you want to use multiple lines","created_at":"2016-02-02T17:03:15Z","updated_at":"2016-02-02T17:03:15Z","html_url":"https://github.com/Theano/Theano/pull/3975#discussion_r51598713","pull_request_url":"https://api.github.com/repos/Theano/Theano/pulls/3975","_links":{"self":{"href":"https://api.github.com/repos/Theano/Theano/pulls/comments/51598713"},"html":{"href":"https://github.com/Theano/Theano/pull/3975#discussion_r51598713"},"pull_request":{"href":"https://api.github.com/repos/Theano/Theano/pulls/3975"}}},"pull_request":{"url":"https://api.github.com/repos/Theano/Theano/pulls/3975","id":57942180,"html_url":"https://github.com/Theano/Theano/pull/3975","diff_url":"https://github.com/Theano/Theano/pull/3975.diff","patch_url":"https://github.com/Theano/Theano/pull/3975.patch","issue_url":"https://api.github.com/repos/Theano/Theano/issues/3975","number":3975,"state":"open","locked":false,"title":"optimize advancedsubtensor","user":{"login":"yingzha","id":8920116,"avatar_url":"https://avatars.githubusercontent.com/u/8920116?v=3","gravatar_id":"","url":"https://api.github.com/users/yingzha","html_url":"https://github.com/yingzha","followers_url":"https://api.github.com/users/yingzha/followers","following_url":"https://api.github.com/users/yingzha/following{/other_user}","gists_url":"https://api.github.com/users/yingzha/gists{/gist_id}","starred_url":"https://api.github.com/users/yingzha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yingzha/subscriptions","organizations_url":"https://api.github.com/users/yingzha/orgs","repos_url":"https://api.github.com/users/yingzha/repos","events_url":"https://api.github.com/users/yingzha/events{/privacy}","received_events_url":"https://api.github.com/users/yingzha/received_events","type":"User","site_admin":false},"body":"Fixes #3166.\r\n@abergeron, I didn't push the test function for this optimization since when I tested it locally, it said optimization failed and I'm not sure which part is going wrong.\r\nI did not remove the annotation to make it a bit clearer. Could you take a look on the function and notify me where the problem is?\r\nThanks!\r\n- [x] Optimization function\r\n- [ ]  Test function for it","created_at":"2016-02-02T05:24:59Z","updated_at":"2016-02-02T17:03:15Z","closed_at":null,"merged_at":null,"merge_commit_sha":"42d9166b4e761b5571e9ed0c591e05a896042c76","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Theano/Theano/pulls/3975/commits","review_comments_url":"https://api.github.com/repos/Theano/Theano/pulls/3975/comments","review_comment_url":"https://api.github.com/repos/Theano/Theano/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Theano/Theano/issues/3975/comments","statuses_url":"https://api.github.com/repos/Theano/Theano/statuses/fd2b86567301afca1a30b41ca64593c167eafa4f","head":{"label":"yingzha:ccw","ref":"ccw","sha":"fd2b86567301afca1a30b41ca64593c167eafa4f","user":{"login":"yingzha","id":8920116,"avatar_url":"https://avatars.githubusercontent.com/u/8920116?v=3","gravatar_id":"","url":"https://api.github.com/users/yingzha","html_url":"https://github.com/yingzha","followers_url":"https://api.github.com/users/yingzha/followers","following_url":"https://api.github.com/users/yingzha/following{/other_user}","gists_url":"https://api.github.com/users/yingzha/gists{/gist_id}","starred_url":"https://api.github.com/users/yingzha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yingzha/subscriptions","organizations_url":"https://api.github.com/users/yingzha/orgs","repos_url":"https://api.github.com/users/yingzha/repos","events_url":"https://api.github.com/users/yingzha/events{/privacy}","received_events_url":"https://api.github.com/users/yingzha/received_events","type":"User","site_admin":false},"repo":{"id":26595449,"name":"Theano","full_name":"yingzha/Theano","owner":{"login":"yingzha","id":8920116,"avatar_url":"https://avatars.githubusercontent.com/u/8920116?v=3","gravatar_id":"","url":"https://api.github.com/users/yingzha","html_url":"https://github.com/yingzha","followers_url":"https://api.github.com/users/yingzha/followers","following_url":"https://api.github.com/users/yingzha/following{/other_user}","gists_url":"https://api.github.com/users/yingzha/gists{/gist_id}","starred_url":"https://api.github.com/users/yingzha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yingzha/subscriptions","organizations_url":"https://api.github.com/users/yingzha/orgs","repos_url":"https://api.github.com/users/yingzha/repos","events_url":"https://api.github.com/users/yingzha/events{/privacy}","received_events_url":"https://api.github.com/users/yingzha/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/yingzha/Theano","description":"Optimizing GPU-meta-programming code generating array oriented optimizing math compiler in Python","fork":true,"url":"https://api.github.com/repos/yingzha/Theano","forks_url":"https://api.github.com/repos/yingzha/Theano/forks","keys_url":"https://api.github.com/repos/yingzha/Theano/keys{/key_id}","collaborators_url":"https://api.github.com/repos/yingzha/Theano/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/yingzha/Theano/teams","hooks_url":"https://api.github.com/repos/yingzha/Theano/hooks","issue_events_url":"https://api.github.com/repos/yingzha/Theano/issues/events{/number}","events_url":"https://api.github.com/repos/yingzha/Theano/events","assignees_url":"https://api.github.com/repos/yingzha/Theano/assignees{/user}","branches_url":"https://api.github.com/repos/yingzha/Theano/branches{/branch}","tags_url":"https://api.github.com/repos/yingzha/Theano/tags","blobs_url":"https://api.github.com/repos/yingzha/Theano/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/yingzha/Theano/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/yingzha/Theano/git/refs{/sha}","trees_url":"https://api.github.com/repos/yingzha/Theano/git/trees{/sha}","statuses_url":"https://api.github.com/repos/yingzha/Theano/statuses/{sha}","languages_url":"https://api.github.com/repos/yingzha/Theano/languages","stargazers_url":"https://api.github.com/repos/yingzha/Theano/stargazers","contributors_url":"https://api.github.com/repos/yingzha/Theano/contributors","subscribers_url":"https://api.github.com/repos/yingzha/Theano/subscribers","subscription_url":"https://api.github.com/repos/yingzha/Theano/subscription","commits_url":"https://api.github.com/repos/yingzha/Theano/commits{/sha}","git_commits_url":"https://api.github.com/repos/yingzha/Theano/git/commits{/sha}","comments_url":"https://api.github.com/repos/yingzha/Theano/comments{/number}","issue_comment_url":"https://api.github.com/repos/yingzha/Theano/issues/comments{/number}","contents_url":"https://api.github.com/repos/yingzha/Theano/contents/{+path}","compare_url":"https://api.github.com/repos/yingzha/Theano/compare/{base}...{head}","merges_url":"https://api.github.com/repos/yingzha/Theano/merges","archive_url":"https://api.github.com/repos/yingzha/Theano/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/yingzha/Theano/downloads","issues_url":"https://api.github.com/repos/yingzha/Theano/issues{/number}","pulls_url":"https://api.github.com/repos/yingzha/Theano/pulls{/number}","milestones_url":"https://api.github.com/repos/yingzha/Theano/milestones{/number}","notifications_url":"https://api.github.com/repos/yingzha/Theano/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/yingzha/Theano/labels{/name}","releases_url":"https://api.github.com/repos/yingzha/Theano/releases{/id}","deployments_url":"https://api.github.com/repos/yingzha/Theano/deployments","created_at":"2014-11-13T15:57:34Z","updated_at":"2016-01-14T16:18:18Z","pushed_at":"2016-02-02T05:10:19Z","git_url":"git://github.com/yingzha/Theano.git","ssh_url":"git@github.com:yingzha/Theano.git","clone_url":"https://github.com/yingzha/Theano.git","svn_url":"https://github.com/yingzha/Theano","homepage":"http://www.deeplearning.net/software/theano","size":47804,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"Theano:master","ref":"master","sha":"cef892bbac7037c6a043e0748bfcc2babbcdad42","user":{"login":"Theano","id":970517,"avatar_url":"https://avatars.githubusercontent.com/u/970517?v=3","gravatar_id":"","url":"https://api.github.com/users/Theano","html_url":"https://github.com/Theano","followers_url":"https://api.github.com/users/Theano/followers","following_url":"https://api.github.com/users/Theano/following{/other_user}","gists_url":"https://api.github.com/users/Theano/gists{/gist_id}","starred_url":"https://api.github.com/users/Theano/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Theano/subscriptions","organizations_url":"https://api.github.com/users/Theano/orgs","repos_url":"https://api.github.com/users/Theano/repos","events_url":"https://api.github.com/users/Theano/events{/privacy}","received_events_url":"https://api.github.com/users/Theano/received_events","type":"Organization","site_admin":false},"repo":{"id":2183193,"name":"Theano","full_name":"Theano/Theano","owner":{"login":"Theano","id":970517,"avatar_url":"https://avatars.githubusercontent.com/u/970517?v=3","gravatar_id":"","url":"https://api.github.com/users/Theano","html_url":"https://github.com/Theano","followers_url":"https://api.github.com/users/Theano/followers","following_url":"https://api.github.com/users/Theano/following{/other_user}","gists_url":"https://api.github.com/users/Theano/gists{/gist_id}","starred_url":"https://api.github.com/users/Theano/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Theano/subscriptions","organizations_url":"https://api.github.com/users/Theano/orgs","repos_url":"https://api.github.com/users/Theano/repos","events_url":"https://api.github.com/users/Theano/events{/privacy}","received_events_url":"https://api.github.com/users/Theano/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Theano/Theano","description":"Theano is a Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently. It can use GPUs and perform efficient symbolic differentiation.","fork":false,"url":"https://api.github.com/repos/Theano/Theano","forks_url":"https://api.github.com/repos/Theano/Theano/forks","keys_url":"https://api.github.com/repos/Theano/Theano/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Theano/Theano/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Theano/Theano/teams","hooks_url":"https://api.github.com/repos/Theano/Theano/hooks","issue_events_url":"https://api.github.com/repos/Theano/Theano/issues/events{/number}","events_url":"https://api.github.com/repos/Theano/Theano/events","assignees_url":"https://api.github.com/repos/Theano/Theano/assignees{/user}","branches_url":"https://api.github.com/repos/Theano/Theano/branches{/branch}","tags_url":"https://api.github.com/repos/Theano/Theano/tags","blobs_url":"https://api.github.com/repos/Theano/Theano/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Theano/Theano/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Theano/Theano/git/refs{/sha}","trees_url":"https://api.github.com/repos/Theano/Theano/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Theano/Theano/statuses/{sha}","languages_url":"https://api.github.com/repos/Theano/Theano/languages","stargazers_url":"https://api.github.com/repos/Theano/Theano/stargazers","contributors_url":"https://api.github.com/repos/Theano/Theano/contributors","subscribers_url":"https://api.github.com/repos/Theano/Theano/subscribers","subscription_url":"https://api.github.com/repos/Theano/Theano/subscription","commits_url":"https://api.github.com/repos/Theano/Theano/commits{/sha}","git_commits_url":"https://api.github.com/repos/Theano/Theano/git/commits{/sha}","comments_url":"https://api.github.com/repos/Theano/Theano/comments{/number}","issue_comment_url":"https://api.github.com/repos/Theano/Theano/issues/comments{/number}","contents_url":"https://api.github.com/repos/Theano/Theano/contents/{+path}","compare_url":"https://api.github.com/repos/Theano/Theano/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Theano/Theano/merges","archive_url":"https://api.github.com/repos/Theano/Theano/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Theano/Theano/downloads","issues_url":"https://api.github.com/repos/Theano/Theano/issues{/number}","pulls_url":"https://api.github.com/repos/Theano/Theano/pulls{/number}","milestones_url":"https://api.github.com/repos/Theano/Theano/milestones{/number}","notifications_url":"https://api.github.com/repos/Theano/Theano/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Theano/Theano/labels{/name}","releases_url":"https://api.github.com/repos/Theano/Theano/releases{/id}","deployments_url":"https://api.github.com/repos/Theano/Theano/deployments","created_at":"2011-08-10T03:48:06Z","updated_at":"2016-02-02T16:41:15Z","pushed_at":"2016-02-02T17:00:29Z","git_url":"git://github.com/Theano/Theano.git","ssh_url":"git@github.com:Theano/Theano.git","clone_url":"https://github.com/Theano/Theano.git","svn_url":"https://github.com/Theano/Theano","homepage":"http://www.deeplearning.net/software/theano","size":56310,"stargazers_count":2911,"watchers_count":2911,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1048,"mirror_url":null,"open_issues_count":584,"forks":1048,"open_issues":584,"watchers":2911,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Theano/Theano/pulls/3975"},"html":{"href":"https://github.com/Theano/Theano/pull/3975"},"issue":{"href":"https://api.github.com/repos/Theano/Theano/issues/3975"},"comments":{"href":"https://api.github.com/repos/Theano/Theano/issues/3975/comments"},"review_comments":{"href":"https://api.github.com/repos/Theano/Theano/pulls/3975/comments"},"review_comment":{"href":"https://api.github.com/repos/Theano/Theano/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Theano/Theano/pulls/3975/commits"},"statuses":{"href":"https://api.github.com/repos/Theano/Theano/statuses/fd2b86567301afca1a30b41ca64593c167eafa4f"}}}}