Here is the code.
class EncoderBiLSTM(nn.Module):
def __init__(self, hidden_size, pretrained_embeddings):
super().__init__()
self.hidden_size = hidden_size
self.embedding_dim = pretrained_embeddings.shape[1]
self.vocab_size = pretrained_embeddings.shape[0]
self.num_layers = 2
self.dropout = 0.1 if self.num_layers > 1 else 0
self.bidirectional = True
self.embedding = nn.Embedding(self.vocab_size, self.embedding_dim)
self.embedding.weight.data.copy_(torch.from_numpy(pretrained_embeddings))
self.embedding.weight.requires_grad = False # 冻结,无需训练
self.lstm = nn.LSTM(self.embedding_dim,
self.hidden_size,
batch_first = True,
dropout = self.dropout,
bidirectional = self.bidirectional)
identity_init = torch.eye(self.hidden_size)
'''
here will report wrong.
AttributeError: 'LSTM' object has no attribute 'weight_hh_l1'
'''
self.lstm.weight_hh_l0.data.copy_(torch.cat([identity_init]*4, dim=0))
self.lstm.weight_hh_l1.data.copy_(torch.cat([identity_init]*4, dim=0))
self.lstm.weight_hh_l0_reverse.data.copy_(torch.cat([identity_init]*4, dim=0))
self.lstm.weight_hh_l1_reverse.data.copy_(torch.cat([identity_init]*4, dim=0))
...
lstm =nn.LSTM(...)
print(lstm.state_dict())
I have print the LSTM object’s state_dict(),‘weight_hh_l0’ is exactly there.
Is anyone coudl help me?