简体   繁体   中英

ValueError: Dimensions must be equal RNN - Keras

I'm constructing an RNN model with different X and Y lengths.
from NLP I know that this should be possible (ie - if you have a model for translations, you will not have same length input sentences/words and output sentences/words...)

Unfortunately this does not work for me, I get the following error:

ValueError: Dimensions must be equal, but are 3 and 405 for '{{node mean_absolute_error/sub}} = Sub[T=DT_FLOAT](sequential_47/time_distributed_46/Reshape_1, IteratorGetNext:1)' with input shapes: [?,3,1], [?,405,1].

(full error below)

I checked also online and found this thread where Chollet himself seems to be involved and closed it in 2021, but the last comment there (2019) is not resolved and has same issue as me.

I'm using google colab and therefore pretty sure that its not a problem of old versions ( tf.__version__ ==> 2.8.2 and tf.keras.__version__ ==> 2.8.0

my model is:

model_gru = Sequential()
model_gru.add(GRU(75, return_sequences=True,input_shape=(train_X.shape[1], train_X.shape[2])))# , unroll=True))
model_gru.add(GRU(units=30, return_sequences=True))
model_gru.add(TimeDistributed(Dense(1)))
model_gru.compile(loss='mae', optimizer='adam')
model_gru.summary()

gru_history = model_gru.fit(train_X, train_y, epochs=30, batch_size=64, validation_data=(test_X, test_y), shuffle=False)

Any idea how to resolve the issue?


full error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-212-1b00467cbad2> in <module>()
----> 1 gru_history = model_gru.fit(train_X, train_y, epochs=30, batch_size=64, validation_data=(test_X, test_y), shuffle=False, callbacks=[callback])

1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
        y, y_pred, sample_weight, regularization_losses=self.losses)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
        losses = call_fn(y_true, y_pred)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1457, in mean_absolute_error
        return backend.mean(tf.abs(y_pred - y_true), axis=-1)

    ValueError: Dimensions must be equal, but are 3 and 405 for '{{node mean_absolute_error/sub}} = Sub[T=DT_FLOAT](sequential_47/time_distributed_46/Reshape_1, IteratorGetNext:1)' with input shapes: [?,3,1], [?,405,1].

An RNN cell needs inputs and labels that are the same length.

An RNN produces an output for every corresponding input. Whether you're using an RNN, GRU, LSTM, etc. the RNN takes in your sequence, then starting with the first element in your sequence it generates an output and hidden state(s). It stores the output, then passes the hidden state(s) and the next element in your sequence through and generates the next output and a new hidden state(s). Once it's completed this for all elements in your sequence it will provide you the output at each timestep and the final hidden state. So the length of the input and the labels DO need to be the same.

RNN translations work by encoding the input, then passing the encoded state to a decoder that sequentially decodes it. Ie translation using RNNs is not done with a single RNN doing the entire work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM