当数据是可变的时,如何设置 keras.layers.SimpleRNN 的"input_shape"?



我试图使用RNN进行时间序列预测,但keras.layers.SimpleRNN'input_shape'连续发生错误,

但是我不能解决它,所以我想问一个问题。

首先,下面是代码。and This isError Message:

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期望输入:形状为[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]

最新更新