{"action":"opened","issue":{"url":"https://api.github.com/repos/fchollet/keras/issues/3703","repository_url":"https://api.github.com/repos/fchollet/keras","labels_url":"https://api.github.com/repos/fchollet/keras/issues/3703/labels{/name}","comments_url":"https://api.github.com/repos/fchollet/keras/issues/3703/comments","events_url":"https://api.github.com/repos/fchollet/keras/issues/3703/events","html_url":"https://github.com/fchollet/keras/issues/3703","id":175255914,"number":3703,"title":"Lambda layer for computing sum over samples does not work","user":{"login":"phdowling","id":876919,"avatar_url":"https://avatars.githubusercontent.com/u/876919?v=3","gravatar_id":"","url":"https://api.github.com/users/phdowling","html_url":"https://github.com/phdowling","followers_url":"https://api.github.com/users/phdowling/followers","following_url":"https://api.github.com/users/phdowling/following{/other_user}","gists_url":"https://api.github.com/users/phdowling/gists{/gist_id}","starred_url":"https://api.github.com/users/phdowling/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/phdowling/subscriptions","organizations_url":"https://api.github.com/users/phdowling/orgs","repos_url":"https://api.github.com/users/phdowling/repos","events_url":"https://api.github.com/users/phdowling/events{/privacy}","received_events_url":"https://api.github.com/users/phdowling/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-09-06T14:05:14Z","updated_at":"2016-09-06T14:05:14Z","closed_at":null,"body":"I'm building a model that entails embedding a sequence words, and summing over the vectors to get a single dense representation. This dense representation is then repeated and merged with the embedding another sequence of words.\r\n\r\nFor computing the sum over vectors, I wrote a Lambda layer that simply performs a `K.sum(x, axis=1)` operation on the input. \r\n\r\nQuestion 1: I use `axis=1` because I assumer the lambda layer gets applied to a whole batch of inputs at a time, meaning axis 0 is the sample index, while axis 1 indexes the words (i.e. word vectors after embedding) in each sample. Is this correct?\r\n\r\nNow, I get a compile error when I actually want to build this model. Below is a simple, almost runnable version of what I want to build. If you uncomment the LSTM line and comment out the custom Lambda layer, everything compiles fine (even though the LSTM *should* do the same dimensionality transformation!).\r\n```python\r\nfrom keras.layers import Input, Lambda, Embedding, merge, RepeatVector, LSTM\r\nfrom keras.models import Model\r\nfrom keras import backend as K\r\nimport numpy as np\r\nvocab_size = 2\r\n\r\n# Inputs and embeddings \r\ninp = Input((4,), dtype=\"int32\")\r\ninp2 = Input((4,), dtype=\"int32\")\r\nembed = Embedding(input_dim=vocab_size, output_dim=3, name=\"word_embeddings\")(inp)\r\nembed2 = Embedding(input_dim=vocab_size, output_dim=3, name=\"word_embeddings\")(inp2)\r\n\r\n# My custom lambda for computing the sum of vectors the input sequence\r\nembedding_sum = Lambda(lambda x: K.sum(x, axis=1))(embed)\r\n# If we use this LSTM layer below instead of our lambda, everything works! \r\n#embedding_sum = LSTM(3, return_sequences=False)(embed)\r\n\r\n# Repeat the sum to merge it with the other sequence\r\nembedding_sum_repeated = RepeatVector(4)(embedding_sum)\r\n\r\n# Do the element-wise merge\r\nmerged = merge([embed2, embedding_sum_repeated], mode=\"dot\")\r\n\r\n# Build model\r\nmodel = Model(inp, embedding_sum)\r\nmodel.compile(optimizer=\"rmsprop\", loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\r\n\r\n# run an example\r\nx = np.array([\r\n\t[0, 1, 1, 0],\r\n\t[1, 1, 1, 0]\r\n\t]\r\n)\r\n\r\nmodel.predict(x)\r\n```\r\n\r\nThis throws an error on model compilation: \r\n```\r\nTraceback (most recent call last):\r\n  File \"<stdin>\", line 2, in <module>\r\n  File \"/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py\", line 1522, in merge\r\n    name=name)\r\n  File \"/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py\", line 1180, in __init__\r\n    node_indices, tensor_indices)\r\n  File \"/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py\", line 1236, in _arguments_validation\r\n    '%s != %s. ' % (shape1[dot_axes[0]], shape2[dot_axes[1]]) +\r\nTypeError: 'int' object has no attribute '__getitem__'\r\n```\r\nNow, this error is an error caused within some code that actually is supposed to throw an exception. At that point in topology.py:\r\n```\r\nraise Exception('Dimension incompatibility using dot mode: ' +\r\n                                '%s != %s. ' % (shape1[dot_axes[0]], shape2[dot_axes[1]]) +\r\n                                'Layer shapes: %s, %s' % (shape1, shape2))\r\n```\r\nCould someone help out here? The lambda layer seems to mess up the dimensions of my tensor."}}
{"actor":{"display_login":"phdowling"}}