I try to feed the the energy consumption with timestamp dataset and covid dataset into the CNN_M_LSTM model (library Tensorflow).
Energy consumption and timestamp has the size (70082, 2)
Covid dataset has the size (744, 1)
I had used tensor slice and tensor zip to pack both data together and window the dataset:
Here is my code to zip and window both the energy consumption and timestamp dataset, the covid dataset:
MAX_LENGTH = 96
BATCH_SIZE = 128
TRAIN.SHUFFLE_BUFFER_SIZE = 1000
def windowed_dataset(series_energy,series_covid, window_size=MAX_LENGTH, batch_size=BATCH_SIZE, shuffle_buffer=TRAIN.SHUFFLE_BUFFER_SIZE):
"""
We create time windows to create X and y features.
For example, if we choose a window of 30, we will create a dataset formed by 30 points as X
"""
dataset_energy = tf.data.Dataset.from_tensor_slices(series_energy)
dataset_covid = tf.data.Dataset.from_tensor_slices(series_covid)
dataset = tf.data.Dataset.zip(dataset_energy,dataset_covid)
dataset = dataset.window(96 + 1, shift=1) #
dataset = dataset.flat_map(lambda window_covid, window_series: tf.data.Dataset.zip((window_covid, window_series)).batch(96 + 1))
dataset = dataset.shuffle(1000)
dataset = dataset.map(lambda window_covid, window_series: (window_covid[:-1], window_series[-1][0]))
dataset = dataset.padded_batch(128,drop_remainder=True).cache()
return dataset
For the model CNN_M_LSTM, I had created 2 inputs. This is my model:
def create_CNN_LSTM_model():
# Define the inputs
input1 = tf.keras.layers.Input(shape=(96, 1), name="input1")
input2 = tf.keras.layers.Input(shape=(96, 2), name="input2")
# Define the CNN-LSTM part of the model
x = tf.keras.layers.Conv1D(filters=128, kernel_size=3, activation='relu', strides=1, padding="causal")(input1)
x = tf.keras.layers.MaxPooling1D(pool_size=2)(x)
x = tf.keras.layers.Conv1D(filters=64, kernel_size=3, activation='relu', strides=1, padding="causal")(x)
x = tf.keras.layers.MaxPooling1D(pool_size=2)(x)
x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.LSTM(16, return_sequences=True)(x)
x = tf.keras.layers.LSTM(8, return_sequences=True)(x)
x = tf.keras.layers.Flatten()(x)
output_lstm = tf.keras.layers.Dense(1)(x)
# Define the dense part of the model
output_dense_1 = tf.keras.layers.Dense(1)(input2[:, -1, :])
# Concatenate the outputs of LSTM and Dense layers
concatenated = tf.keras.layers.Concatenate()([output_dense_1, output_lstm])
# Add further dense layers
x = tf.keras.layers.Dense(6, activation=tf.nn.leaky_relu)(concatenated)
output = tf.keras.layers.Dense(4)(x)
model_final = tf.keras.Model(inputs=[input1, input2], outputs=output)
# Define the final model
return model_final
How I fit my model:
model_cnn_m_lstm = create_CNN_LSTM_model()
# Compile the model
model_cnn_m_lstm.compile(
loss=tf.keras.losses.Huber(),
optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
metrics=["mse"]
)
model_cnn_m_lstm.summary()
model_cnn_m_lstm.fit(train_dataset, epochs=100, batch_size=128)
For the above trial, the error is model expects 2 input(s), but it received 1 input tensor.
My expectation is to feed 3 inputs into my CNN_M_LSTM model. Also, the 3 inputs dataset must be packed all together, having a window size of 96 and batch size of 128.