Search code examples
tensorflowkerasrecurrent-neural-network

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


I am trying to do time-series forecasting using RNN, but an error continuously occurred in 'input_shape' of keras.layers.SimpleRNN,

but I could not solve it, so I would like to ask a question.

First of all, below is the code. and This is Error 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')

Solution

  • SimpleRNN expects inputs: A 3D tensor, with shape [batch, timesteps, feature]

    Sample Code

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

    Output has shape [32, 4].