【发布时间】:2021-10-23 01:51:01
【问题描述】:
我正在尝试使用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')
【问题讨论】:
-
它需要一个 3D 输入为
(batch_size, n_timesteps, n_features)但是你传递了一个形状为(58118,). 的数组,它是一个 1D 数组。 -
将数据重塑为
(1, -1, 1)。
标签: tensorflow keras recurrent-neural-network