简体   繁体   中英

Shape error for Vanilla RNN's dense layer in Keras?

I am trying to do the timeseries forecasting using Vanilla RNN (No LSTM).

Error when checking target: expected dense_2 to have shape (2,) but got array with shape (1,)

I have learned LSTM time forecasting from the link here . I tried and tested it using my own data set. Now I want to implement timeseries using RNN to learn myself (and also to compare difference between LSTM and Vanilaa RNN). But I face the above error.

With my research over web, I have figured out that the problem is with selecting the right error function (I suppose). But I am not sure. Following is my code snippet.

Note that since it is time series forecasting Y(t) = X(T-1)

#X_train.shape = (7141, 1)
#y_train.shape = (7141, 1)
model = Sequential()
model.add(Dense(5, activation='relu'))
model.add(Dense(2))
model.compile(loss='mean_absolute_error', optimizer = 'adam')
history = model.fit(X_train, y_train, epochs=10, batch_size=64, verbose=1, shuffle=False)

The problem is, that your output has -- just as the error states -- shape (2,) , because it's produced by a Dense(2) layer. If you replace it with Dense(1) , shapes will match.

However note that there is nothing recurrent yet and you're just modeling y(t_i) as a function of x(t_i) , ie a single previous sample.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM