keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.71k stars 19.43k forks source link

ugrade from keras 0.1.2 , a NLP example #1302

Closed StevenLOL closed 8 years ago

StevenLOL commented 8 years ago

Hi, I am new to Keras, and found a NLP example in Keras 0.1.2

https://github.com/vsl9/Sentiment-Analysis-with-Convolutional-Networks/blob/master

However it is out-of-date, can anyone show me how to upgrade it to the latest keras ? It could be a good NLP example.

The network is:

conv_input_height=2600
conv_input_width=200
# Number of feature maps (outputs of convolutional layer)
N_fm = 300
# kernel size of convolutional layer
kernel_size = 8

model = Sequential()
# Embedding layer (lookup table of trainable word vectors)

model.add(Embedding(input_dim=W.shape[0], output_dim=W.shape[1], weights=[W], W_constraint=unitnorm()))

'''
W= ndarray
W.shape[0] is 112541
W.shape[1] is 200
'''

# Reshape word vectors from Embedding to tensor format suitable for Convolutional layer
model.add(Reshape(1, conv_input_height, conv_input_width))

# first convolutional layer
model.add(Convolution2D(N_fm, 1, kernel_size, conv_input_width, border_mode='valid', W_regularizer=l2(0.0001)))
# ReLU activation
model.add(Activation('relu'))

# aggregate data in every feature map to scalar using MAX operation
model.add(MaxPooling2D(poolsize=(conv_input_height-kernel_size+1, 1), ignore_border=True))

model.add(Flatten())
model.add(Dropout(0.5))
# Inner Product layer (as in regular neural network, but without non-linear activation function)
model.add(Dense(N_fm, 2))
# SoftMax activation; actually, Dense+SoftMax works as Multinomial Logistic Regression
model.add(Activation('softmax'))

# Custom optimizers could be used, though right now standard adadelta is employed
model.compile(loss='categorical_crossentropy', optimizer='adadelta')

The first error is

@ model.add(Reshape(1, conv_input_height, conv_input_width)) ypeError: init() takes exactly 2 arguments (4 given) This can be fixed by model.add(Reshape((1,conv_input_height, conv_input_width)))

Then when adding the Convolution2D layer, this message is printed out: "Cannot connect non-masking layer to layer with masked output" .

farizrahman4u commented 8 years ago

There is a working example in the examples folder: https://github.com/fchollet/keras/blob/master/examples/imdb_cnn.py Also see: https://github.com/fchollet/keras/blob/master/examples/imdb_cnn_lstm.py

StevenLOL commented 8 years ago

@farizrahman4u thanks

And these will help:

embedding layber

issue #853