Dimensions not appropriate for keras LSTM

I want to use the LSTM neural network with keras to predict time series groups, and I'm having trouble getting the model to fit what I want. Sizes of my data:

input tensor: (data length, number of series to train, time steps to look back)

output tensor: (data length, number of series to forecast, time steps to look ahead)

Note. I want to keep the dimensions exactly so there is no transposition.

Dummy data code that reproduces the problem:

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, TimeDistributed, LSTM

epoch_number = 100
batch_size = 20
input_dim = 4
output_dim = 3
look_back = 24
look_ahead = 24
n = 100

trainX = np.random.rand(n, input_dim, look_back)
trainY = np.random.rand(n, output_dim, look_ahead)
print('test X:', trainX.shape)
print('test Y:', trainY.shape)

model = Sequential()

# Add the first LSTM layer (The intermediate layers need to pass the sequences to the next layer)
model.add(LSTM(10, batch_input_shape=(None, input_dim, look_back), return_sequences=True))

# add the first LSTM layer (the dimensions are only needed in the first layer)
model.add(LSTM(10, return_sequences=True))

# the TimeDistributed object allows a 3D output
model.add(TimeDistributed(Dense(look_ahead)))

model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
model.fit(trainX, trainY, nb_epoch=epoch_number, batch_size=batch_size, verbose=1)

These are the arrows:

Exception: error while checking the target model: it is expected timedistributed_1 to have a form (None, 4, 24), but received an array with the form (100, 3, 24)

The problem is determining the layer TimeDistributed.

How to define a layer TimeDistributedso that it compiles and trains?

+4
2

. node timedistributed_1, node . , , node , , .. trainY.

trainY (n, output_dim, look_ahead), (100, 3, 24), (batch_size, input_dim, look_ahead). , output_dim!= input_dim. , node, .

0

, , output_dim (!= input_dim) TimeDistributed, . , : .

3D, .

TimeDistributed - . , .

4 3, , , - TimeDistributed.

PS: , output_dim , . ( - ), - .

0

Source: https://habr.com/ru/post/1657999/


All Articles