{"action":"created","comment":{"url":"https://api.github.com/repos/bartvm/blocks/pulls/comments/23898248","id":23898248,"diff_hunk":"@@ -319,3 +320,84 @@ def apply_noise(graph, variables, level, seed=None):\n         replace[variable] = (variable +\n                              rng.normal(variable.shape, std=level))\n     return graph.replace(replace)\n+\n+\n+def collect_parameters(graph, params):\n+    \"\"\"Replace parameters with a single shared variable.\n+\n+    This can be useful if you need to calculate the full Hessian of a\n+    computational graph. It replaces parameters with slices of a single\n+    large vectors like\n+\n+    >>> from blocks.utils import shared_floatx\n+    >>> W1 = shared_floatx(numpy.random.rand(10, 10))\n+    >>> W2 = shared_floatx(numpy.random.rand(10, 10))\n+    >>> all_params = shared_floatx(numpy.concatenate(\n+    ...     [W1.get_value().flatten(), W2.get_value().flatten()]))\n+    >>> W1 = all_params[:W1.size]\n+    >>> W2 = all_params[W1.size:]\n+\n+    Parameters\n+    ----------\n+    graph : :class:`ComputationGraph` instance\n+        The managed Theano graph in which to collect parameters.\n+    params : list of Theano shared variables\n+        The parameters whose values should be collected.\n+\n+    Returns\n+    -------\n+    ComputationGraph instance\n+        A new Theano graph which has all the given parameters collected\n+        into a single large shared variable.\n+\n+    Notes\n+    -----\n+    Note that this replacement makes the training of the model\n+    significantly slower because of the large amount of Theano's\n+    ``set_subtensor`` calls needed to train the model.\n+\n+    Examples\n+    --------\n+    >>> from blocks.bricks import MLP, Sigmoid\n+    >>> from blocks.bricks.cost import SquaredError\n+    >>> from theano import tensor\n+    >>> x = tensor.matrix()\n+    >>> mlp = MLP(activations=[Sigmoid(), Sigmoid()], dims=[784, 100, 784])\n+    >>> cost = SquaredError().apply(x, mlp.apply(x))\n+    >>> cg = ComputationGraph(cost)\n+    >>> new_cg = collect_parameters(cg, cg.shared_variables)\n+\n+    The new graph only has a single shared variable.\n+\n+    >>> new_cg.shared_variables\n+    [collected_params]\n+\n+    The bricks' variables have been replaced with reshaped segments of this\n+    single shared variable.\n+\n+    >>> from blocks.filter import VariableFilter\n+    >>> from blocks.roles import PARAMETER\n+    >>> var_filter = VariableFilter(roles=[PARAMETER])\n+    >>> var_filter(new_cg.variables) # doctest: +SKIP\n+    [Reshape{1}.0, Reshape{1}.0, Reshape{2}.0, Reshape{2}.0]\n+\n+    \"\"\"\n+    param_values, param_sizes, param_shapes = [], [], []\n+    for param in params:\n+        param_values.append(param.get_value(borrow=True))\n+        param_sizes.append(param_values[-1].size)\n+        param_shapes.append(param_values[-1].shape)\n+\n+    new_params = shared_floatx_zeros(sum(param_sizes))\n+    new_params.set_value(numpy.concatenate([value.flatten()\n+                                            for value in param_values]))\n+    new_params.name = 'collected_params'","path":"blocks/graph.py","position":92,"original_position":92,"commit_id":"bb7921916a0567d8f96714121338ee2801ec0e4a","original_commit_id":"bb7921916a0567d8f96714121338ee2801ec0e4a","user":{"login":"bartvm","id":2299595,"avatar_url":"https://avatars.githubusercontent.com/u/2299595?v=3","gravatar_id":"","url":"https://api.github.com/users/bartvm","html_url":"https://github.com/bartvm","followers_url":"https://api.github.com/users/bartvm/followers","following_url":"https://api.github.com/users/bartvm/following{/other_user}","gists_url":"https://api.github.com/users/bartvm/gists{/gist_id}","starred_url":"https://api.github.com/users/bartvm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bartvm/subscriptions","organizations_url":"https://api.github.com/users/bartvm/orgs","repos_url":"https://api.github.com/users/bartvm/repos","events_url":"https://api.github.com/users/bartvm/events{/privacy}","received_events_url":"https://api.github.com/users/bartvm/received_events","type":"User","site_admin":false},"body":"Right, that's where it gets tricky, and it really all depends on the semantics we want to use. Instead I give the `PARAMETER` roles to the reshaped slices of this shared variable (see line 401). I don't want to do both, because otherwise filtering by `PARAMETER` would give you each parameter twice.\r\n\r\nI think the current behavior probably makes most sense, because you can still filter by `WEIGHTS` and `BIASES` and retrieve the correct tensor variables, even if they aren't shared anymore. If you want to e.g. noise the weight matrices afterwards, you can still use `apply_noise(new_cg, VariableFilter(role=[WEIGHTS])(new_cg.variables))`.\r\n\r\nThis does mean that you can't rely on a `PARAMETER` variable being shared, but we already said that this was the case (for my hypothetical case at the time where the weight matrix is the transpose of another).","created_at":"2015-02-01T15:23:37Z","updated_at":"2015-02-01T15:23:37Z","html_url":"https://github.com/bartvm/blocks/pull/175#discussion_r23898248","pull_request_url":"https://api.github.com/repos/bartvm/blocks/pulls/175","_links":{"self":{"href":"https://api.github.com/repos/bartvm/blocks/pulls/comments/23898248"},"html":{"href":"https://github.com/bartvm/blocks/pull/175#discussion_r23898248"},"pull_request":{"href":"https://api.github.com/repos/bartvm/blocks/pulls/175"}}},"pull_request":{"url":"https://api.github.com/repos/bartvm/blocks/pulls/175","id":28328841,"html_url":"https://github.com/bartvm/blocks/pull/175","diff_url":"https://github.com/bartvm/blocks/pull/175.diff","patch_url":"https://github.com/bartvm/blocks/pull/175.patch","issue_url":"https://api.github.com/repos/bartvm/blocks/issues/175","number":175,"state":"open","locked":false,"title":"WIP: Reintroduce parameter collection","user":{"login":"bartvm","id":2299595,"avatar_url":"https://avatars.githubusercontent.com/u/2299595?v=3","gravatar_id":"","url":"https://api.github.com/users/bartvm","html_url":"https://github.com/bartvm","followers_url":"https://api.github.com/users/bartvm/followers","following_url":"https://api.github.com/users/bartvm/following{/other_user}","gists_url":"https://api.github.com/users/bartvm/gists{/gist_id}","starred_url":"https://api.github.com/users/bartvm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bartvm/subscriptions","organizations_url":"https://api.github.com/users/bartvm/orgs","repos_url":"https://api.github.com/users/bartvm/repos","events_url":"https://api.github.com/users/bartvm/events{/privacy}","received_events_url":"https://api.github.com/users/bartvm/received_events","type":"User","site_admin":false},"body":"- [x] Write tests\r\n- [ ] Move graph modifications like `collect_parameters` and `apply_noise` to a separate file?","created_at":"2015-01-30T00:35:41Z","updated_at":"2015-02-01T15:23:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4826c70cf77075a48749d3f36ed094483cc62964","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/bartvm/blocks/pulls/175/commits","review_comments_url":"https://api.github.com/repos/bartvm/blocks/pulls/175/comments","review_comment_url":"https://api.github.com/repos/bartvm/blocks/pulls/comments/{number}","comments_url":"https://api.github.com/repos/bartvm/blocks/issues/175/comments","statuses_url":"https://api.github.com/repos/bartvm/blocks/statuses/bb7921916a0567d8f96714121338ee2801ec0e4a","head":{"label":"bartvm:collect","ref":"collect","sha":"bb7921916a0567d8f96714121338ee2801ec0e4a","user":{"login":"bartvm","id":2299595,"avatar_url":"https://avatars.githubusercontent.com/u/2299595?v=3","gravatar_id":"","url":"https://api.github.com/users/bartvm","html_url":"https://github.com/bartvm","followers_url":"https://api.github.com/users/bartvm/followers","following_url":"https://api.github.com/users/bartvm/following{/other_user}","gists_url":"https://api.github.com/users/bartvm/gists{/gist_id}","starred_url":"https://api.github.com/users/bartvm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bartvm/subscriptions","organizations_url":"https://api.github.com/users/bartvm/orgs","repos_url":"https://api.github.com/users/bartvm/repos","events_url":"https://api.github.com/users/bartvm/events{/privacy}","received_events_url":"https://api.github.com/users/bartvm/received_events","type":"User","site_admin":false},"repo":{"id":24829371,"name":"blocks","full_name":"bartvm/blocks","owner":{"login":"bartvm","id":2299595,"avatar_url":"https://avatars.githubusercontent.com/u/2299595?v=3","gravatar_id":"","url":"https://api.github.com/users/bartvm","html_url":"https://github.com/bartvm","followers_url":"https://api.github.com/users/bartvm/followers","following_url":"https://api.github.com/users/bartvm/following{/other_user}","gists_url":"https://api.github.com/users/bartvm/gists{/gist_id}","starred_url":"https://api.github.com/users/bartvm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bartvm/subscriptions","organizations_url":"https://api.github.com/users/bartvm/orgs","repos_url":"https://api.github.com/users/bartvm/repos","events_url":"https://api.github.com/users/bartvm/events{/privacy}","received_events_url":"https://api.github.com/users/bartvm/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bartvm/blocks","description":"A Theano framework for building and training neural networks","fork":false,"url":"https://api.github.com/repos/bartvm/blocks","forks_url":"https://api.github.com/repos/bartvm/blocks/forks","keys_url":"https://api.github.com/repos/bartvm/blocks/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bartvm/blocks/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bartvm/blocks/teams","hooks_url":"https://api.github.com/repos/bartvm/blocks/hooks","issue_events_url":"https://api.github.com/repos/bartvm/blocks/issues/events{/number}","events_url":"https://api.github.com/repos/bartvm/blocks/events","assignees_url":"https://api.github.com/repos/bartvm/blocks/assignees{/user}","branches_url":"https://api.github.com/repos/bartvm/blocks/branches{/branch}","tags_url":"https://api.github.com/repos/bartvm/blocks/tags","blobs_url":"https://api.github.com/repos/bartvm/blocks/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bartvm/blocks/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bartvm/blocks/git/refs{/sha}","trees_url":"https://api.github.com/repos/bartvm/blocks/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bartvm/blocks/statuses/{sha}","languages_url":"https://api.github.com/repos/bartvm/blocks/languages","stargazers_url":"https://api.github.com/repos/bartvm/blocks/stargazers","contributors_url":"https://api.github.com/repos/bartvm/blocks/contributors","subscribers_url":"https://api.github.com/repos/bartvm/blocks/subscribers","subscription_url":"https://api.github.com/repos/bartvm/blocks/subscription","commits_url":"https://api.github.com/repos/bartvm/blocks/commits{/sha}","git_commits_url":"https://api.github.com/repos/bartvm/blocks/git/commits{/sha}","comments_url":"https://api.github.com/repos/bartvm/blocks/comments{/number}","issue_comment_url":"https://api.github.com/repos/bartvm/blocks/issues/comments/{number}","contents_url":"https://api.github.com/repos/bartvm/blocks/contents/{+path}","compare_url":"https://api.github.com/repos/bartvm/blocks/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bartvm/blocks/merges","archive_url":"https://api.github.com/repos/bartvm/blocks/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bartvm/blocks/downloads","issues_url":"https://api.github.com/repos/bartvm/blocks/issues{/number}","pulls_url":"https://api.github.com/repos/bartvm/blocks/pulls{/number}","milestones_url":"https://api.github.com/repos/bartvm/blocks/milestones{/number}","notifications_url":"https://api.github.com/repos/bartvm/blocks/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bartvm/blocks/labels{/name}","releases_url":"https://api.github.com/repos/bartvm/blocks/releases{/id}","created_at":"2014-10-06T00:08:32Z","updated_at":"2015-02-01T15:09:59Z","pushed_at":"2015-02-01T15:09:59Z","git_url":"git://github.com/bartvm/blocks.git","ssh_url":"git@github.com:bartvm/blocks.git","clone_url":"https://github.com/bartvm/blocks.git","svn_url":"https://github.com/bartvm/blocks","homepage":"","size":5815,"stargazers_count":28,"watchers_count":28,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":16,"mirror_url":null,"open_issues_count":48,"forks":16,"open_issues":48,"watchers":28,"default_branch":"master"}},"base":{"label":"bartvm:master","ref":"master","sha":"d3c85c782390724b1b4b8ef7395563841be598c9","user":{"login":"bartvm","id":2299595,"avatar_url":"https://avatars.githubusercontent.com/u/2299595?v=3","gravatar_id":"","url":"https://api.github.com/users/bartvm","html_url":"https://github.com/bartvm","followers_url":"https://api.github.com/users/bartvm/followers","following_url":"https://api.github.com/users/bartvm/following{/other_user}","gists_url":"https://api.github.com/users/bartvm/gists{/gist_id}","starred_url":"https://api.github.com/users/bartvm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bartvm/subscriptions","organizations_url":"https://api.github.com/users/bartvm/orgs","repos_url":"https://api.github.com/users/bartvm/repos","events_url":"https://api.github.com/users/bartvm/events{/privacy}","received_events_url":"https://api.github.com/users/bartvm/received_events","type":"User","site_admin":false},"repo":{"id":24829371,"name":"blocks","full_name":"bartvm/blocks","owner":{"login":"bartvm","id":2299595,"avatar_url":"https://avatars.githubusercontent.com/u/2299595?v=3","gravatar_id":"","url":"https://api.github.com/users/bartvm","html_url":"https://github.com/bartvm","followers_url":"https://api.github.com/users/bartvm/followers","following_url":"https://api.github.com/users/bartvm/following{/other_user}","gists_url":"https://api.github.com/users/bartvm/gists{/gist_id}","starred_url":"https://api.github.com/users/bartvm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bartvm/subscriptions","organizations_url":"https://api.github.com/users/bartvm/orgs","repos_url":"https://api.github.com/users/bartvm/repos","events_url":"https://api.github.com/users/bartvm/events{/privacy}","received_events_url":"https://api.github.com/users/bartvm/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bartvm/blocks","description":"A Theano framework for building and training neural networks","fork":false,"url":"https://api.github.com/repos/bartvm/blocks","forks_url":"https://api.github.com/repos/bartvm/blocks/forks","keys_url":"https://api.github.com/repos/bartvm/blocks/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bartvm/blocks/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bartvm/blocks/teams","hooks_url":"https://api.github.com/repos/bartvm/blocks/hooks","issue_events_url":"https://api.github.com/repos/bartvm/blocks/issues/events{/number}","events_url":"https://api.github.com/repos/bartvm/blocks/events","assignees_url":"https://api.github.com/repos/bartvm/blocks/assignees{/user}","branches_url":"https://api.github.com/repos/bartvm/blocks/branches{/branch}","tags_url":"https://api.github.com/repos/bartvm/blocks/tags","blobs_url":"https://api.github.com/repos/bartvm/blocks/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bartvm/blocks/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bartvm/blocks/git/refs{/sha}","trees_url":"https://api.github.com/repos/bartvm/blocks/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bartvm/blocks/statuses/{sha}","languages_url":"https://api.github.com/repos/bartvm/blocks/languages","stargazers_url":"https://api.github.com/repos/bartvm/blocks/stargazers","contributors_url":"https://api.github.com/repos/bartvm/blocks/contributors","subscribers_url":"https://api.github.com/repos/bartvm/blocks/subscribers","subscription_url":"https://api.github.com/repos/bartvm/blocks/subscription","commits_url":"https://api.github.com/repos/bartvm/blocks/commits{/sha}","git_commits_url":"https://api.github.com/repos/bartvm/blocks/git/commits{/sha}","comments_url":"https://api.github.com/repos/bartvm/blocks/comments{/number}","issue_comment_url":"https://api.github.com/repos/bartvm/blocks/issues/comments/{number}","contents_url":"https://api.github.com/repos/bartvm/blocks/contents/{+path}","compare_url":"https://api.github.com/repos/bartvm/blocks/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bartvm/blocks/merges","archive_url":"https://api.github.com/repos/bartvm/blocks/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bartvm/blocks/downloads","issues_url":"https://api.github.com/repos/bartvm/blocks/issues{/number}","pulls_url":"https://api.github.com/repos/bartvm/blocks/pulls{/number}","milestones_url":"https://api.github.com/repos/bartvm/blocks/milestones{/number}","notifications_url":"https://api.github.com/repos/bartvm/blocks/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bartvm/blocks/labels{/name}","releases_url":"https://api.github.com/repos/bartvm/blocks/releases{/id}","created_at":"2014-10-06T00:08:32Z","updated_at":"2015-02-01T15:09:59Z","pushed_at":"2015-02-01T15:09:59Z","git_url":"git://github.com/bartvm/blocks.git","ssh_url":"git@github.com:bartvm/blocks.git","clone_url":"https://github.com/bartvm/blocks.git","svn_url":"https://github.com/bartvm/blocks","homepage":"","size":5815,"stargazers_count":28,"watchers_count":28,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":16,"mirror_url":null,"open_issues_count":48,"forks":16,"open_issues":48,"watchers":28,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/bartvm/blocks/pulls/175"},"html":{"href":"https://github.com/bartvm/blocks/pull/175"},"issue":{"href":"https://api.github.com/repos/bartvm/blocks/issues/175"},"comments":{"href":"https://api.github.com/repos/bartvm/blocks/issues/175/comments"},"review_comments":{"href":"https://api.github.com/repos/bartvm/blocks/pulls/175/comments"},"review_comment":{"href":"https://api.github.com/repos/bartvm/blocks/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/bartvm/blocks/pulls/175/commits"},"statuses":{"href":"https://api.github.com/repos/bartvm/blocks/statuses/bb7921916a0567d8f96714121338ee2801ec0e4a"}}}}