{"action":"created","comment":{"url":"https://api.github.com/repos/pfnet/chainer/pulls/comments/35471301","id":35471301,"diff_hunk":"@@ -0,0 +1,285 @@\n+import math\n+\n+import numpy\n+\n+from chainer import cuda\n+from chainer import function\n+from chainer.utils import array\n+from chainer.utils import type_check\n+\n+\n+class TensorNetwork(function.Function):\n+\n+    def __init__(self, left_size, right_size, out_size, nobias=False,\n+                 initialW=None, initial_bias=None):\n+\n+        self.W = None\n+        self.gW = None\n+        self.V1 = None\n+        self.gV1 = None\n+        self.V2 = None\n+        self.gV2 = None\n+        self.b = None\n+        self.gb = None\n+\n+        self.in_sizes = (left_size, right_size)\n+        self.nobias = nobias\n+\n+        if initialW is not None:\n+            assert initialW.shape == (\n+                self.in_sizes[0], self.in_sizes[1], out_size)\n+            self.W = initialW\n+        else:\n+            # TODO(Kenta OONO): I do not know appropriate way of\n+            # initializing weights in tensor network.\n+            # This initialization is a modification of\n+            # that of Linear function.\n+            in_size = numpy.prod(self.in_sizes)\n+            self.W = numpy.random.normal(\n+                0, math.sqrt(1. / in_size),\n+                (self.in_sizes[0], self.in_sizes[1], out_size)\n+            ).astype(numpy.float32)\n+\n+        if not self.nobias:\n+            if initial_bias is not None:\n+                assert len(initial_bias) == 3\n+                assert initial_bias[0].shape == (self.in_sizes[0], out_size)\n+                assert initial_bias[1].shape == (self.in_sizes[1], out_size)\n+                assert initial_bias[2].shape == (out_size,)\n+                self.V1, self.V2, self.b = initial_bias\n+            else:\n+                self.V1 = numpy.zeros(\n+                    (self.in_sizes[0], out_size), dtype=numpy.float32)\n+                self.V2 = numpy.zeros(\n+                    (self.in_sizes[1], out_size), dtype=numpy.float32)\n+                self.b = numpy.zeros((out_size, ), dtype=numpy.float32)\n+\n+        self.gW = array.empty_like(self.W)\n+        if not self.nobias:\n+            self.gV1 = array.empty_like(self.V1)\n+            self.gV2 = array.empty_like(self.V2)\n+            self.gb = array.empty_like(self.b)\n+\n+    @property\n+    def parameter_names(self):\n+        if self.nobias:\n+            return 'W',\n+        assert self.V1 is not None\n+        assert self.V2 is not None\n+        assert self.b is not None\n+        return 'W', 'V1', 'V2', 'b'\n+\n+    @property\n+    def gradient_names(self):\n+        if self.nobias:\n+            return 'gW',\n+        assert self.gV1 is not None\n+        assert self.gV2 is not None\n+        assert self.gb is not None\n+        return 'gW', 'gV1', 'gV2', 'gb'\n+\n+    def check_type_forward(self, in_types):\n+        type_check.expect(in_types.size() == 2)\n+\n+        e1_type, e2_type = in_types\n+        type_check.expect(\n+            e1_type.ndim >= 2,\n+            e2_type.ndim >= 2,\n+            e1_type.shape[0] == e2_type.shape[0]\n+        )\n+\n+        in_sizes = type_check.Variable(self.in_sizes, 'in_sizes')\n+        type_check_prod = type_check.Variable(numpy.prod, 'prod')\n+        type_check.expect(\n+            type_check_prod(e1_type.shape[1:]) == in_sizes[0],\n+            type_check_prod(e2_type.shape[1:]) == in_sizes[1]\n+        )\n+\n+    def zero_grads(self):\n+        self.gW.fill(0)\n+        if not self.nobias:\n+            self.gV1.fill(0)\n+            self.gV2.fill(0)\n+            self.gb.fill(0)\n+\n+    def forward_cpu(self, x):\n+        e1 = array.as_mat(x[0])\n+        e2 = array.as_mat(x[1])\n+        y = numpy.einsum('ij,ik,jkl->il', e1, e2, self.W)\n+        if not self.nobias:\n+            y += e1.dot(self.V1)\n+            y += e2.dot(self.V2)\n+            y += self.b\n+        return y,\n+\n+    def forward_gpu(self, x):\n+        i_len, j_len = x[0].shape","path":"chainer/functions/tensor_network.py","position":null,"original_position":116,"commit_id":"357dfe6f86ce8259db632e9e5d27b68c4a00cb2c","original_commit_id":"f962021d3a4bc8f00702f9b4dbad7bb4f3a8db72","user":{"login":"delta2323","id":407052,"avatar_url":"https://avatars.githubusercontent.com/u/407052?v=3","gravatar_id":"","url":"https://api.github.com/users/delta2323","html_url":"https://github.com/delta2323","followers_url":"https://api.github.com/users/delta2323/followers","following_url":"https://api.github.com/users/delta2323/following{/other_user}","gists_url":"https://api.github.com/users/delta2323/gists{/gist_id}","starred_url":"https://api.github.com/users/delta2323/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/delta2323/subscriptions","organizations_url":"https://api.github.com/users/delta2323/orgs","repos_url":"https://api.github.com/users/delta2323/repos","events_url":"https://api.github.com/users/delta2323/events{/privacy}","received_events_url":"https://api.github.com/users/delta2323/received_events","type":"User","site_admin":false},"body":"I found `TensorNetwork` did work in the case `x[0].ndim >= 3`. So I fixed the code.","created_at":"2015-07-24T22:20:34Z","updated_at":"2015-07-24T22:20:34Z","html_url":"https://github.com/pfnet/chainer/pull/213#discussion_r35471301","pull_request_url":"https://api.github.com/repos/pfnet/chainer/pulls/213","_links":{"self":{"href":"https://api.github.com/repos/pfnet/chainer/pulls/comments/35471301"},"html":{"href":"https://github.com/pfnet/chainer/pull/213#discussion_r35471301"},"pull_request":{"href":"https://api.github.com/repos/pfnet/chainer/pulls/213"}}},"pull_request":{"url":"https://api.github.com/repos/pfnet/chainer/pulls/213","id":40202799,"html_url":"https://github.com/pfnet/chainer/pull/213","diff_url":"https://github.com/pfnet/chainer/pull/213.diff","patch_url":"https://github.com/pfnet/chainer/pull/213.patch","issue_url":"https://api.github.com/repos/pfnet/chainer/issues/213","number":213,"state":"open","locked":false,"title":"[WIP] Neural tensor network","user":{"login":"delta2323","id":407052,"avatar_url":"https://avatars.githubusercontent.com/u/407052?v=3","gravatar_id":"","url":"https://api.github.com/users/delta2323","html_url":"https://github.com/delta2323","followers_url":"https://api.github.com/users/delta2323/followers","following_url":"https://api.github.com/users/delta2323/following{/other_user}","gists_url":"https://api.github.com/users/delta2323/gists{/gist_id}","starred_url":"https://api.github.com/users/delta2323/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/delta2323/subscriptions","organizations_url":"https://api.github.com/users/delta2323/orgs","repos_url":"https://api.github.com/users/delta2323/repos","events_url":"https://api.github.com/users/delta2323/events{/privacy}","received_events_url":"https://api.github.com/users/delta2323/received_events","type":"User","site_admin":false},"body":"This PR implements Neural Tensor Network, which is issued as #171 \r\n\r\n* [ ] Specify order of indices of tensors\r\n* [ ] Write its document\r\n* [x] Refactor unittest","created_at":"2015-07-17T06:10:33Z","updated_at":"2015-07-24T22:20:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"dd294f85a9c2da478d77e9e4b879c2c84b1869d8","assignee":{"login":"unnonouno","id":740680,"avatar_url":"https://avatars.githubusercontent.com/u/740680?v=3","gravatar_id":"","url":"https://api.github.com/users/unnonouno","html_url":"https://github.com/unnonouno","followers_url":"https://api.github.com/users/unnonouno/followers","following_url":"https://api.github.com/users/unnonouno/following{/other_user}","gists_url":"https://api.github.com/users/unnonouno/gists{/gist_id}","starred_url":"https://api.github.com/users/unnonouno/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/unnonouno/subscriptions","organizations_url":"https://api.github.com/users/unnonouno/orgs","repos_url":"https://api.github.com/users/unnonouno/repos","events_url":"https://api.github.com/users/unnonouno/events{/privacy}","received_events_url":"https://api.github.com/users/unnonouno/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/pfnet/chainer/pulls/213/commits","review_comments_url":"https://api.github.com/repos/pfnet/chainer/pulls/213/comments","review_comment_url":"https://api.github.com/repos/pfnet/chainer/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pfnet/chainer/issues/213/comments","statuses_url":"https://api.github.com/repos/pfnet/chainer/statuses/357dfe6f86ce8259db632e9e5d27b68c4a00cb2c","head":{"label":"pfnet:neural_tensor_network","ref":"neural_tensor_network","sha":"357dfe6f86ce8259db632e9e5d27b68c4a00cb2c","user":{"login":"pfnet","id":9739572,"avatar_url":"https://avatars.githubusercontent.com/u/9739572?v=3","gravatar_id":"","url":"https://api.github.com/users/pfnet","html_url":"https://github.com/pfnet","followers_url":"https://api.github.com/users/pfnet/followers","following_url":"https://api.github.com/users/pfnet/following{/other_user}","gists_url":"https://api.github.com/users/pfnet/gists{/gist_id}","starred_url":"https://api.github.com/users/pfnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pfnet/subscriptions","organizations_url":"https://api.github.com/users/pfnet/orgs","repos_url":"https://api.github.com/users/pfnet/repos","events_url":"https://api.github.com/users/pfnet/events{/privacy}","received_events_url":"https://api.github.com/users/pfnet/received_events","type":"Organization","site_admin":false},"repo":{"id":36914478,"name":"chainer","full_name":"pfnet/chainer","owner":{"login":"pfnet","id":9739572,"avatar_url":"https://avatars.githubusercontent.com/u/9739572?v=3","gravatar_id":"","url":"https://api.github.com/users/pfnet","html_url":"https://github.com/pfnet","followers_url":"https://api.github.com/users/pfnet/followers","following_url":"https://api.github.com/users/pfnet/following{/other_user}","gists_url":"https://api.github.com/users/pfnet/gists{/gist_id}","starred_url":"https://api.github.com/users/pfnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pfnet/subscriptions","organizations_url":"https://api.github.com/users/pfnet/orgs","repos_url":"https://api.github.com/users/pfnet/repos","events_url":"https://api.github.com/users/pfnet/events{/privacy}","received_events_url":"https://api.github.com/users/pfnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/pfnet/chainer","description":"A flexible framework of neural networks for deep learning","fork":false,"url":"https://api.github.com/repos/pfnet/chainer","forks_url":"https://api.github.com/repos/pfnet/chainer/forks","keys_url":"https://api.github.com/repos/pfnet/chainer/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pfnet/chainer/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pfnet/chainer/teams","hooks_url":"https://api.github.com/repos/pfnet/chainer/hooks","issue_events_url":"https://api.github.com/repos/pfnet/chainer/issues/events{/number}","events_url":"https://api.github.com/repos/pfnet/chainer/events","assignees_url":"https://api.github.com/repos/pfnet/chainer/assignees{/user}","branches_url":"https://api.github.com/repos/pfnet/chainer/branches{/branch}","tags_url":"https://api.github.com/repos/pfnet/chainer/tags","blobs_url":"https://api.github.com/repos/pfnet/chainer/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pfnet/chainer/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pfnet/chainer/git/refs{/sha}","trees_url":"https://api.github.com/repos/pfnet/chainer/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pfnet/chainer/statuses/{sha}","languages_url":"https://api.github.com/repos/pfnet/chainer/languages","stargazers_url":"https://api.github.com/repos/pfnet/chainer/stargazers","contributors_url":"https://api.github.com/repos/pfnet/chainer/contributors","subscribers_url":"https://api.github.com/repos/pfnet/chainer/subscribers","subscription_url":"https://api.github.com/repos/pfnet/chainer/subscription","commits_url":"https://api.github.com/repos/pfnet/chainer/commits{/sha}","git_commits_url":"https://api.github.com/repos/pfnet/chainer/git/commits{/sha}","comments_url":"https://api.github.com/repos/pfnet/chainer/comments{/number}","issue_comment_url":"https://api.github.com/repos/pfnet/chainer/issues/comments{/number}","contents_url":"https://api.github.com/repos/pfnet/chainer/contents/{+path}","compare_url":"https://api.github.com/repos/pfnet/chainer/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pfnet/chainer/merges","archive_url":"https://api.github.com/repos/pfnet/chainer/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pfnet/chainer/downloads","issues_url":"https://api.github.com/repos/pfnet/chainer/issues{/number}","pulls_url":"https://api.github.com/repos/pfnet/chainer/pulls{/number}","milestones_url":"https://api.github.com/repos/pfnet/chainer/milestones{/number}","notifications_url":"https://api.github.com/repos/pfnet/chainer/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pfnet/chainer/labels{/name}","releases_url":"https://api.github.com/repos/pfnet/chainer/releases{/id}","created_at":"2015-06-05T05:50:37Z","updated_at":"2015-07-24T01:48:02Z","pushed_at":"2015-07-24T22:15:15Z","git_url":"git://github.com/pfnet/chainer.git","ssh_url":"git@github.com:pfnet/chainer.git","clone_url":"https://github.com/pfnet/chainer.git","svn_url":"https://github.com/pfnet/chainer","homepage":"http://chainer.org","size":10001,"stargazers_count":572,"watchers_count":572,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":80,"mirror_url":null,"open_issues_count":57,"forks":80,"open_issues":57,"watchers":572,"default_branch":"master"}},"base":{"label":"pfnet:master","ref":"master","sha":"195b65c1645c51545efde574bb9acf7b73e80780","user":{"login":"pfnet","id":9739572,"avatar_url":"https://avatars.githubusercontent.com/u/9739572?v=3","gravatar_id":"","url":"https://api.github.com/users/pfnet","html_url":"https://github.com/pfnet","followers_url":"https://api.github.com/users/pfnet/followers","following_url":"https://api.github.com/users/pfnet/following{/other_user}","gists_url":"https://api.github.com/users/pfnet/gists{/gist_id}","starred_url":"https://api.github.com/users/pfnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pfnet/subscriptions","organizations_url":"https://api.github.com/users/pfnet/orgs","repos_url":"https://api.github.com/users/pfnet/repos","events_url":"https://api.github.com/users/pfnet/events{/privacy}","received_events_url":"https://api.github.com/users/pfnet/received_events","type":"Organization","site_admin":false},"repo":{"id":36914478,"name":"chainer","full_name":"pfnet/chainer","owner":{"login":"pfnet","id":9739572,"avatar_url":"https://avatars.githubusercontent.com/u/9739572?v=3","gravatar_id":"","url":"https://api.github.com/users/pfnet","html_url":"https://github.com/pfnet","followers_url":"https://api.github.com/users/pfnet/followers","following_url":"https://api.github.com/users/pfnet/following{/other_user}","gists_url":"https://api.github.com/users/pfnet/gists{/gist_id}","starred_url":"https://api.github.com/users/pfnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pfnet/subscriptions","organizations_url":"https://api.github.com/users/pfnet/orgs","repos_url":"https://api.github.com/users/pfnet/repos","events_url":"https://api.github.com/users/pfnet/events{/privacy}","received_events_url":"https://api.github.com/users/pfnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/pfnet/chainer","description":"A flexible framework of neural networks for deep learning","fork":false,"url":"https://api.github.com/repos/pfnet/chainer","forks_url":"https://api.github.com/repos/pfnet/chainer/forks","keys_url":"https://api.github.com/repos/pfnet/chainer/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pfnet/chainer/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pfnet/chainer/teams","hooks_url":"https://api.github.com/repos/pfnet/chainer/hooks","issue_events_url":"https://api.github.com/repos/pfnet/chainer/issues/events{/number}","events_url":"https://api.github.com/repos/pfnet/chainer/events","assignees_url":"https://api.github.com/repos/pfnet/chainer/assignees{/user}","branches_url":"https://api.github.com/repos/pfnet/chainer/branches{/branch}","tags_url":"https://api.github.com/repos/pfnet/chainer/tags","blobs_url":"https://api.github.com/repos/pfnet/chainer/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pfnet/chainer/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pfnet/chainer/git/refs{/sha}","trees_url":"https://api.github.com/repos/pfnet/chainer/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pfnet/chainer/statuses/{sha}","languages_url":"https://api.github.com/repos/pfnet/chainer/languages","stargazers_url":"https://api.github.com/repos/pfnet/chainer/stargazers","contributors_url":"https://api.github.com/repos/pfnet/chainer/contributors","subscribers_url":"https://api.github.com/repos/pfnet/chainer/subscribers","subscription_url":"https://api.github.com/repos/pfnet/chainer/subscription","commits_url":"https://api.github.com/repos/pfnet/chainer/commits{/sha}","git_commits_url":"https://api.github.com/repos/pfnet/chainer/git/commits{/sha}","comments_url":"https://api.github.com/repos/pfnet/chainer/comments{/number}","issue_comment_url":"https://api.github.com/repos/pfnet/chainer/issues/comments{/number}","contents_url":"https://api.github.com/repos/pfnet/chainer/contents/{+path}","compare_url":"https://api.github.com/repos/pfnet/chainer/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pfnet/chainer/merges","archive_url":"https://api.github.com/repos/pfnet/chainer/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pfnet/chainer/downloads","issues_url":"https://api.github.com/repos/pfnet/chainer/issues{/number}","pulls_url":"https://api.github.com/repos/pfnet/chainer/pulls{/number}","milestones_url":"https://api.github.com/repos/pfnet/chainer/milestones{/number}","notifications_url":"https://api.github.com/repos/pfnet/chainer/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pfnet/chainer/labels{/name}","releases_url":"https://api.github.com/repos/pfnet/chainer/releases{/id}","created_at":"2015-06-05T05:50:37Z","updated_at":"2015-07-24T01:48:02Z","pushed_at":"2015-07-24T22:15:15Z","git_url":"git://github.com/pfnet/chainer.git","ssh_url":"git@github.com:pfnet/chainer.git","clone_url":"https://github.com/pfnet/chainer.git","svn_url":"https://github.com/pfnet/chainer","homepage":"http://chainer.org","size":10001,"stargazers_count":572,"watchers_count":572,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":80,"mirror_url":null,"open_issues_count":57,"forks":80,"open_issues":57,"watchers":572,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pfnet/chainer/pulls/213"},"html":{"href":"https://github.com/pfnet/chainer/pull/213"},"issue":{"href":"https://api.github.com/repos/pfnet/chainer/issues/213"},"comments":{"href":"https://api.github.com/repos/pfnet/chainer/issues/213/comments"},"review_comments":{"href":"https://api.github.com/repos/pfnet/chainer/pulls/213/comments"},"review_comment":{"href":"https://api.github.com/repos/pfnet/chainer/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pfnet/chainer/pulls/213/commits"},"statuses":{"href":"https://api.github.com/repos/pfnet/chainer/statuses/357dfe6f86ce8259db632e9e5d27b68c4a00cb2c"}}}}