У меня проблемы с внедрением Keras TimeseriesGenerator. Я хочу поэкспериментировать с разными значениями для look_back
, переменной, определяющей длину лага для X с точки зрения каждого y. Прямо сейчас у меня установлено значение 3, но я хотел бы иметь возможность протестировать несколько значений. По сути, я хочу увидеть, увеличивает ли точность использование последних n строк для прогнозирования значения. Вот мой код:
### trying with timeseries generator
from keras.preprocessing.sequence import TimeseriesGenerator
look_back = 3
train_data_gen = TimeseriesGenerator(X_train, X_train,
length=look_back, sampling_rate=1,stride=1,
batch_size=3)
test_data_gen = TimeseriesGenerator(X_test, X_test,
length=look_back, sampling_rate=1,stride=1,
batch_size=1)
### Bi_LSTM
Bi_LSTM = Sequential()
Bi_LSTM.add(layers.Bidirectional(layers.LSTM(512, input_shape=(look_back, 11))))
Bi_LSTM.add(layers.Dropout(.5))
# Bi_LSTM.add(layers.Flatten())
Bi_LSTM.add(Dense(11, activation='softmax'))
Bi_LSTM.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
### fitting a small normal model seems to be necessary for compile
Bi_LSTM.fit(X_train[:1],
y_train[:1],
epochs=1,
batch_size=32,
validation_data=(X_test[:1], y_test[:1]),
class_weight=class_weights)
print('ignore above, necessary to run custom generator...')
Bi_LSTM_history = Bi_LSTM.fit_generator(Bi_LSTM.fit_generator(generator,
steps_per_epoch=1,
epochs=20,
verbose=0,
class_weight=class_weights))
Что дает следующую ошибку:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-35-11561ec7fb92> in <module>()
26 batch_size=32,
27 validation_data=(X_test[:1], y_test[:1]),
---> 28 class_weight=class_weights)
29 print('ignore above, necessary to run custom generator...')
30 Bi_LSTM_history = Bi_LSTM.fit_generator(Bi_LSTM.fit_generator(generator,
2 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
143 ': expected ' + names[i] + ' to have shape ' +
144 str(shape) + ' but got array with shape ' +
--> 145 str(data_shape))
146 return data
147
ValueError: Error when checking input: expected lstm_16_input to have shape (3, 11) but got array with shape (1, 11)
Если я изменю форму ввода BiLSTM на (1,11), как указано выше, то я получу это ошибка:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-36-7360e3790518> in <module>()
31 epochs=20,
32 verbose=0,
---> 33 class_weight=class_weights))
34
5 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
143 ': expected ' + names[i] + ' to have shape ' +
144 str(shape) + ' but got array with shape ' +
--> 145 str(data_shape))
146 return data
147
ValueError: Error when checking input: expected lstm_17_input to have shape (1, 11) but got array with shape (3, 11)
Что здесь происходит?
Если необходимо, мои данные считываются из df, где каждая строка (наблюдение) - это (1,11)
вектор с плавающей запятой, а каждая метка - это int, который я конвертирую в 1 горячую векторную фигуру (1,11)
.