简体   繁体   中英

Keras output dimension mismatch in LSTM

I have been working on a sales prediction model. The model has to predict the product sales for the next 11 days.

The dataset is of this form : Productid, Sales_on_date_1,........Sales_on_date_142 I have taken the first 131 samples as feature set for the products and 11 samples as labels.

There are in total 1636 products. I have modelled this as a multivariate multistep time series forecasting.

There are 142 time steps.

There is 1 sample for each product.

My code is as follows:

    X=train_data[:,:131]
    y=train_data[:,131:]
    X=X.reshape((1,131,1636))
    y=y.reshape((1,11,1636)) 
    model=Sequential()
    model.add(LSTM(units=50,return_sequences=True,input_shape=(X.shape[1],X.shape[2])))
    model.add(Dropout(0.2))
    model.add(Dense(units=11))
    model.compile(optimizer = 'adam', loss = 'mean_squared_error')  
    model.fit(X, y, epochs = 100, batch_size = 1)

This is the error I get.

ValueError: Error when checking target: expected dense_3 to have shape (131, 11) but got array with shape (11, 1636)

I am doing LSTM for the first time. Can somebody please help me on how should I model the dimensions of the label data ?

Since you want to predict one product at a time your training data should be of shape (#ofSamples, sizeOfSample, sampleDimensions) which is in your case (1636, 311, 1) and your labels accordingly (1636, 11) . This means you don't have to reshape your data, just need to add a dimension to X . Try this:

X=train_data[:,:131,np.newaxis]
y=train_data[:,131:]
model=Sequential()
model.add(LSTM(units=50,return_sequences=True,input_shape=(X.shape[1],X.shape[2])))
model.add(Dropout(0.2))
model.add(Dense(units=11))
model.compile(optimizer = 'adam', loss = 'mean_squared_error')  
model.fit(X, y, epochs = 100, batch_size = 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM