pytorch初始化RNN权重的方法

RNN的weight和bias封装在parameters中,且需要对weight和bias分开初始化,否则会报如下错误!

Fan in and fan out can not be computed for tensor with fewer than 2 dimensions

在模型定义的__init__函数中进行初始化:

self.rnn = nn.LSTM(input_size=embedding_size, hidden_size=128, num_layers=1, bidirectional=False)
for name, param in self.rnn.named_parameters():
	if name.startswith("weight"):
		nn.init.xavier_normal_(param)
	else:
		nn.init.zeros_(param)

你可能感兴趣的:(自然语言处理,pytorch,深度学习,神经网络)