Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytorch hidden state LSTM

Why do we need to initialize the hidden state h0 in LSTM in pytorch. As h0 will anyways be calculated and get overwritten ? Isn't it like

int a a = 0

a = 4

Even if we do not do a=0, it should be fine..

like image 528
sww Avatar asked May 04 '26 09:05

sww


1 Answers

The point is that you are able to supply the initial state, it is a feature. They could have implemented it as a default but by letting you control the allocation of the tensor you can save some memory (allocating once, zeroing on every invocation).

Why would you need to set h? Sequence-to-sequence models require this (compress input to one vector, use this vector as hidden state for the decoder) or you might want to make the initial state learnable.

like image 135
nemo Avatar answered May 07 '26 06:05

nemo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!