I found the following piece of code in one of the example of pytorch. Its from an EncoderRNN
class.
def init_weights(self, bsz):
"""Initialize weight parameters for the encoder."""
weight = next(self.parameters()).data
num_directions = 2 if self.bidirectional else 1
if self.rnn_type == 'LSTM':
return (Variable(weight.new(self.n_layers * num_directions, bsz, self.hidden_size).zero_()),
Variable(weight.new(self.n_layers * num_directions, bsz, self.hidden_size).zero_()))
else:
return Variable(weight.new(self.n_layers * num_directions, bsz, self.hidden_size).zero_())
I have two question.
- What is happening in this statement -
weight = next(self.parameters()).data
? - What does
weight.new()
mean? Why we can’t create a Variable without usingweight.new()
? Are we somehow attaching the hidden variables to the model (encoder) parameters?