{"action":"created","issue":{"url":"https://api.github.com/repos/fchollet/keras/issues/233","labels_url":"https://api.github.com/repos/fchollet/keras/issues/233/labels{/name}","comments_url":"https://api.github.com/repos/fchollet/keras/issues/233/comments","events_url":"https://api.github.com/repos/fchollet/keras/issues/233/events","html_url":"https://github.com/fchollet/keras/issues/233","id":88770386,"number":233,"title":"Example of How to Construct 1D Convolutional Net on Text","user":{"login":"simonhughes22","id":2167017,"avatar_url":"https://avatars.githubusercontent.com/u/2167017?v=3","gravatar_id":"","url":"https://api.github.com/users/simonhughes22","html_url":"https://github.com/simonhughes22","followers_url":"https://api.github.com/users/simonhughes22/followers","following_url":"https://api.github.com/users/simonhughes22/following{/other_user}","gists_url":"https://api.github.com/users/simonhughes22/gists{/gist_id}","starred_url":"https://api.github.com/users/simonhughes22/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/simonhughes22/subscriptions","organizations_url":"https://api.github.com/users/simonhughes22/orgs","repos_url":"https://api.github.com/users/simonhughes22/repos","events_url":"https://api.github.com/users/simonhughes22/events{/privacy}","received_events_url":"https://api.github.com/users/simonhughes22/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":22,"created_at":"2015-06-16T16:11:20Z","updated_at":"2015-07-15T21:40:41Z","closed_at":"2015-06-16T20:58:43Z","body":"I'd like to use keras to build a 1D convolutional net with pooling layers on some textual input, but I can't figure out the right input format and the right number of incoming connections above the flatten layer. Would you be able to provide a simple example using one of the data sets?\r\n\r\nAwesome work by the way, great library."},"comment":{"url":"https://api.github.com/repos/fchollet/keras/issues/comments/121758481","html_url":"https://github.com/fchollet/keras/issues/233#issuecomment-121758481","issue_url":"https://api.github.com/repos/fchollet/keras/issues/233","id":121758481,"user":{"login":"manwinder123","id":6258160,"avatar_url":"https://avatars.githubusercontent.com/u/6258160?v=3","gravatar_id":"","url":"https://api.github.com/users/manwinder123","html_url":"https://github.com/manwinder123","followers_url":"https://api.github.com/users/manwinder123/followers","following_url":"https://api.github.com/users/manwinder123/following{/other_user}","gists_url":"https://api.github.com/users/manwinder123/gists{/gist_id}","starred_url":"https://api.github.com/users/manwinder123/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/manwinder123/subscriptions","organizations_url":"https://api.github.com/users/manwinder123/orgs","repos_url":"https://api.github.com/users/manwinder123/repos","events_url":"https://api.github.com/users/manwinder123/events{/privacy}","received_events_url":"https://api.github.com/users/manwinder123/received_events","type":"User","site_admin":false},"created_at":"2015-07-15T21:40:41Z","updated_at":"2015-07-15T21:40:41Z","body":"hey guys, great library @fchollet \r\n\r\n\r\n\r\nSo i have created a Feed forward neural network using Keras, think i can call it a deep nn, i have 3 hidden layers. sorry if my lingo is off. I am not hitting the accuracy i want on my test set so i wanna see if CNN can help. I have text that i send my nn, i send it a 100 characters (i normalize the characters by dividing their ascii integer value by 255), it does its work and comes out with an output of 20. I have a classification problem.\r\n\r\n\r\n\r\nSo @ameasure i am looking at your code and i am little confused. what would field size be in my case, or what is it in your case and how did you get the numbers. What about stride?\r\n\r\n\r\n\r\n@fchollet I would think that using a 1d CNN makes sense for text (since in my case it would be 100x1 sized array), you guys talk about 2d being optimal, why is that?\r\n\r\n\r\nlooking at the api for 1d: Convolution1D(input_dim, nb_filter, filter_length, ...)\r\nI think my settings would be:\r\n    *input_dim = 100 (guessing that this is the input size but you say that this is the # of channels, in pictures there are 3, rgb, so would i set this to 1)\r\n    *nb_filter = 25 (i'm not really sure about this one, on the Keras website you write \"(dimensionality of the output)\", i guess this is sorta like how many outputs the cnn layer will have\r\n   *filter_length = completely lost on this one\r\n\r\n\r\nlooking at the api for 2d: Convolution2D(nb_filter, stack_size, nb_row, nb_col, ...)\r\nI think my settings would be:\r\n    *nb_filter = 25 (see 1d above)\r\n    *stack_size = 1 (i've seen 3 in some of your examples and i assume that its 3 because the pictures have a red, green and blue color channel, my input is text so i think it has only 1 channel??)\r\n    *nb_row = 1 (my data doesn't have multiple rows, its just one row with 100 columns)\r\n    *nb_col = 100 (i have a 100 characters, so i guess thats 100 rows)\r\n\r\n\r\nI have 50 samples to train on and 20 to test on\r\n\r\nAlso there is a subsample_length (1d) and subsample (2d) in the cnn layers, i have read that subsampling is similar to pooling. If i added the subsample option to my cnn layer would i skip pooling?\r\n\r\nSorry about all the questions, i've spent hours looking for examples and trying to understand CNN's. I have a good concept of what they are, whats confusing me is the parameters and what i need to set them at. There is also the 1d vs 2d.\r\n\r\n```\r\n# Convolution layers expect a 4-D input so we reshape our 2-D input\r\nnb_samples = X_train.shape[0]\r\nnb_features = X_train.shape[1]\r\nnewshape = (nb_samples, 1, nb_features, 1)\r\nX_train = np.reshape(X_train, newshape).astype(theano.config.floatX)\r\n\r\n# We set some hyperparameters\r\nBATCH_SIZE = 16\r\nFIELD_SIZE = 5 * 300\r\nSTRIDE = 300\r\nN_FILTERS = 200\r\n\r\n# We fit the model\r\nmodel = Sequential()\r\nmodel.add(Convolution2D(nb_filter=N_FILTERS, stack_size=1, nb_row=FIELD_SIZE, \r\n                        nb_col=1, subsample=(STRIDE, 1)))\r\nmodel.add(Activation('relu'))\r\nmodel.add(MaxPooling2D(poolsize=(((nb_features - FIELD_SIZE) / STRIDE) + 1, 1)))\r\nmodel.add(Dropout(0.5))\r\nmodel.add(Flatten())\r\nmodel.add(Dense(N_FILTERS, nb_classes))\r\nmodel.add(Activation('softmax'))\r\n\r\nmodel.compile(loss='categorical_crossentropy', optimizer='adadelta')\r\nprint 'fitting model'\r\nmodel.fit(X_train, Y_train, nb_epoch=10, batch_size=BATCH_SIZE, verbose=1, \r\n          show_accuracy=True, validation_split=0.1)\r\n```\r\n\r\nthanks for any help :)"}}