简体   繁体   English

LSTM中Keras输出尺寸不匹配

[英]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. 该模型必须预测未来11天的产品销售量。

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. 数据集的格式如下:Productid,Sales_on_date_1,........ Sales_on_date_142我已将前131个样本作为产品的功能集,并将11个样本作为标签。

There are in total 1636 products. 总共有1636个产品。 I have modelled this as a multivariate multistep time series forecasting. 我将其建模为多元多步时间序列预测。

There are 142 time steps. 有142个时间步。

There is 1 sample for each product. 每个产品有1个样品。

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) ValueError:检查目标时出错:预期density_3具有形状(131,11),但数组具有形状(11,1636)

I am doing LSTM for the first time. 我是第一次做LSTM。 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) . 由于您想一次预测一种产品,因此训练数据的形状(#ofSamples, sizeOfSample, sampleDimensions)为您的情况(1636, 311, 1) (#ofSamples, sizeOfSample, sampleDimensions) ,并且标签也应为相应的形状(#ofSamples, sizeOfSample, sampleDimensions) (1636, 11) This means you don't have to reshape your data, just need to add a dimension to X . 这意味着您不必重塑数据,只需向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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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