Trouble Settign GRU Input Sizes in Keras Model Stack

101 Views Asked by At

I'm very new to machine learning and Keras. I'm attempting predictive values on a time series with 9 input features and 1 output feature.

I have input data of shape:(5787, 9) --> 5787 data points, 9 features per sample I have target data of shape:(5787,) --> 5787 target data points

My model looks like this and compiles just fine:

# Build Model
model = Sequential()
model.add(layers.Dense(units=(features * neuron_per_feature), 
                # input_shape=input_data.shape,  
                activation = 'relu',
                name='dense1',
                )) 
# First GRU Layer
# input shape should be form (batch, window_size, features)
model.add(layers.GRU(features * neuron_per_feature, 
              #  input_shape=(5787, features * neuron_per_feature),
               dropout=dropout,
               return_sequences=True,
               name='gru1',
               ))
model.add(layers.Dense(features * (neuron_per_feature /  2), 
                activation = 'relu',
                name='dense2',
                ))
model.add(layers.GRU(features * 4,
              #  input_shape=(window_size * features, ),
               dropout=dropout,
               return_sequences=False,
               name='gru2',
               ))
model.add(layers.Dense(1,
          name='dense3',
          ))

# Configure Adam optimizer
opt = keras.optimizers.Adam(
    learning_rate=learning_rate,
    beta_1=0.9,
    beta_2=0.98,
    epsilon=1e-9)

# Compile Model
model.compile(optimizer=opt, loss='mse') # mse = mean squared error
# model.summary()

I try to train the model like this:

`# Fit network
history = model.fit(
    x=input_data,
    y=target_data,
    validation_split=0.0,
    batch_size=batch_size,
    epochs=epochs,
    verbose="auto",
    # validation_data=(test_X, test_Y),
    shuffle=False,
    workers=2,
    use_multiprocessing=True,
)`

However, I continually get errors when adding in the GRU layers:

`<ipython-input-62-76971a07670d> in <module>
      1 # Fit network
----> 2 history = model.fit(
      3     x=input_data,
      4     y=target_data,
      5     validation_split=0.0,

1 frames
/usr/local/lib/python3.8/dist-packages/keras/engine/training.py in 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 "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1040, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1030, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 889, in train_step
        y_pred = self(x, training=True)
    File "/usr/local/lib/python3.8/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.8/dist-packages/keras/engine/input_spec.py", line 214, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" '

    ValueError: Exception encountered when calling layer "sequential_19" (type Sequential).
    
    Input 0 of layer "gru1" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 108)
    
    Call arguments received by layer "sequential_19" (type Sequential):
      • inputs=tf.Tensor(shape=(None, 9), dtype=float32)
      • training=True
      • mask=None`

Any help would be much appreciated!

I've tried disabling the GRU layers, and the model will run the fit training. But when I re-add the GRUs, it fails.

0

There are 0 best solutions below