{"action":"created","issue":{"url":"https://api.github.com/repos/fchollet/keras/issues/322","labels_url":"https://api.github.com/repos/fchollet/keras/issues/322/labels{/name}","comments_url":"https://api.github.com/repos/fchollet/keras/issues/322/comments","events_url":"https://api.github.com/repos/fchollet/keras/issues/322/events","html_url":"https://github.com/fchollet/keras/issues/322","id":92650406,"number":322,"title":"Convolution1D Input Error","user":{"login":"valexandersaulys","id":8105763,"avatar_url":"https://avatars.githubusercontent.com/u/8105763?v=3","gravatar_id":"","url":"https://api.github.com/users/valexandersaulys","html_url":"https://github.com/valexandersaulys","followers_url":"https://api.github.com/users/valexandersaulys/followers","following_url":"https://api.github.com/users/valexandersaulys/following{/other_user}","gists_url":"https://api.github.com/users/valexandersaulys/gists{/gist_id}","starred_url":"https://api.github.com/users/valexandersaulys/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/valexandersaulys/subscriptions","organizations_url":"https://api.github.com/users/valexandersaulys/orgs","repos_url":"https://api.github.com/users/valexandersaulys/repos","events_url":"https://api.github.com/users/valexandersaulys/events{/privacy}","received_events_url":"https://api.github.com/users/valexandersaulys/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-07-02T14:22:12Z","updated_at":"2015-07-04T09:52:15Z","closed_at":null,"body":"I saw that Convolution1D layer requires the input to be a 4D tensor and not a 3D tensor of (nb_samples, nb_channels, sequence_length), but some tensor that gets wrapped by T.nnet.conv.conv2d(X, (nb_filter, stack_size, 1, filter_length)). See  <https://github.com/fchollet/keras/issues/272> for more on this post. \r\n\r\nI have the following code that is throwing a weird error for me, after I format the input to be a 4D tensor. I thought that it might be best to leave it as a 2D input of (nb_samples, seq_length), but also throws an error as it expected the input to be 4D. Below is a snippet of my code.\r\n\r\n\r\nnb_samples = x_train.shape[0]\r\nnb_features = x_train.shape[1]\r\nnewshape = (nb_samples, 1, nb_features, 1)\r\nprint newshape\r\nx_train = np.reshape(x_train, newshape).astype('float32')\r\nprint x_train.shape; print x_train;\r\n\r\nmodel = Sequential()\r\nmodel.add(Convolution1D(nb_filter=5, \r\n                        stack_size=1, \r\n                        filter_length=3, \r\n                        init='uniform', \r\n                        activation=activation_function_1))\r\nmodel.add(MaxPooling1D(pool_length=3))\r\nmodel.add(Convolution1D(nb_filter=5, \r\n                        stack_size=1, \r\n                        filter_length=3, \r\n                        init='uniform', \r\n                        activation=activation_function_1))\r\nmodel.add(MaxPooling1D(pool_length=3))\r\nmodel.add(Dense(input_dim=100,output_dim=1,init='uniform'))\r\nmodel.add(Activation('linear'))\r\nprint \"Now Building Model\"\r\nprint \"Model will run for \" + str(num_epochs)\r\nmodel.compile(loss='mean_squared_error',\r\n              optimizer=RMSprop(lr=LR)\r\n              )\r\n\r\n\r\nprint \"Model Compiled! Now fitting ... \"\r\n\r\nmodel.fit(x_train,y_train,nb_epoch=num_epochs,batch_size=BATCH_SIZE)\r\n\r\n\r\nAnd below is the error I receive. \r\n\r\nTraceback (most recent call last):\r\n  File \"keras_conv1d.py\", line 103, in <module>\r\n    model.fit(x_train,y_train,nb_epoch=num_epochs,batch_size=BATCH_SIZE)\r\n  File \"/home/sensauratech/ml_vincent/keras/keras/models.py\", line 241, in fit\r\n    loss = self._train(*ins)\r\n  File \"/home/sensauratech/Theano/theano/compile/function_module.py\", line 534, in __call__\r\n    allow_downcast=s.allow_downcast)\r\n  File \"/home/sensauratech/Theano/theano/tensor/type.py\", line 168, in filter\r\n    data.shape))\r\nTypeError: ('Bad input argument to theano function with name \"/home/sensauratech/ml_vincent/keras/keras\r\n/models.py:127\"  at index 1(0-based)', 'Wrong number of dimensions: expected 4, got 2 with shape (1, 1)\r\n.')       \r\n\r\n\r\nI am so lost as to what to do here. Not formatting the input to be 4D results in the following error instead.\r\n\r\n\r\nTraceback (most recent call last):\r\n  File \"keras_conv1d.py\", line 95, in <module>\r\n    model.fit(x_train,y_train,nb_epoch=num_epochs,batch_size=BATCH_SIZE)\r\n  File \"/home/sensauratech/ml_vincent/keras/keras/models.py\", line 241, in fit\r\n    loss = self._train(*ins)\r\n  File \"/home/sensauratech/Theano/theano/compile/function_module.py\", line 534, in __call__\r\n    allow_downcast=s.allow_downcast)\r\n  File \"/home/sensauratech/Theano/theano/tensor/type.py\", line 168, in filter\r\n    data.shape))\r\nTypeError: ('Bad input argument to theano function with name \"/home/sensauratech/ml_vincent/keras/keras\r\n/models.py:127\"  at index 0(0-based)', 'Wrong number of dimensions: expected 4, got 2 with shape (1, 73\r\n57).')      \r\n\r\n\r\nAny help would be much appreciated!  I am at a loss of what to do. :)\r\n"},"comment":{"url":"https://api.github.com/repos/fchollet/keras/issues/comments/118494526","html_url":"https://github.com/fchollet/keras/issues/322#issuecomment-118494526","issue_url":"https://api.github.com/repos/fchollet/keras/issues/322","id":118494526,"user":{"login":"mikekestemont","id":4376879,"avatar_url":"https://avatars.githubusercontent.com/u/4376879?v=3","gravatar_id":"","url":"https://api.github.com/users/mikekestemont","html_url":"https://github.com/mikekestemont","followers_url":"https://api.github.com/users/mikekestemont/followers","following_url":"https://api.github.com/users/mikekestemont/following{/other_user}","gists_url":"https://api.github.com/users/mikekestemont/gists{/gist_id}","starred_url":"https://api.github.com/users/mikekestemont/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikekestemont/subscriptions","organizations_url":"https://api.github.com/users/mikekestemont/orgs","repos_url":"https://api.github.com/users/mikekestemont/repos","events_url":"https://api.github.com/users/mikekestemont/events{/privacy}","received_events_url":"https://api.github.com/users/mikekestemont/received_events","type":"User","site_admin":false},"created_at":"2015-07-04T09:52:15Z","updated_at":"2015-07-04T09:52:15Z","body":"Great update, @fchollet! I think it would be insightful to add a working example on conv1d to the examples dir. I could contribute a first attempt using a character-level convolution for a text classification task (with one channel for each char in the alphabet?) in the coming days, if that's useful? The imdb dataset might be good for this, potentially with an LSTM overlaid, to give an example of how the output of 1D convs could be processed?"}}