我尝试将带有时间戳的能耗数据集和 covid 数据集输入 CNN_M_LSTM 模型(库 Tensorflow 和 Keras API)。带有时间戳的数据集的能耗大小为……
我尝试将带有时间戳数据集和 covid 数据集的能量消耗输入 CNN_M_LSTM 模型(库 Tensorflow 和 Keras API)。
带时间戳的能耗数据集的大小为 (730, 2)
Covid 数据集的大小为 (730, 1)
我想对 covid 数据集和带有时间戳的能耗进行切片和窗口化(批处理大小 = 128,窗口大小为 96)。最后将两个数据集压缩在一起。
这是我的代码片段和窗口,包括能源消耗和时间戳数据集,即 covid 数据集:
MAX_LENGTH = 96
BATCH_SIZE = 128
TRAIN.SHUFFLE_BUFFER_SIZE = 1000
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
以下是我压缩数据集的方法:
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()
我的模型:
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
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)
在尝试窗口化并压缩数据集时,我收到以下错误
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')
我还通过将模型的输入形状更改为
input1 = tf.keras.layers.Input(shape=(128,2), name="input1")
input2 = tf.keras.layers.Input(shape=(128), name="input2")
The error appear as expected 2 dims but received 3 dims.
我的期望是将 zip 数据集输入到我的 CNN_M_LSTM 模型中。