在實作混合量子 LSTM 模型時,該模型過度擬合,因此精度較低。我試著設定dropout = 1中nn.LSTM,但沒有改善。我使用了一個隱藏層。如何添加 dropout 層以減少過擬合?
型號引數:
input_dim = 16
hidden_dim = 100
layer_dim = 1
output_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)
uj5u.com熱心網友回復:
Pytorch 的LSTM 層將dropout引數作為該層的節點歸零的概率。當您傳遞 1 時,它會將整個圖層歸零。我假設您打算將其設為常規值,例如 0.3 或 0.5。
正如@ayandas 上面所說,它也將 dropout 應用于除最后一層之外的每一層(請參閱上面的鏈接),因此它不適用于單層 LSTM。如果您愿意,您始終可以在 LSTM 層的輸出中使用nn.dropout應用您自己的 dropout。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/323199.html
