在验证和测试集中是否需要初始化 lstm 隐藏状态?或者只是将其重置为零

Jin*_* Gu 0 neural-network lstm pytorch

在训练时,最好初始化隐藏状态而不是将其设置为 0。但我想知道在验证和测试时初始化隐藏状态是好是坏。谢谢

And*_*uib 6

绝对没有理由自定义将隐藏状态初始化为零实际情况是这样的:

def forward(self, input, hx=None):
    ...

    if hx is None:
        num_directions = 2 if self.bidirectional else 1
        hx = torch.zeros(self.num_layers * num_directions,
                         max_batch_size, self.hidden_size,
                         dtype=input.dtype, device=input.device)
    else:
        # Each batch of the hidden state should match the input sequence that
        # the user believes he/she is passing in.
        hx = self.permute_hidden(hx, sorted_indices)
Run Code Online (Sandbox Code Playgroud)

它首先检查您是否传递了任何自定义隐藏状态值,如果没有,它会将其初始化为零


此外,从理论上讲,您通常不需要测试模式下初始化模型的隐藏状态(随机或使用预定义值)。