LSTM ValueError:形状为(879,1)的不可广播的输出操作数与广播形状(879,18)不匹配

时间:2019-12-16 12:21:35

标签: python tensorflow machine-learning deep-learning lstm

我正在尝试在我的数据集上拟合LSTM模型。当我运行代码时,它给出ValueError:形状为(879,1)的不可广播的输出操作数与广播形状(879,18)不匹配。

Traceback (most recent call last):
  File "C:/Users/SCS/Desktop/RNN/RecurentNueralNetwork.py", line 54, in <module>
    trainPredict = scaler.inverse_transform(trainPredict)
  File "C:\Users\SCS\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 406, in inverse_transform
    X -= self.min_
ValueError: non-broadcastable output operand with shape (879,1) doesn't match the broadcast shape (879,18)

代码在这里:

# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = numpy.reshape(testX, (testX.shape[0], testX.shape[1], 1))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(look_back, 1)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=4, batch_size=1, verbose=2)
# make predictions
trainPredict = model.predict(trainX)         // here is error 
testPredict = model.predict(testX)
print("Train predict shape",testPredict.shape)
# invert

trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])

1 个答案:

答案 0 :(得分:0)

请在第54行之前试用以下几行:

train_predict_new = np.zeros(shape=(len(train_predict), 18))
train_predict_new[:,0] = train_predict[:,0]
trainPredict = scaler.inverse_transform(train_predict_new )[:,0]

希望这会有所帮助。