1D CNN for spatiotemporal forecasting, confused about input shape

27 Views Asked by At

My dataset contains hourly measurements of NO2 values in the air for 8 days (so 24 * 8 = 192) for 48 sensors (so in total 192 * 48 = 9216 rows). My features consist of the lon and lat of the sensors. I want my model to predict the NO2 values for a particular time at a particular sensor (so the output would be (time, lon, lat, NO2 value)). I'm unsure of what my input should be, currently I'm shaping it as (time, sensors, features) so (192, 9216, 2) but I'm getting an error when I try to fit the model.

I will share my code below, hope someone can help give me some clarity on this! This is also my first time working with CNNs and spatiotemporal data so I'm quite lost on what to do.

This is my model

def define_model_cnn(times, sensors, features):
    """
    Creates and returns a model with 1D CNN layers. Input data is expected to have shape (times, sensors, features).
    """

    model = Sequential()
    model.add(TimeDistributed(Conv1D(filters=32, kernel_size=3, activation='relu', input_shape=(times, sensors, features))))
    model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
    model.add(TimeDistributed(Flatten()))
    
    model.add(Dense(1))

    model.compile(optimizer='adam', loss='mse')

    return model
model = define_model_cnn(time, sensors, features)
model.fit(df, y, epochs = 50)
ValueError                                Traceback (most recent call last)
Cell In[11], line 1
----> 1 model.fit(df, y, 
      2            epochs = 50)

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File /var/folders/gl/hsh292j13gdfv_d1vkbvt7fw0000gn/T/__autograph_generated_file2lbzcq47.py:15, in outer_factory..inner_factory..tf__train_function(iterator)
     13 try:
     14     do_return = True
---> 15     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16 except:
     17     do_return = False

ValueError: in user code:

    File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/src/engine/training.py", line 1401, in train_function  *
        return step_function(self, iterator)
...
    Call arguments received by layer 'time_distributed' (type TimeDistributed):
      • inputs=tf.Tensor(shape=(32, 48, 2), dtype=float32)
      • training=True
      • mask=None
0

There are 0 best solutions below