how to reshape text data to be suitable for LSTM model in keras

僤鯓⒐⒋嵵緔 提交于 2019-11-29 14:16:45

You will need to reshape your data in the following way:

  • Samples. One sequence is one sample. A batch is comprised of one or more samples.
  • Time Steps. One time step is one point of observation in the sample.
  • Features. One feature is one observation at a time step.

(samples, time_steps, features)

Then your model should look like the following (simplified version):

visible = Input(shape=(time_steps, features))
encoder = LSTM(100, activation='relu')(visible)
# define reconstruct decoder
decoder = RepeatVector(time_steps)(encoder)
decoder = LSTM(100, activation='relu', return_sequences=True)(decoder)
decoder = TimeDistributed(Dense(features))(decoder)
model = Model(visible, decoder)

Check this great tutorial. Should be helpful for your case.

However, that said you might only need to expand the dimensions of the array.

Check this out as well it might clear things up.

Hope the above is helpful.

So as said in the comments it turns out I just needed to do one_hot_encoding.

when I did one_hot encoding using the tf.keras.backend it throws the error that I have updated in my question.

Then I tried to_categorical(sent_wids, num_classes=VOCAB_SIZE) and it fixed (however faced with memory error :D which is different story)!!!

I should also mention that I tried sparse_categorical_crossentropy instead of one_hot_encoding though it did not work!

Thank you for all your help:)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!