简体   繁体   中英

Tensorflow: Incompatible shapes: [32,12] vs. [32,4]

I've got myself into a bit of trouble. I've got 4 features, and I want to predict each one of them at the same time. My lookback is 12 and I want to predict 12 timesteps ahead. Is it possible to predict all the 4 targets in parallel?

I have to following piece of code. Shape on train_df is (40000, 4) and val_df is (8000, 4).

win_length=12
batch=32
n_features=4

train_generator = TimeseriesGenerator(train_df, train_df, length=win_length, sampling_rate=1, batch_size=batch)
val_generator = TimeseriesGenerator(val_df, val_df, length=win_length, sampling_rate=1, batch_size=batch)

model = Sequential()
model.add(LSTM(128, activation='tanh', input_shape=(win_length, n_features), return_sequences=True))
model.add(LSTM(128, activation='tanh', return_sequences=True))
model.add(LSTM(64, activation='tanh', return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(loss='mse', optimizer='adam')
model.summary()

model.fit_generator(train_generator, validation_data=val_generator)

I get the following error from the fit_generator-function, and I can't seem to figure out how so. Any ideas?

tensorflow.python.framework.errors_impl.InvalidArgumentError:  Incompatible shapes: [32,12] vs. [32,4]

I don't quite understand how TimeseriesGenerator works, but it seems like you want to have two same sequences for data and target, hence the mse . I've checked myself and TimeseriesGenerator doesn't produce two same sequences (data with shape (32, 12, 4) and targets with shape (32, 4)). My best bet right now is to implement the generator manually. Also I think model.add(TimeDistributed(Dense(1))) should be changed to model.add(TimeDistributed(Dense(4)))

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