Skip to content

Instantly share code, notes, and snippets.

@ameasure
Created July 8, 2015 02:47
Show Gist options
  • Save ameasure/944439a04546f4c02cb9 to your computer and use it in GitHub Desktop.
Save ameasure/944439a04546f4c02cb9 to your computer and use it in GitHub Desktop.
reuters_multi_cnn.py
from __future__ import absolute_import
from __future__ import print_function
import numpy as np
from keras.datasets import reuters
from keras.models import Sequential
from keras.layers.embeddings import Embedding
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.core import Dense, Dropout, Activation, Flatten, Reshape, Merge
from keras.utils import np_utils
from keras.preprocessing.sequence import pad_sequences
vocab_size = 20000
batch_size = 1
embedding_size = 64
maxlen = 50
nb_feature_maps = 32
print("Loading data...")
(X_train, y_train), (X_test, y_test) = reuters.load_data(nb_words=vocab_size, test_split=0.2)
print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')
nb_classes = np.max(y_train) + 1
print(nb_classes, 'classes')
X_train = pad_sequences(X_train, maxlen=maxlen)
X_test = pad_sequences(X_test, maxlen=maxlen)
print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)
print("Convert class vector to binary class matrix (for use with categorical_crossentropy)")
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
print('Y_train shape:', Y_train.shape)
print('Y_test shape:', Y_test.shape)
ngram_filters = [2, 3, 4]
conv_filters = []
for n_gram in ngram_filters:
sequential = Sequential()
conv_filters.append(sequential)
sequential.add(Embedding(vocab_size + 1, embedding_size))
sequential.add(Reshape(1, maxlen, embedding_size))
sequential.add(Convolution2D(nb_feature_maps, 1, n_gram, embedding_size))
sequential.add(Activation("relu"))
sequential.add(MaxPooling2D(poolsize=(maxlen - n_gram + 1, 1)))
sequential.add(Flatten())
model = Sequential()
model.add(Merge(conv_filters, mode='concat'))
model.add(Dropout(0.5))
model.add(Dense(nb_feature_maps * len(conv_filters), nb_classes))
model.add(Activation("sigmoid"))
model.compile(loss='categorical_crossentropy', optimizer='adadelta')
model.fit(X=X_train, y=Y_train, batch_size=batch_size, nb_epoch=200, verbose=1, show_accuracy=True, validation_split=0.1)
score = model.evaluate(X_test, Y_test, batch_size=batch_size, verbose=1, show_accuracy=True)
print('Test score:', score[0])
print('Test accuracy:', score[1])
@simonhughes22
Copy link

What you are missing is that the inputs require to be be duplicated for this form of model (as it technically allows a series of different inputs to the merged layers). Change the last part to:

concat_X_test  = []
concat_X_train  = []
for i in range(len(ngram_filters)):
    concat_X_test.append(X_test)
    concat_X_train.append(X_train)

Xp = model.predict(concat_X_train)
print("Xp shape:", Xp.shape) #Check the expected output shape, a trick I learned from the author
model.fit(X=concat_X_train, y=Y_train, batch_size=batch_size, nb_epoch=200, verbose=1, show_accuracy=True, validation_split=0.1)

score = model.evaluate(concat_X_test, Y_test, batch_size=batch_size, verbose=1, show_accuracy=True)

This tripped me up at first until I read through the examples a few times. Re-read the examples of the merge layers. What you may want to do is to use the Graph container instead, although beware that also has a different input format, a dictionary of labelled np arrays.

@LopezGG
Copy link

LopezGG commented Dec 31, 2016

Reshape gives the folowing error:
sequential.add(Reshape(1, maxlen, embedding_size))
TypeError: init() takes exactly 2 arguments (4 given)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment