Keras Stateful LSTM fit_generator how to use batch_size > 1

霸气de小男生 提交于 2019-12-07 12:42:07

问题


I want to train an stateful LSTM network using the functional API in Keras.

The fit method is fit_generator.

I am able to train it, using: batch_size = 1

My Input layer is:

Input(shape=(n_history, n_cols),batch_shape=(batch_size, n_history, n_cols), 
    dtype='float32', name='daily_input')

The generator is as follows:

def training_data():
    while 1:       
        for i in range(0,pdf_daily_data.shape[0]-n_history,1):            
            x = f(i)() # f(i) shape is (1, n_history, n_cols)
            y = y(i)
            yield (x,y)

And then the fit is:

model.fit_generator(training_data(),
                    steps_per_epoch=pdf_daily_data.shape[0]//batch_size,...

This works and trains well, however, very slow and performing a gradient update at every time step since batch_size = 1

How, within this configuration, can I set a batch_size > 1 ? remember: the LSTM layer has stateful = True


回答1:


You will have to modify your generator to yeld the desired number of elements you want your batch to have.

Currently you are iterating over your data element by element (as per your third parameter of range()), obtaining a single x and y, and then yielding that element. As you are returning a single element you are obtaining a batch_size=1, as your fit_generator is training element by element.

Say you want your batch size to be 10, you will then have to slice your data and obtain segments of 10 elements each, and yield those slices instead of single elements. Just be sure that you reflect those changes accordingly on the shape of your Input layers, passing the corresponding batch_size.



来源:https://stackoverflow.com/questions/48382859/keras-stateful-lstm-fit-generator-how-to-use-batch-size-1

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