Jin*_* Gu 0 neural-network lstm pytorch
在训练时,最好初始化隐藏状态而不是将其设置为 0。但我想知道在验证和测试时初始化隐藏状态是好是坏。谢谢
绝对没有理由自定义将隐藏状态初始化为零;实际情况是这样的:
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)
它首先检查您是否传递了任何自定义隐藏状态值,如果没有,它会将其初始化为零。
此外,从理论上讲,您通常不需要在测试模式下初始化模型的隐藏状态(随机或使用预定义值)。
归档时间: |
|
查看次数: |
2491 次 |
最近记录: |