(ValueError)如何在RNN中设置数据形状?

时间:2019-07-14 05:04:50

标签: python lstm recurrent-neural-network

我在RNN模型中的数据形状有问题。

y_pred = model.predict(X_test_re) # X_test_re.shape (35,1,1)

它返回了如下错误。
ValueError:在有状态网络中,您应仅传递带有大量样本的输入,这些样本可以除以批处理大小。找到:35个样本。批量大小:32。

  1. 第一个问题
    我不明白,因为我定义了batch_size = 10,但是为什么错误消息会显示批处理大小:32?
  2. 第二个问题
    当我如下修改代码时
model.predict(X_test_re[:32])

我也收到了一条错误消息,但我不知道这意味着什么。
InvalidArgumentError:不兼容的形状:[32,20]与[10,20]      [[{{node lstm_1 / while / add_1}}]

我建立了一个模型并将其拟合如下。

features = 1
timesteps = 1
batch_size = 10

K.clear_session()
model=Sequential()
model.add(LSTM(20, return_sequences=True, stateful=True,
               batch_input_shape=(batch_size, timesteps, features)))
model.add(LSTM(20, stateful=True))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()

enter image description here

earyl_stop = EarlyStopping(monitor='val_loss', patience=5, verbose=1)
hist = model.fit(X_train_re, y_train, # X_train_re.shape (70,1,1), y_train(70,)
                 batch_size=batch_size,
                 epochs=100,
                 verbose=1,
                 shuffle=False,
                 callbacks=[earyl_stop])

直到适合模型,它才能正常工作。

+)源代码
首先,df看起来像
enter image description here

# split_train_test from dataframe
train,test = df[:-35],df[-35:]
# print(train.shape, test.shape) (70, 2) (35, 2)

# scaling
sc = MinMaxScaler(feature_range=(-1,1))
train_sc = sc.fit_transform(train)
test_sc = sc.transform(test)

# Split X,y (column t-1 is X)
X_train, X_test, y_train, y_test = train_sc[:,1], test_sc[:,1], train_sc[:,0], test_sc[:,0]

# reshape X_train
X_train_re = X_train.reshape(X_train.shape[0],1,1)
X_test_re = X_test.reshape(X_test.shape[0],1,1)

0 个答案:

没有答案