如何在LSTM中添加dropout层以避免过拟合

在实现混合量子LSTM模型时,模型出现了过拟合,导致准确率低。我尝试在nn.LSTM中设置dropout = 1,但没有改善。我使用了一个隐藏层。我该如何添加dropout层来减少过拟合?

模型参数:

input_dim = 16hidden_dim = 100layer_dim = 1output_dim = 1

模型类:

class LSTMModel(nn.Module):    def __init__(self, input_dim, hidden_dim, layer_dim, output_dim):        super(LSTMModel, self).__init__()        self.hidden_dim = hidden_dim                self.layer_dim = layer_dim        self.lstm = nn.LSTM(input_dim, hidden_dim, layer_dim, dropout=1, batch_first=True, )              self.fc = nn.Linear(hidden_dim, output_dim)        self.hybrid = Hybrid(qiskit.Aer.get_backend('qasm_simulator'), 100, np.pi / 2)    def forward(self, x):        h0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_()        c0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_()                x, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))               x = self.fc(x[:, -1, :])         x = self.hybrid(x)        return T.cat((x, 1 - x), -1)    

回答:

Pytorch的LSTM层接受dropout参数作为层中节点被置零的概率。当你传入1时,它会将整个层置零。我猜你本来是想设定一个常规值,比如0.3或0.5。

正如@ayandas上面所说,它对每个层(除了最后一层)应用dropout(参见上面的链接),因此对于单层LSTM不起作用。如果你愿意,你总可以在LSTM层的输出处使用nn.dropout来应用你自己的dropout。

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中创建了一个多类分类项目。该项目可以对…

发表回复

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