{"action":"created","comment":{"url":"https://api.github.com/repos/Theano/Theano/pulls/comments/39895535","id":39895535,"diff_hunk":"@@ -0,0 +1,57 @@\n+import theano\n+from theano.scalar import Composite\n+from theano.scalar import add, sub, true_div, mul\n+\n+\n+class BNComposite(Composite):\n+\n+    def __init__(self, dtype):\n+        x = theano.scalar.Scalar(dtype=dtype).make_variable()\n+        mean = theano.scalar.Scalar(dtype=dtype).make_variable()\n+        std = theano.scalar.Scalar(dtype=dtype).make_variable()\n+        gamma = theano.scalar.Scalar(dtype=dtype).make_variable()\n+        beta = theano.scalar.Scalar(dtype=dtype).make_variable()\n+        o = add(mul(true_div(sub(x, mean), std), gamma), beta)\n+        inputs = [x, mean, std, gamma, beta]\n+        outputs = [o]\n+        super(BNComposite, self).__init__(inputs, outputs)\n+\n+    def grad(self, inps, grads):\n+        x, mean, std, gamma, beta = inps\n+        top, = grads\n+        dx = (top * gamma) / std\n+        dmean = -(top * gamma) / std\n+        dstd = -(top * gamma * (x - mean)) / (std * std)\n+        dgamma = top * (x - mean) / std\n+        return [dx, dmean, dstd, dgamma, top]\n+\n+\n+def batch_normalization(inputs, gamma, beta, mean, std):\n+    \"\"\"\n+    This function will build the symbolic graph for applying batch normalization\n+    to a set of activations. As no intermediate representations are stored for the\n+    back-propagation, this implementation lower the memory usage, however,\n+    it is 5-10% slower than a naive theano implementation, as it redo\n+    some foward computations for the backprop.","path":"theano/tensor/nnet/bn.py","position":35,"original_position":35,"commit_id":"b536fab483b8f9b120fe9d50277550f18a4ac70d","original_commit_id":"b536fab483b8f9b120fe9d50277550f18a4ac70d","user":{"login":"nouiz","id":180987,"avatar_url":"https://avatars.githubusercontent.com/u/180987?v=3","gravatar_id":"","url":"https://api.github.com/users/nouiz","html_url":"https://github.com/nouiz","followers_url":"https://api.github.com/users/nouiz/followers","following_url":"https://api.github.com/users/nouiz/following{/other_user}","gists_url":"https://api.github.com/users/nouiz/gists{/gist_id}","starred_url":"https://api.github.com/users/nouiz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nouiz/subscriptions","organizations_url":"https://api.github.com/users/nouiz/orgs","repos_url":"https://api.github.com/users/nouiz/repos","events_url":"https://api.github.com/users/nouiz/events{/privacy}","received_events_url":"https://api.github.com/users/nouiz/received_events","type":"User","site_admin":false},"body":"This PR is a continuation of the idea in lasagne comment. It is the same, a big Composite with a grad implemented. We changed how to implement the grad as it was easier/faster to implement that way.\r\n\r\nThe speed difference I timmed on a matrix of size 128,256 on the GPU was that the big op was 7% to 10% then the full graph during training. But it was using less memory.\r\n\r\nAs the quantity of memory on the GPU is a big bottleneck, I would default to the version that use less memory as it significatively less memory.\r\n\r\nThere won't be optimization that introduce it manually. If someone add it, it should not be enabled by default, but only with the low_mem tag is used.\r\n\r\nPersonnaly, as this version use much less memory, I think people would prefer that even if there is a small speed loos. If you think differently, tell it.\r\n\r\n@ballasn didn't remart the speed different on a small model on mnist.","created_at":"2015-09-18T20:08:58Z","updated_at":"2015-09-18T20:08:58Z","html_url":"https://github.com/Theano/Theano/pull/3410#discussion_r39895535","pull_request_url":"https://api.github.com/repos/Theano/Theano/pulls/3410","_links":{"self":{"href":"https://api.github.com/repos/Theano/Theano/pulls/comments/39895535"},"html":{"href":"https://github.com/Theano/Theano/pull/3410#discussion_r39895535"},"pull_request":{"href":"https://api.github.com/repos/Theano/Theano/pulls/3410"}}},"pull_request":{"url":"https://api.github.com/repos/Theano/Theano/pulls/3410","id":45402575,"html_url":"https://github.com/Theano/Theano/pull/3410","diff_url":"https://github.com/Theano/Theano/pull/3410.diff","patch_url":"https://github.com/Theano/Theano/pull/3410.patch","issue_url":"https://api.github.com/repos/Theano/Theano/issues/3410","number":3410,"state":"open","locked":false,"title":"Add batch normalization op","user":{"login":"ballasn","id":7395449,"avatar_url":"https://avatars.githubusercontent.com/u/7395449?v=3","gravatar_id":"","url":"https://api.github.com/users/ballasn","html_url":"https://github.com/ballasn","followers_url":"https://api.github.com/users/ballasn/followers","following_url":"https://api.github.com/users/ballasn/following{/other_user}","gists_url":"https://api.github.com/users/ballasn/gists{/gist_id}","starred_url":"https://api.github.com/users/ballasn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ballasn/subscriptions","organizations_url":"https://api.github.com/users/ballasn/orgs","repos_url":"https://api.github.com/users/ballasn/repos","events_url":"https://api.github.com/users/ballasn/events{/privacy}","received_events_url":"https://api.github.com/users/ballasn/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-09-18T12:51:13Z","updated_at":"2015-09-18T20:08:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0590e6ed61ea01319c304b292ee4610b670b54a1","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Theano/Theano/pulls/3410/commits","review_comments_url":"https://api.github.com/repos/Theano/Theano/pulls/3410/comments","review_comment_url":"https://api.github.com/repos/Theano/Theano/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Theano/Theano/issues/3410/comments","statuses_url":"https://api.github.com/repos/Theano/Theano/statuses/b536fab483b8f9b120fe9d50277550f18a4ac70d","head":{"label":"ballasn:batchnormalization","ref":"batchnormalization","sha":"b536fab483b8f9b120fe9d50277550f18a4ac70d","user":{"login":"ballasn","id":7395449,"avatar_url":"https://avatars.githubusercontent.com/u/7395449?v=3","gravatar_id":"","url":"https://api.github.com/users/ballasn","html_url":"https://github.com/ballasn","followers_url":"https://api.github.com/users/ballasn/followers","following_url":"https://api.github.com/users/ballasn/following{/other_user}","gists_url":"https://api.github.com/users/ballasn/gists{/gist_id}","starred_url":"https://api.github.com/users/ballasn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ballasn/subscriptions","organizations_url":"https://api.github.com/users/ballasn/orgs","repos_url":"https://api.github.com/users/ballasn/repos","events_url":"https://api.github.com/users/ballasn/events{/privacy}","received_events_url":"https://api.github.com/users/ballasn/received_events","type":"User","site_admin":false},"repo":{"id":21708564,"name":"Theano","full_name":"ballasn/Theano","owner":{"login":"ballasn","id":7395449,"avatar_url":"https://avatars.githubusercontent.com/u/7395449?v=3","gravatar_id":"","url":"https://api.github.com/users/ballasn","html_url":"https://github.com/ballasn","followers_url":"https://api.github.com/users/ballasn/followers","following_url":"https://api.github.com/users/ballasn/following{/other_user}","gists_url":"https://api.github.com/users/ballasn/gists{/gist_id}","starred_url":"https://api.github.com/users/ballasn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ballasn/subscriptions","organizations_url":"https://api.github.com/users/ballasn/orgs","repos_url":"https://api.github.com/users/ballasn/repos","events_url":"https://api.github.com/users/ballasn/events{/privacy}","received_events_url":"https://api.github.com/users/ballasn/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ballasn/Theano","description":"Optimizing GPU-meta-programming code generating array oriented optimizing math compiler in Python","fork":true,"url":"https://api.github.com/repos/ballasn/Theano","forks_url":"https://api.github.com/repos/ballasn/Theano/forks","keys_url":"https://api.github.com/repos/ballasn/Theano/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ballasn/Theano/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ballasn/Theano/teams","hooks_url":"https://api.github.com/repos/ballasn/Theano/hooks","issue_events_url":"https://api.github.com/repos/ballasn/Theano/issues/events{/number}","events_url":"https://api.github.com/repos/ballasn/Theano/events","assignees_url":"https://api.github.com/repos/ballasn/Theano/assignees{/user}","branches_url":"https://api.github.com/repos/ballasn/Theano/branches{/branch}","tags_url":"https://api.github.com/repos/ballasn/Theano/tags","blobs_url":"https://api.github.com/repos/ballasn/Theano/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ballasn/Theano/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ballasn/Theano/git/refs{/sha}","trees_url":"https://api.github.com/repos/ballasn/Theano/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ballasn/Theano/statuses/{sha}","languages_url":"https://api.github.com/repos/ballasn/Theano/languages","stargazers_url":"https://api.github.com/repos/ballasn/Theano/stargazers","contributors_url":"https://api.github.com/repos/ballasn/Theano/contributors","subscribers_url":"https://api.github.com/repos/ballasn/Theano/subscribers","subscription_url":"https://api.github.com/repos/ballasn/Theano/subscription","commits_url":"https://api.github.com/repos/ballasn/Theano/commits{/sha}","git_commits_url":"https://api.github.com/repos/ballasn/Theano/git/commits{/sha}","comments_url":"https://api.github.com/repos/ballasn/Theano/comments{/number}","issue_comment_url":"https://api.github.com/repos/ballasn/Theano/issues/comments{/number}","contents_url":"https://api.github.com/repos/ballasn/Theano/contents/{+path}","compare_url":"https://api.github.com/repos/ballasn/Theano/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ballasn/Theano/merges","archive_url":"https://api.github.com/repos/ballasn/Theano/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ballasn/Theano/downloads","issues_url":"https://api.github.com/repos/ballasn/Theano/issues{/number}","pulls_url":"https://api.github.com/repos/ballasn/Theano/pulls{/number}","milestones_url":"https://api.github.com/repos/ballasn/Theano/milestones{/number}","notifications_url":"https://api.github.com/repos/ballasn/Theano/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ballasn/Theano/labels{/name}","releases_url":"https://api.github.com/repos/ballasn/Theano/releases{/id}","created_at":"2014-07-10T20:08:29Z","updated_at":"2015-04-24T18:59:18Z","pushed_at":"2015-09-18T16:20:59Z","git_url":"git://github.com/ballasn/Theano.git","ssh_url":"git@github.com:ballasn/Theano.git","clone_url":"https://github.com/ballasn/Theano.git","svn_url":"https://github.com/ballasn/Theano","homepage":"http://www.deeplearning.net/software/theano","size":37437,"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":"9b10aaeebb14f78d075fdff6a5f6ac6ecd5516ce","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}","created_at":"2011-08-10T03:48:06Z","updated_at":"2015-09-18T19:47:30Z","pushed_at":"2015-09-18T20:04:47Z","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":63523,"stargazers_count":2038,"watchers_count":2038,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":720,"mirror_url":null,"open_issues_count":474,"forks":720,"open_issues":474,"watchers":2038,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Theano/Theano/pulls/3410"},"html":{"href":"https://github.com/Theano/Theano/pull/3410"},"issue":{"href":"https://api.github.com/repos/Theano/Theano/issues/3410"},"comments":{"href":"https://api.github.com/repos/Theano/Theano/issues/3410/comments"},"review_comments":{"href":"https://api.github.com/repos/Theano/Theano/pulls/3410/comments"},"review_comment":{"href":"https://api.github.com/repos/Theano/Theano/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Theano/Theano/pulls/3410/commits"},"statuses":{"href":"https://api.github.com/repos/Theano/Theano/statuses/b536fab483b8f9b120fe9d50277550f18a4ac70d"}}}}