努力应对多元时间序列的输入形状

时间:2019-07-15 19:20:15

标签: python tensorflow neural-network prediction

我正在为ltsm网络的正确设置而苦苦挣扎:

我的数据集包含5622个产品,我每个产品的销售记录都为4年(每月)。此外,数据集包含7个数字特征。我的目标是根据过去的销售情况和其他功能,对每个产品在未来3个月的销售量(y)进行预测。首先,我将数据集分为每个产品的大块,并分配了第一个45分钟的培训时间和最近3个月的测试时间。我的设置如下:

X_Train: (45, 1, 7)
X_Test: (3, 1, 7)
y_train: (45,)
y_test:  (3,)


regressor = Sequential()
regressor.add(LSTM(units=50, return_sequences=True, input_shape=(1,7)))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.5))
regressor.add(LSTM(units=50))
regressor.add(Dropout(0.5))
regressor.add(Dense(units=1))

# Compiling the RNN
regressor.compile(optimizer='rmsprop', loss='mean_squared_error')
# Fitting to the training set
# history ={}
for i in productList:
    print("Fitting to", i)
    regressor.fit(trainset[i]["X"], trainset[i]["y"], epochs=50, batch_size=1)

pred_result = {}
for i in productList:
    y_true = scaler_x[i].inverse_transform(testset[i]["y"].reshape(-1,1))
    y_pred = scaler_y[i].inverse_transform(regressor.predict(testset[i]["X"][0]))
    MSE = mean_squared_error(y_true, y_pred)
    pred_result[i] = {}
    pred_result[i]["True"] = y_true
    pred_result[i]["Pred"] = y_pred
    plt.figure(figsize=(14,6))
    plt.title("{} with MSE {:10.4f}".format(i,MSE))
    plt.plot(y_true)
    plt.plot(y_pred)

在第21行之前一切正常,但是随后出现以下错误:

**ValueError:** non-broadcastable output operand with shape (3,1) doesn't match the broadcast shape (3,8)

因此,显然我的数据形状有问题。有人能给我一个提示我做错了什么吗?可能X_Train和X_Test错误吗?

非常感谢您的支持!

0 个答案:

没有答案