{"action":"created","comment":{"url":"https://api.github.com/repos/bartvm/blocks/pulls/comments/24300864","id":24300864,"diff_hunk":"@@ -0,0 +1,163 @@\n+from theano.tensor.nnet.conv import conv2d\n+from theano.tensor.signal.downsample import max_pool_2d\n+\n+from blocks.bricks import Initializable, Feedforward, Sequence\n+from blocks.bricks.base import application, Brick, lazy\n+from blocks.roles import add_role, FILTERS\n+from blocks.utils import shared_floatx_zeros\n+\n+\n+class Convolutional(Initializable):\n+    \"\"\"Performs a 2D convolution.\n+\n+    .. todo::\n+\n+       Allow passing of image shapes for faster execution.\n+\n+    Parameters\n+    ----------\n+    filter_size : tuple\n+        The height and width of the filter (also called *kernels*).\n+    num_filters : int\n+        Number of filters per channel.\n+    num_channels : int\n+        Number of input channels in the image. For the first layer this is\n+        normally 1 for grayscale images and 3 for color (RGB) images. For\n+        subsequent layers this is equal to the number of filters output by\n+        the previous convolutional layer. The filters are pooled over the\n+        channels.\n+    step : tuple, optional\n+        The step (or stride) with which to slide the filters over the\n+        image. Defaults to (1, 1).\n+    border_mode : {'valid', 'full'}, optional\n+        The border mode to use, see :func:`scipy.signal.convolve2d` for\n+        details. Defaults to 'valid'.\n+\n+    \"\"\"\n+    has_bias = False\n+\n+    @lazy\n+    def __init__(self, filter_size, num_filters, num_channels,\n+                 step=(1, 1), border_mode='valid', **kwargs):\n+        super(Convolutional, self).__init__(**kwargs)\n+\n+        self.filter_size = filter_size\n+        self.border_mode = border_mode\n+        self.num_filters = num_filters\n+        self.num_channels = num_channels\n+        self.step = step\n+\n+    def _allocate(self):\n+        filter_size_x, filter_size_y = self.filter_size\n+        W = shared_floatx_zeros((self.num_filters, self.num_channels,\n+                                 filter_size_x, filter_size_y), name='W')\n+        add_role(W, FILTERS)\n+        self.params.append(W)\n+        self.add_auxiliary_variable(W.norm(2), name='W_norm')\n+\n+    def _initialize(self):\n+        W, = self.params\n+        self.weights_init.initialize(W, self.rng)\n+\n+    @application(inputs=['input_'], outputs=['output'])\n+    def apply(self, input_):\n+        \"\"\"Perform the convolution.\n+\n+        Parameters\n+        ----------\n+        input_ : :class:`~tensor.TensorVariable`\n+            A 4D tensor with the axes representing batch size, number of\n+            channels, image height, and image width.\n+\n+        Returns\n+        -------\n+        output : :class:`~tensor.TensorVariable`\n+            A 4D tensor of filtered images (feature maps) with dimensions\n+            representing batch size, number of filters, feature map height,\n+            and feature map width.\n+\n+            The height and width of the feature map depend on the border\n+            mode. For 'valid' it is ``image_size - filter_size + 1`` while\n+            for 'full' it is ``image_shape + filter_size - 1``.\n+\n+        \"\"\"\n+        W, = self.params\n+        output = conv2d(\n+            input_, W, subsample=self.step, border_mode=self.border_mode,\n+            filter_shape=(self.num_filters,\n+                          self.num_channels) + self.filter_size)\n+        return output\n+\n+\n+class MaxPooling(Initializable, Feedforward):\n+    \"\"\"Max pooling layer.\n+\n+    Parameters\n+    ----------\n+    pooling_size : tuple\n+        The height and width of the pooling region i.e. this is the factor\n+        by which your input's last two dimensions will be downscaled.\n+    step : tuple, optional\n+        The vertical and horizontal shift (stride) between pooling regions.\n+        By default this is equal to `pooling_size`. Setting this to a lower\n+        number results in overlapping pooling regions.\n+\n+    \"\"\"\n+    @lazy\n+    def __init__(self, pooling_size, step=None, **kwargs):\n+        super(MaxPooling, self).__init__(**kwargs)\n+        self.pooling_size = pooling_size\n+        self.step = step\n+\n+    @application(inputs=['input_'], outputs=['output'])\n+    def apply(self, input_):\n+        \"\"\"Apply the pooling (subsampling) transformation.\n+\n+        Parameters\n+        ----------\n+        input_ : :class:`~tensor.TensorVariable`\n+            An tensor with dimension greater or equal to 2. The last two\n+            dimensions will be downsampled. For example, with images this\n+            means that the last two dimensions should represent the height\n+            and width of your image.\n+\n+        Returns\n+        -------\n+        output : :class:`~tensor.TensorVariable`\n+            A tensor with the same number of dimensions as `input_`, but\n+            with the last two dimensions downsampled.\n+\n+        \"\"\"\n+        output = max_pool_2d(input_, self.pooling_size, st=self.step)\n+        return output\n+\n+\n+class ConvolutionalLayer(Sequence, Initializable):","path":"blocks/bricks/conv.py","position":135,"original_position":135,"commit_id":"fe9b61b5698619e6093a4b1be7c739ba4aa86cca","original_commit_id":"fe9b61b5698619e6093a4b1be7c739ba4aa86cca","user":{"login":"dmitriy-serdyuk","id":2872615,"avatar_url":"https://avatars.githubusercontent.com/u/2872615?v=3","gravatar_id":"","url":"https://api.github.com/users/dmitriy-serdyuk","html_url":"https://github.com/dmitriy-serdyuk","followers_url":"https://api.github.com/users/dmitriy-serdyuk/followers","following_url":"https://api.github.com/users/dmitriy-serdyuk/following{/other_user}","gists_url":"https://api.github.com/users/dmitriy-serdyuk/gists{/gist_id}","starred_url":"https://api.github.com/users/dmitriy-serdyuk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dmitriy-serdyuk/subscriptions","organizations_url":"https://api.github.com/users/dmitriy-serdyuk/orgs","repos_url":"https://api.github.com/users/dmitriy-serdyuk/repos","events_url":"https://api.github.com/users/dmitriy-serdyuk/events{/privacy}","received_events_url":"https://api.github.com/users/dmitriy-serdyuk/received_events","type":"User","site_admin":false},"body":"@bartvm When I was implementing this I thought that it would be a good idea that `ConvolutionalLayer` could compute the output dimension if the input dimension is provided. Do you know an easy way to do it? ","created_at":"2015-02-08T18:27:44Z","updated_at":"2015-02-08T18:27:44Z","html_url":"https://github.com/bartvm/blocks/pull/221#discussion_r24300864","pull_request_url":"https://api.github.com/repos/bartvm/blocks/pulls/221","_links":{"self":{"href":"https://api.github.com/repos/bartvm/blocks/pulls/comments/24300864"},"html":{"href":"https://github.com/bartvm/blocks/pull/221#discussion_r24300864"},"pull_request":{"href":"https://api.github.com/repos/bartvm/blocks/pulls/221"}}},"pull_request":{"url":"https://api.github.com/repos/bartvm/blocks/pulls/221","id":28871012,"html_url":"https://github.com/bartvm/blocks/pull/221","diff_url":"https://github.com/bartvm/blocks/pull/221.diff","patch_url":"https://github.com/bartvm/blocks/pull/221.patch","issue_url":"https://api.github.com/repos/bartvm/blocks/issues/221","number":221,"state":"open","locked":false,"title":"WIP: Convolutional and max pooling bricks","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":"Parts of this have been based of @dmitriy-serdyuk's work for cats and dogs: https://github.com/dmitriy-serdyuk/cats_vs_dogs/blob/master/cats_vs_dogs/bricks.py\r\n\r\nFirst time working with convolutional networks, so figuring things out as I go along. This introduces the basic convolution brick and a max pooling brick.\r\n\r\n- [ ] Add tests\r\n- [x] Combine convolution, pooling and activation into a single brick\r\n- [ ] Combine these bricks into a sequence with an easy interface","created_at":"2015-02-07T22:04:15Z","updated_at":"2015-02-08T18:27:44Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b5ac27da354109db72c844e6345b1aa368c96631","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/bartvm/blocks/pulls/221/commits","review_comments_url":"https://api.github.com/repos/bartvm/blocks/pulls/221/comments","review_comment_url":"https://api.github.com/repos/bartvm/blocks/pulls/comments/{number}","comments_url":"https://api.github.com/repos/bartvm/blocks/issues/221/comments","statuses_url":"https://api.github.com/repos/bartvm/blocks/statuses/fe9b61b5698619e6093a4b1be7c739ba4aa86cca","head":{"label":"bartvm:conv","ref":"conv","sha":"fe9b61b5698619e6093a4b1be7c739ba4aa86cca","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-08T13:47:59Z","pushed_at":"2015-02-08T16:46:02Z","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":13672,"stargazers_count":65,"watchers_count":65,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":19,"mirror_url":null,"open_issues_count":52,"forks":19,"open_issues":52,"watchers":65,"default_branch":"master"}},"base":{"label":"bartvm:master","ref":"master","sha":"9b58079fba7a738b44370eb605b6ab2a7de82f4c","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-08T13:47:59Z","pushed_at":"2015-02-08T16:46:02Z","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":13672,"stargazers_count":65,"watchers_count":65,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":19,"mirror_url":null,"open_issues_count":52,"forks":19,"open_issues":52,"watchers":65,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/bartvm/blocks/pulls/221"},"html":{"href":"https://github.com/bartvm/blocks/pull/221"},"issue":{"href":"https://api.github.com/repos/bartvm/blocks/issues/221"},"comments":{"href":"https://api.github.com/repos/bartvm/blocks/issues/221/comments"},"review_comments":{"href":"https://api.github.com/repos/bartvm/blocks/pulls/221/comments"},"review_comment":{"href":"https://api.github.com/repos/bartvm/blocks/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/bartvm/blocks/pulls/221/commits"},"statuses":{"href":"https://api.github.com/repos/bartvm/blocks/statuses/fe9b61b5698619e6093a4b1be7c739ba4aa86cca"}}}}