I try to feed the the energy consumption with timestamp dataset and covid dataset into the CNN_M_LSTM model (library Tensorflow and Keras API).
Energy consumption with timestamp dataset has the size (730, 2)
Covid dataset has the size (730, 1)
I want to slice and window both covid dataset, Energy consumption with timestamp ( with batch size = 128 and window size is 96). Finally zip both dataset together.
Here is my code slice and window both the energy consumption and timestamp dataset, the covid dataset:
TRAIN.SHUFFLE_BUFFER_SIZE = 1000
<code>MAX_LENGTH = 96
BATCH_SIZE = 128
TRAIN.SHUFFLE_BUFFER_SIZE = 1000
</code>
MAX_LENGTH = 96
BATCH_SIZE = 128
TRAIN.SHUFFLE_BUFFER_SIZE = 1000
<code>def windowed_dataset(series, 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= tf.data.Dataset.from_tensor_slices(series)
dataset = dataset.window(window_size + 1, shift=1)
dataset = dataset.flat_map(lambda window: window.batch(window_size + 1)) # the order dataset stays the same
dataset = dataset.shuffle(1000)
dataset = dataset.map(lambda window: (window[:-1], window[-1][0]))
dataset = dataset.padded_batch(128,drop_remainder=True).cache()
<code>def windowed_dataset(series, 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= tf.data.Dataset.from_tensor_slices(series)
dataset = dataset.window(window_size + 1, shift=1)
dataset = dataset.flat_map(lambda window: window.batch(window_size + 1)) # the order dataset stays the same
dataset = dataset.shuffle(1000)
dataset = dataset.map(lambda window: (window[:-1], window[-1][0]))
dataset = dataset.padded_batch(128,drop_remainder=True).cache()
return dataset
</code>
def windowed_dataset(series, 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= tf.data.Dataset.from_tensor_slices(series)
dataset = dataset.window(window_size + 1, shift=1)
dataset = dataset.flat_map(lambda window: window.batch(window_size + 1)) # the order dataset stays the same
dataset = dataset.shuffle(1000)
dataset = dataset.map(lambda window: (window[:-1], window[-1][0]))
dataset = dataset.padded_batch(128,drop_remainder=True).cache()
return dataset
Here how I zip the dataset:
train_energy_dataset = windowed_dataset(TRAIN.PRE_PROCESSED_SERIES)
train_covid_dataset = windowed_dataset(TRAIN.SERIES_COVID)
train_dataset = tf.data.Dataset.zip((train_covid_dataset,train_energy_dataset))
train_dataset = train_dataset.shuffle(buffer_size=1000)
train_dataset = train_dataset.map(lambda data1, data2: ((data1[0], data2[0]), data1[1])) # Adjust mapping as per your need
train_dataset = train_dataset.batch(128, drop_remainder=True).cache()
<code>
train_energy_dataset = windowed_dataset(TRAIN.PRE_PROCESSED_SERIES)
train_covid_dataset = windowed_dataset(TRAIN.SERIES_COVID)
train_dataset = tf.data.Dataset.zip((train_covid_dataset,train_energy_dataset))
train_dataset = train_dataset.shuffle(buffer_size=1000)
train_dataset = train_dataset.map(lambda data1, data2: ((data1[0], data2[0]), data1[1])) # Adjust mapping as per your need
train_dataset = train_dataset.batch(128, drop_remainder=True).cache()
</code>
train_energy_dataset = windowed_dataset(TRAIN.PRE_PROCESSED_SERIES)
train_covid_dataset = windowed_dataset(TRAIN.SERIES_COVID)
train_dataset = tf.data.Dataset.zip((train_covid_dataset,train_energy_dataset))
train_dataset = train_dataset.shuffle(buffer_size=1000)
train_dataset = train_dataset.map(lambda data1, data2: ((data1[0], data2[0]), data1[1])) # Adjust mapping as per your need
train_dataset = train_dataset.batch(128, drop_remainder=True).cache()
My model:
def create_CNN_LSTM_model():
input1 = tf.keras.layers.Input(shape=(128,1), name="input1")
input2 = tf.keras.layers.Input(shape=(128,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)
# Concatenate the outputs of LSTM and Dense layers
concatenated = tf.keras.layers.Concatenate(axis=1)([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)
<code>
def create_CNN_LSTM_model():
# Define the inputs
input1 = tf.keras.layers.Input(shape=(128,1), name="input1")
input2 = tf.keras.layers.Input(shape=(128,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)
# Concatenate the outputs of LSTM and Dense layers
concatenated = tf.keras.layers.Concatenate(axis=1)([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
</code>
def create_CNN_LSTM_model():
# Define the inputs
input1 = tf.keras.layers.Input(shape=(128,1), name="input1")
input2 = tf.keras.layers.Input(shape=(128,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)
# Concatenate the outputs of LSTM and Dense layers
concatenated = tf.keras.layers.Concatenate(axis=1)([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
<code>model_cnn_m_lstm = create_CNN_LSTM_model()
model_cnn_m_lstm.compile(
loss=tf.keras.losses.Huber(),
optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
model_cnn_m_lstm.summary()
model_cnn_m_lstm.fit(train_dataset, epochs=100, batch_size=128)
<code>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)
</code>
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)
With the trial to window and zip the datasets, I receive error as
<code>ValueError: Exception encountered when calling Functional.call().
Invalid input shape for input Tensor("functional_100_1/Cast_1:0", shape=(128, 128, None, 2), dtype=float32). Expected shape (None, 128, 2), but input has incompatible shape (128, 128, None, 2)
Arguments received by Functional.call():
• inputs=('tf.Tensor(shape=(128, 128, None, 1), dtype=float64)', 'tf.Tensor(shape=(128, 128, None, 2), dtype=float64)')
<code>ValueError: Exception encountered when calling Functional.call().
Invalid input shape for input Tensor("functional_100_1/Cast_1:0", shape=(128, 128, None, 2), dtype=float32). Expected shape (None, 128, 2), but input has incompatible shape (128, 128, None, 2)
Arguments received by Functional.call():
• inputs=('tf.Tensor(shape=(128, 128, None, 1), dtype=float64)', 'tf.Tensor(shape=(128, 128, None, 2), dtype=float64)')
• training=True
• mask=('None', 'None')
</code>
ValueError: Exception encountered when calling Functional.call().
Invalid input shape for input Tensor("functional_100_1/Cast_1:0", shape=(128, 128, None, 2), dtype=float32). Expected shape (None, 128, 2), but input has incompatible shape (128, 128, None, 2)
Arguments received by Functional.call():
• inputs=('tf.Tensor(shape=(128, 128, None, 1), dtype=float64)', 'tf.Tensor(shape=(128, 128, None, 2), dtype=float64)')
• training=True
• mask=('None', 'None')
I also fixed by change the input shape of the model to
<code> input1 = tf.keras.layers.Input(shape=(128,2), name="input1")
input2 = tf.keras.layers.Input(shape=(128), name="input2")
<code> input1 = tf.keras.layers.Input(shape=(128,2), name="input1")
input2 = tf.keras.layers.Input(shape=(128), name="input2")
</code>
input1 = tf.keras.layers.Input(shape=(128,2), name="input1")
input2 = tf.keras.layers.Input(shape=(128), name="input2")
<code>The error appear as expected 2 dims but received 3 dims.
<code>The error appear as expected 2 dims but received 3 dims.
</code>
The error appear as expected 2 dims but received 3 dims.
My expectation is to feed the zip dataset into my CNN_M_LSTM model.