How to use fit_generator with sequential data that is split into batches?

非 Y 不嫁゛ 提交于 2019-12-23 19:51:05

问题


I am trying to write a generator for my Keras lstm model. To use it with fit_generator method. My first question is what should my generator return? A batch? A sequence? Example in Keras documentation returns x,y for each data entry, but what if my data is sequential? And I want to split it into batches?

Here is the python method that creates a batch for a given input

def get_batch(data, batch_num, batch_size, seq_length):
    i_start = batch_num*batch_size;
    batch_sequences = []
    batch_labels = []
    batch_chunk = data.iloc[i_start:(i_start+batch_size)+seq_length].values
    for i in range(0, batch_size):
        sequence = batch_chunk[(i_start+i):(i_start+i)+seq_length];
        label = data.iloc[(i_start+i)+seq_length].values;
        batch_labels.append(label)
        batch_sequences.append(sequence)
    return np.array(batch_sequences), np.array(batch_labels);

The output of this method for an input like this:

get_batch(data, batch_num=0, batch_size=2, seq_length=3):

Would be:

x = [
      [[1],[2],[3]],
      [[2],[3],[4]]
    ]

Here is how I imagine my model:

model = Sequential()
model.add(LSTM(256, return_sequences=True, input_shape=(seq_length, num_features)))
model.add(Dropout(0.2))
model.add(LSTM(256))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')

My question is how can I translate my method into a generator?


回答1:


Here is a solution that uses Sequence which acts like a generator in Keras:

class MySequence(Sequence):
  def __init__(self, num_batches):
    self.num_batches = num_batches

  def __len__(self):
    return self.num_batches # the length is the number of batches

  def __getitem__(self, batch_id):
    return get_batch(data, batch_id, self.batch_size, seq_length)

I think this is cleaner and doesn't modify your original function. Now you pass an instance of MySequence to model.fit_generator.



来源:https://stackoverflow.com/questions/50600624/how-to-use-fit-generator-with-sequential-data-that-is-split-into-batches

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