简体   繁体   中英

Keras - How to convert LSTM code into CNN

I am working on binary classification sentiment analysis either positive or negative my lstm code is working fine but i am converting my lstm code into cnn having Value error of "input_length" is 30, but received input has shape (None, 1)

my input shape is (30,1) my batch size is 24 in lstm

model.add(Embedding(30,30,input_length=30))
model.add(Conv1D(padding='valid',activation='relu',strides=1))
model.add(Dropout(0.2))
model.add(Dense(30))
model.add(Dropout(0.2))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(inputBatch, ponlabel,batch_size=24,epochs=20,validation_data=(inputBatch, ponlabel))

for the sake of reference I have put my LSTM Code.

model.add(LSTM(100, input_shape=(30, 1)))
model.add(Dense(30, activation="relu"))
model.add(Dense(1, activation="sigmoid"))
model.compile(loss='mean_absolute_error', optimizer='adam',metrics=["accuracy"])
model.fit(inputBatch, ponlabel,
          batch_size=24, epochs=20, verbose=1)

You are using Convolution2D but your data over timesteps is 1 dimensional. So you need to use Convolutional1D to convolve over the tokens in your sentence. There is a CNN text classification in the Keras examples folder imdb_cnn.py .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM