Я хочу обучить модели LSTM, используя TimeSeriesGenerator. Вот пример данных:
import numpy as np
import tensorflow as tf
from tensorflow import keras
X_train = pd.DataFrame(X[:50, :3]).to_dict()
data_dict = {0: {0: 17.03263553814233,
1: 16.91773130053791,
2: 16.880430969011925,
3: 16.941212281508385,
4: 17.06728136015525,
5: 16.951460252480615,
6: 16.88878546128022,
7: 16.83633974954705,
8: 16.839547021666732,
9: 16.893047070188103,
10: 16.908247408273184,
11: 16.932061227796794,
12: 16.70144454118497,
13: 16.66517923074079,
14: 16.59713738578013,
15: 16.67469995149962,
16: 16.64827817893891,
17: 16.689412341406623,
18: 16.745563889588468,
19: 16.84815901998644,
20: 16.82860048430865,
21: 16.80598224635871,
22: 16.772347951779608,
23: 16.787230667980424,
24: 16.655011192853294,
25: 16.73827787518816,
26: 16.72633573886843,
27: 16.8401507524151,
28: 16.702673077521396,
29: 16.762289900352002,
30: 16.586687597474594,
31: 16.660249461764664,
32: 16.707573930305628,
33: 16.752390582119617,
34: 16.708825403777027,
35: 16.722208462947748,
36: 16.72985951041818,
37: 16.73602224988734,
38: 16.65139978234363,
39: 16.67022197897856,
40: 16.86610115511084,
41: 16.8482315125475,
42: 16.833133725267032,
43: 16.797556855127254,
44: 16.78316620312438,
45: 16.780553429685014,
46: 16.806822449260707,
47: 16.8451916415556,
48: 16.94550168688282,
49: 16.84370375684513},
1: {0: 43000.0,
1: 111800.0,
2: 96400.0,
3: 58700.0,
4: 23900.0,
5: 9100.0,
6: 67900.0,
7: 80200.0,
8: 87400.0,
9: 42100.0,
10: 26100.0,
11: 42900.0,
12: 250400.0,
13: 97500.0,
14: 257800.0,
15: 48900.0,
16: 137300.0,
17: 68700.0,
18: 50200.0,
19: 81200.0,
20: 45200.0,
21: 37800.0,
22: 24300.0,
23: 12700.0,
24: 58400.0,
25: 46500.0,
26: 10400.0,
27: 10000.0,
28: 44300.0,
29: 46300.0,
30: 225000.0,
31: 23900.0,
32: 1000800.0,
33: 71400.0,
34: 29500.0,
35: 149600.0,
36: 117600.0,
37: 89800.0,
38: 50000.0,
39: 63100.0,
40: 28300.0,
41: 8500.0,
42: 34400.0,
43: 22300.0,
44: 60700.0,
45: 8800.0,
46: 43400.0,
47: 75500.0,
48: 136800.0,
49: 24600.0},
2: {0: 23.543331924769625,
1: 23.3956762891149,
2: 23.417613400194405,
3: 23.859848685580317,
4: 23.60976568548289,
5: 23.538354361435772,
6: 23.547637010902115,
7: 23.552996092560093,
8: 23.78698229934543,
9: 23.664908494474194,
10: 23.747881962380657,
11: 23.683826412575037,
12: 23.686379446852914,
13: 23.687620700357115,
14: 23.918675354407362,
15: 23.904309666941682,
16: 24.148060575406685,
17: 24.13765511195884,
18: 23.996674352326753,
19: 23.998178700008626,
20: 24.126958920350923,
21: 24.04678460856936,
22: 24.155977056578347,
23: 24.14693905013938,
24: 23.831879893861824,
25: 23.84887261006559,
26: 24.12559692458759,
27: 24.11140163004467,
28: 23.955514506550816,
29: 24.04190979398875,
30: 24.058656527592404,
31: 24.12582514532358,
32: 24.22946534475154,
33: 24.20869509568419,
34: 24.09349543840721,
35: 24.187021249006563,
36: 24.21144457575573,
37: 24.13637519528531,
38: 24.22781423971693,
39: 24.2215057285322,
40: 24.118051328759705,
41: 24.112170255986804,
42: 24.113874923758225,
43: 24.114421718733333,
44: 24.054540271482693,
45: 24.06846432723949,
46: 24.089484450162367,
47: 24.018806212384856,
48: 24.113251988260423,
49: 23.89928510418779}}
X_train = pd.DataFrame(data_dict).values
y_train = X_train[:, 0]
А вот мой код:
# genrator
train_test_index_split = 0.8
train_generator = keras.preprocessing.sequence.TimeseriesGenerator(
data=X_train, # sample.drop(columns=['close_orig']).values
targets=y_train,
length=5,
sampling_rate=1,
stride=1,
start_index=0,
end_index=int(0.8*X_train.shape[0]),
shuffle=False,
reverse=False,
batch_size=10
)
# modeling
model = keras.models.Sequential([
keras.layers.LSTM(20, return_sequences=True, input_shape=[None, X_train.shape[1]]),
keras.layers.LSTM(20, return_sequences=True),
keras.layers.TimeDistributed(keras.layers.Dense(1))
])
model.compile(loss='mse',optimizer='adam')
history = model.fit_generator(train_generator, steps_per_epoch=1, epochs=20, verbose=1)
pd.DataFrame(history.history).plot()
Я получаю ошибку:
InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: Incompatible shapes: [10,5,1] vs. [10,1]
[[{{node loss_7/time_distributed_7_loss/SquaredDifference}}]]
[[loss_7/mul/_641]]
(1) Invalid argument: Incompatible shapes: [10,5,1] vs. [10,1]
[[{{node loss_7/time_distributed_7_loss/SquaredDifference}}]]
0 successful operations.
0 derived errors ignored.
Я не понимаю что не так с моими размерами.
Бонусный вопрос. Поскольку есть некоторые проблемы с функцией evaluate_generator
и predict_generator
( Keras: как оценить точность модели Предикат_генератор)? ), и у меня достаточно оперативной памяти, как я могу преобразовать генератор в X yy y со всеми данными. Тогда я смогу использовать обычный прогноз и оценку.