当数据不变时,如何设置 keras.layers.SimpleRNN 的 'input_shape'?

How can I set 'input_shape' of keras.layers.SimpleRNN, when Data is unvariate?

我正在尝试使用 RNN 进行时间序列预测,但是在 keras.layers.SimpleRNN

'input_shape' 连续出现错误

但是我无法解决,所以想请教一个问题

首先,下面是代码。这是错误消息:

ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 1)
# X_train.shape = (58118,)
# y_train.shape = (58118,)

X_train, X_test, y_train, y_test = train_test_split(x,y,test_size=0.2,shuffle=False,random_state=1004)

X_train,X_val,y_train,y_val = train_test_split(X_train,y_train,test_size=0.125,shuffle=False,random_state=1004)

print(X_train.shape)
print(y_train.shape)

with tf.device('/gpu:0'):
    model = keras.models.Sequential([
        keras.layers.SimpleRNN(20, return_sequences=True, input_shape=[None,1]),
        keras.layers.SimpleRNN(20, return_sequences=True),
        keras.layers.TimeDistributed(keras.layers.Dense(10))
    ])

    model.compile(loss="mse", optimizer="adam")
    history = model.fit(X_train, y_train, epochs=20,validation_data=(X_val, y_val)) #Error
    model.save('rnn.h5')

SimpleRNN 需要输入:一个 3D 张量,形状为 [batch, timesteps, feature]

示例代码

inputs = np.random.random([32, 10, 8]).astype(np.float32)
simple_rnn = tf.keras.layers.SimpleRNN(4)

output = simple_rnn(inputs)  

输出的形状为 [32, 4]