{"action":"created","issue":{"url":"https://api.github.com/repos/pfnet/chainer/issues/1006","repository_url":"https://api.github.com/repos/pfnet/chainer","labels_url":"https://api.github.com/repos/pfnet/chainer/issues/1006/labels{/name}","comments_url":"https://api.github.com/repos/pfnet/chainer/issues/1006/comments","events_url":"https://api.github.com/repos/pfnet/chainer/issues/1006/events","html_url":"https://github.com/pfnet/chainer/issues/1006","id":138439108,"number":1006,"title":"Suggestion: more general reduction kernel","user":{"login":"ryotat","id":1888788,"avatar_url":"https://avatars.githubusercontent.com/u/1888788?v=3","gravatar_id":"","url":"https://api.github.com/users/ryotat","html_url":"https://github.com/ryotat","followers_url":"https://api.github.com/users/ryotat/followers","following_url":"https://api.github.com/users/ryotat/following{/other_user}","gists_url":"https://api.github.com/users/ryotat/gists{/gist_id}","starred_url":"https://api.github.com/users/ryotat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ryotat/subscriptions","organizations_url":"https://api.github.com/users/ryotat/orgs","repos_url":"https://api.github.com/users/ryotat/repos","events_url":"https://api.github.com/users/ryotat/events{/privacy}","received_events_url":"https://api.github.com/users/ryotat/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-03-04T10:28:44Z","updated_at":"2016-06-09T05:19:56Z","closed_at":"2016-06-09T05:19:56Z","body":"Hi,\r\n\r\nSuppose I want to compute the weighted sum of a column along some axis. The weights are not normalized. In numpy/cupy, I would write something like\r\n```python\r\nout = (x * weight).sum(axis=-1) / weight.sum()\r\n```\r\n\r\nI don't know how this is currently done in cupy but I guess the most naive way of doing it would be to call (1) multiply (x * weight) (2) sum over the terms in the numerator, (3) sum over the weights, and (4) divide the first sum by the second sum. I was wondering if it would be more efficient, if I could write a kernel for doing this.\r\n\r\nI was trying to do this by defining the following kernel\r\n```python\r\nweighted_sum=cupy.ReductionKernel('T x, T weight',\r\n                                  'T out',\r\n                                  'x * weight; total_weight += weight',\r\n                                  'a + b',\r\n                                  'out = a / total_weight',\r\n                                  '0); T total_weight=(0',\r\n                                  'weighted_sum',\r\n                                  'T')\r\n```\r\nwhich seems to compile ok but does not work. Somehow `total_weight` at the end is zero and I get inf everywhere.\r\n\r\nIs there a way to define reductions over multiple variables? In this case I would like to compute both the sum in the numerator and the sum in the denominator. If not, I think this would be a valuable feature for many users.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/pfnet/chainer/issues/comments/224802345","html_url":"https://github.com/pfnet/chainer/issues/1006#issuecomment-224802345","issue_url":"https://api.github.com/repos/pfnet/chainer/issues/1006","id":224802345,"user":{"login":"okuta","id":7133388,"avatar_url":"https://avatars.githubusercontent.com/u/7133388?v=3","gravatar_id":"","url":"https://api.github.com/users/okuta","html_url":"https://github.com/okuta","followers_url":"https://api.github.com/users/okuta/followers","following_url":"https://api.github.com/users/okuta/following{/other_user}","gists_url":"https://api.github.com/users/okuta/gists{/gist_id}","starred_url":"https://api.github.com/users/okuta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/okuta/subscriptions","organizations_url":"https://api.github.com/users/okuta/orgs","repos_url":"https://api.github.com/users/okuta/repos","events_url":"https://api.github.com/users/okuta/events{/privacy}","received_events_url":"https://api.github.com/users/okuta/received_events","type":"User","site_admin":false},"created_at":"2016-06-09T05:19:56Z","updated_at":"2016-06-09T05:19:56Z","body":"It seems the problem has been resolved."}}
{"actor":{"display_login":"okuta"}}