LSTM模型在整个训练过程中都持续损失

发布于 2025-02-03 04:54:34 字数 1533 浏览 3 评论 0原文

暹罗模型实施可以在下面找到:

class ClassifierSiameseLSTM(nn.Module):
    def __init__(self, num_sensors=2, hidden_units=16):
        super().__init__()
        self.num_sensors = num_sensors  # this is the number of features
        self.hidden_units = hidden_units
        self.num_layers = 1

        self.lstm = nn.LSTM(
          input_size=num_sensors,
          hidden_size=hidden_units,
          batch_first=True,
          num_layers=self.num_layers)

        self.fc = nn.Linear(in_features=self.hidden_units, out_features=256)

    def forward_once(self, x):
        batch_size = x.shape[0]
        h0 = torch.zeros(
          self.num_layers, batch_size, self.hidden_units,
          dtype=torch.double).to(device).requires_grad_()
        c0 = torch.zeros(self.num_layers, batch_size, self.hidden_units, 
          dtype=torch.double).to(device).requires_grad_()

        output, (hn, cn) = self.lstm(x, (h0, c0))
        out = self.fc(output[:, -1, :])
        return out

    def forward(self,x1,x2):
        output1 = self.forward_once(x1)
        output2 = self.forward_once(x2)

        return output1,output2

当我尝试训练模型时,我得到以下损失值:

Epoch : 0
Train Loss: 0.090 | Accuracy: 64.000

Epoch : 1
Train Loss: 0.091 | Accuracy: 64.000

Epoch : 2
Train Loss: 0.090 | Accuracy: 64.000

Epoch : 3
Train Loss: 0.090 | Accuracy: 64.000

Epoch : 4
Train Loss: 0.091 | Accuracy: 64.000

Epoch : 5
Train Loss: 0.090 | Accuracy: 64.000

损失值几乎是恒定的,并且模型根本没有学习。有人知道解决方案还是对问题的原因有想法?

Siamese Model implementation can be found below:

class ClassifierSiameseLSTM(nn.Module):
    def __init__(self, num_sensors=2, hidden_units=16):
        super().__init__()
        self.num_sensors = num_sensors  # this is the number of features
        self.hidden_units = hidden_units
        self.num_layers = 1

        self.lstm = nn.LSTM(
          input_size=num_sensors,
          hidden_size=hidden_units,
          batch_first=True,
          num_layers=self.num_layers)

        self.fc = nn.Linear(in_features=self.hidden_units, out_features=256)

    def forward_once(self, x):
        batch_size = x.shape[0]
        h0 = torch.zeros(
          self.num_layers, batch_size, self.hidden_units,
          dtype=torch.double).to(device).requires_grad_()
        c0 = torch.zeros(self.num_layers, batch_size, self.hidden_units, 
          dtype=torch.double).to(device).requires_grad_()

        output, (hn, cn) = self.lstm(x, (h0, c0))
        out = self.fc(output[:, -1, :])
        return out

    def forward(self,x1,x2):
        output1 = self.forward_once(x1)
        output2 = self.forward_once(x2)

        return output1,output2

When I try to train the model, I got the following loss values:

Epoch : 0
Train Loss: 0.090 | Accuracy: 64.000

Epoch : 1
Train Loss: 0.091 | Accuracy: 64.000

Epoch : 2
Train Loss: 0.090 | Accuracy: 64.000

Epoch : 3
Train Loss: 0.090 | Accuracy: 64.000

Epoch : 4
Train Loss: 0.091 | Accuracy: 64.000

Epoch : 5
Train Loss: 0.090 | Accuracy: 64.000

The loss value is almost constant and the model is not learning at all. Does anyone know the solution or have an idea about the cause of the problem?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文