I want to use sensor reading to estimate some quantities. My sensor readings in each time step have 9 elements and the out quantities have 4 elements
input_size = (304414,9)
target_size = (304414,4)
I want to create a RNN model to estimate the output.
For preprocessing and create data set, I used this function
def create_dataset(dataset, look_back):
dataX, dataY = [], []
for i in range(len(dataset)-look_back):
dataX.append(dataset[i:(i+look_back)]) # all 22 columns for X
dataY.append(dataset[i + look_back, 9:14]) # first 8 columns for Y, just as an example
return np.array(dataX), np.array(dataY)
data = np.concatenate((input, output), axis=1)
X, Y = create_dataset(data, 1)
Now, my data set has been changed to input_size = (304413,1,13) target_size = (304413,4)
The model which I used is
model=Sequential()
model.add(Embedding(1, 13, input_length=304413))
model.add(LSTM(12, input_shape=(304413,1,13), kernel_initializer='normal',activation='relu',return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(12, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(4, activation='relu'))
model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),metrics=['accuracy','mse'])
But after trying to use model.fit
I encountered with this error
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential_20" is incompatible with the layer: expected shape=(None, 304413), found shape=(None, 1, 13)
What is my mistake?
I think you simply have to set the input_shape
inside the first layer added to your sequential model. In this case would be:
model=Sequential()
model.add(Embedding(1, 13, input_length=304413, input_shape=(304413,1,13)))
model.add(LSTM(12, kernel_initializer='normal',activation='relu',return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(12, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(4, activation='relu'))
model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),metrics=['accuracy','mse'])
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.