{"action":"created","issue":{"url":"https://api.github.com/repos/fchollet/keras/issues/4758","repository_url":"https://api.github.com/repos/fchollet/keras","labels_url":"https://api.github.com/repos/fchollet/keras/issues/4758/labels{/name}","comments_url":"https://api.github.com/repos/fchollet/keras/issues/4758/comments","events_url":"https://api.github.com/repos/fchollet/keras/issues/4758/events","html_url":"https://github.com/fchollet/keras/issues/4758","id":196292878,"number":4758,"title":"Vector matrix dot product is inconsistient between backends","user":{"login":"JosephDavidsonKSWH","id":14977791,"avatar_url":"https://avatars.githubusercontent.com/u/14977791?v=3","gravatar_id":"","url":"https://api.github.com/users/JosephDavidsonKSWH","html_url":"https://github.com/JosephDavidsonKSWH","followers_url":"https://api.github.com/users/JosephDavidsonKSWH/followers","following_url":"https://api.github.com/users/JosephDavidsonKSWH/following{/other_user}","gists_url":"https://api.github.com/users/JosephDavidsonKSWH/gists{/gist_id}","starred_url":"https://api.github.com/users/JosephDavidsonKSWH/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JosephDavidsonKSWH/subscriptions","organizations_url":"https://api.github.com/users/JosephDavidsonKSWH/orgs","repos_url":"https://api.github.com/users/JosephDavidsonKSWH/repos","events_url":"https://api.github.com/users/JosephDavidsonKSWH/events{/privacy}","received_events_url":"https://api.github.com/users/JosephDavidsonKSWH/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-12-18T19:08:05Z","updated_at":"2016-12-19T14:11:01Z","closed_at":null,"body":"Using K.dot(a,b) in the tensorflow backend to compute a vector/matrix dot product crashes because of different dims. The same calculation in the theano backend is correctly computed.\r\n\r\nSimilarly, matrix/vector and vector/vector products fail\r\n\r\n[Gist](https://gist.github.com/JosephDavidsonKSWH/1404e5ea0886a442c2982fae70a4a243)\r\n\r\n- [x] Check that you are up-to-date with the master branch of Keras. You can update with:\r\npip install git+git://github.com/fchollet/keras.git --upgrade --no-deps\r\n\r\n- [x] If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found [here](https://www.tensorflow.org/get_started/os_setup).\r\n\r\n- [x] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:\r\npip install git+git://github.com/Theano/Theano.git --upgrade --no-deps\r\n"},"comment":{"url":"https://api.github.com/repos/fchollet/keras/issues/comments/267972846","html_url":"https://github.com/fchollet/keras/issues/4758#issuecomment-267972846","issue_url":"https://api.github.com/repos/fchollet/keras/issues/4758","id":267972846,"user":{"login":"JosephDavidsonKSWH","id":14977791,"avatar_url":"https://avatars.githubusercontent.com/u/14977791?v=3","gravatar_id":"","url":"https://api.github.com/users/JosephDavidsonKSWH","html_url":"https://github.com/JosephDavidsonKSWH","followers_url":"https://api.github.com/users/JosephDavidsonKSWH/followers","following_url":"https://api.github.com/users/JosephDavidsonKSWH/following{/other_user}","gists_url":"https://api.github.com/users/JosephDavidsonKSWH/gists{/gist_id}","starred_url":"https://api.github.com/users/JosephDavidsonKSWH/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JosephDavidsonKSWH/subscriptions","organizations_url":"https://api.github.com/users/JosephDavidsonKSWH/orgs","repos_url":"https://api.github.com/users/JosephDavidsonKSWH/repos","events_url":"https://api.github.com/users/JosephDavidsonKSWH/events{/privacy}","received_events_url":"https://api.github.com/users/JosephDavidsonKSWH/received_events","type":"User","site_admin":false},"created_at":"2016-12-19T14:11:01Z","updated_at":"2016-12-19T14:11:01Z","body":"Not sure it's as simple as that. tf.mul(a, b) will only work if the final dimensions are the same.\r\nThe replacing K.dot(...) in the above gist with a tf.mul still crashes.\r\n\r\nI have a fairly inelegant workaround which expands and squeezes tensors in the appropriate dimension if there is a difference of 1 between the dims. It replicates the dot functionality which I am looking for, but probably won't work so well for larger tensors with dims > 2.\r\n\r\n```python\r\ndef tf_dotproduct(x, y):\r\n        expandx = False\r\n        expandy = False\r\n        dy = K.ndim(y)\r\n        dx = K.ndim(x)\r\n        if dy > 1 or dx > 1:\r\n            if dx == dy - 1:\r\n                # Expand x at dim 0\r\n                xt = K.expand_dims(x, 0)\r\n                expandx = True\r\n            else:\r\n                xt = x\r\n            if dy == dx - 1:\r\n                # expand at dim n\r\n                yt = K.expand_dims(y, dy)\r\n                expandy = True\r\n            else:\r\n                yt = y\r\n            res = K.dot(xt, yt)\r\n            if expandx:\r\n                res = K.squeeze(res, 0)\r\n            if expandy:\r\n                res = K.squeeze(res, dy)\r\n        else:\r\n            res = K.tf.reduce_sum(K.tf.mul(x, y))\r\n        return res\r\n```\r\n\r\n"}}
{"actor":{"display_login":"JosephDavidsonKSWH"}}