Keras Masking for RNN with Varying Time Steps

前端 未结 2 720
夕颜
夕颜 2020-12-15 17:08

I\'m trying to fit an RNN in Keras using sequences that have varying time lengths. My data is in a Numpy array with format (sample, time, feature) = (20631, max_time,

相关标签:
2条回答
  • 2020-12-15 17:51

    The way you implemented masking should be correct. If you have data with the shape (samples, timesteps, features), and you want to mask timesteps lacking data with a zero mask of the same size as the features argument, then you add Masking(mask_value=0., input_shape=(timesteps, features)). See here: keras.io/layers/core/#masking

    Your model could potentially be too simple, and/or your number of epochs could be insufficient for the model to differentiate between all of your classes. Try this model:

    model = Sequential()
    model.add(Masking(mask_value=0., input_shape=(max_time, 24)))
    model.add(LSTM(256, input_dim=24))
    model.add(Dense(1024))
    model.add(Dense(2))
    model.add(Activation(activate))
    model.compile(loss=weibull_loglik_discrete, optimizer=RMSprop(lr=.01))
    model.fit(train_x, train_y, nb_epoch=100, batch_size=1000, verbose=2, validation_data=(test_x, test_y)) 
    

    If that does not work, try doubling the epochs a few times (e.g. 200, 400) and see if that improves the results.

    0 讨论(0)
  • 2020-12-15 18:06

    I could not validate without actual data, but I had a similar experience with an RNN. In my case normalization solved the issue. Add a normalization layer to your model.

    0 讨论(0)
提交回复
热议问题