ValueError:尺寸必须等于RNN -KERAS

发布于 2025-02-13 06:21:24 字数 4001 浏览 1 评论 0原文

我正在构建具有不同x和y长度的RNN模型。
从NLP中,我知道这应该是可能的(即 - 如果您有翻译模型,您将没有相同的长度输入句子/单词和输出句子/单词...)

不幸的是,这对我不起作用,我收到以下错误:(

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].

下面的完整错误)

我也在线检查并找到这个线程 Chollet本人似乎在2021年参与并关闭了该线程,但是那里的最后一条评论(2019年)没有解决,并且与我有同样的问题。

我正在使用Google Colab,因此可以肯定的是,它不是旧版本的问题(tf .__版本__ ==> 2.8.2 and tf.keras .__ version _____版本__ ==> 2.8 .0

我的模型是:

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)

如何解决问题


---------------------------------------------------------------------------
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].

I'm constructing an RNN model with different X and Y lengths.
from NLP I know that this should be possible (i.e. - 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].

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未蓝澄海的烟 2025-02-20 06:21:24

RNN单元格需要相同长度的输入和标签。

RNN为每个相应的输入产生输出。无论您使用RNN,GRU,LSTM等。RNN都采用序列,然后从序列中的第一个元素开始,它会生成输出和隐藏状态。它存储输出,然后将隐藏状态和序列中的下一个元素传递,并生成下一个输出和新的隐藏状态。一旦完成了序列中的所有元素,它将在每个时间步和最终隐藏状态下为您提供输出。因此,输入和标签的长度确实需要相同。

RNN翻译通过编码输入,然后将编码状态传递到依次解码它的解码器来起作用。 IE使用RNN的翻译不是完成整个工作的单个RNN。

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. I.e. translation using RNNs is not done with a single RNN doing the entire work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文