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?