在PyTorch 1.6中使用GRU时出现retain_graph问题

我知道,在使用loss.backward()时,如果有多个网络和多个损失函数需要分别优化每个网络,我们需要指定retain_graph=True。但即使指定(或不指定)这个参数,我还是会遇到错误。以下是一个最小工作示例(MWE),用于重现问题(在PyTorch 1.6上)。

retain_graph设置为True时,我会得到以下错误:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [100, 300]], which is output 0 of TBackward, is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

如果不使用该参数,得到的错误是:

RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling backward the first time.

这是预期的错误。

请指出上述代码中需要做哪些更改才能开始训练。任何帮助都将不胜感激。


回答:

在这种情况下,可以断开计算图以排除不需要优化的参数。在本例中,应在第二次使用gru1进行前向传播后断开计算图,即:

....gru1_opt.step()gru1_output, _ = gru1(vector)gru1_output = gru1_output.detach()....

这样,您就不会“尝试第二次通过图进行反向传播”,如错误消息中提到的。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注