2017-05-28 131 views
3

我在嘗試加載保存的模型時出現以下錯誤。KeyError:'unexpected key'module.encoder.embedding.weight「in state_dict'

KeyError: 'unexpected key "module.encoder.embedding.weight" in state_dict'

這是我使用加載保存的模型的功能。

def load_model_states(model, tag): 
    """Load a previously saved model states.""" 
    filename = os.path.join(args.save_path, tag) 
    with open(filename, 'rb') as f: 
     model.load_state_dict(torch.load(f)) 

該模型是一個序列到序列的網絡,其初始函數(構造函數)在下面給出。

def __init__(self, dictionary, embedding_index, max_sent_length, args): 
    """"Constructor of the class.""" 
    super(Sequence2Sequence, self).__init__() 
    self.dictionary = dictionary 
    self.embedding_index = embedding_index 
    self.config = args 
    self.encoder = Encoder(len(self.dictionary), self.config) 
    self.decoder = AttentionDecoder(len(self.dictionary), max_sent_length, self.config) 
    self.criterion = nn.NLLLoss() # Negative log-likelihood loss 

    # Initializing the weight parameters for the embedding layer in the encoder. 
    self.encoder.init_embedding_weights(self.dictionary, self.embedding_index, self.config.emsize) 

當我打印模型(序列到序列網絡),我得到以下內容。

Sequence2Sequence (
    (encoder): Encoder (
    (drop): Dropout (p = 0.25) 
    (embedding): Embedding(43723, 300) 
    (rnn): LSTM(300, 300, batch_first=True, dropout=0.25) 
) 
    (decoder): AttentionDecoder (
    (embedding): Embedding(43723, 300) 
    (attn): Linear (600 -> 12) 
    (attn_combine): Linear (600 -> 300) 
    (drop): Dropout (p = 0.25) 
    (out): Linear (300 -> 43723) 
    (rnn): LSTM(300, 300, batch_first=True, dropout=0.25) 
) 
    (criterion): NLLLoss (
) 
) 

所以,module.encoder.embedding是一個埋層,並且module.encoder.embedding.weight表示所述關聯權重矩陣。那麼,它爲什麼說 - unexpected key "module.encoder.embedding.weight" in state_dict

回答

6

我解決了這個問題。實際上我使用nn.DataParallel來保存模型,它將模型存儲在模塊中,然後我試圖在沒有DataParallel的情況下加載模型。因此,無論是爲了加載目的,我需要在我的網絡中暫時添加一個nn.DataParallel,或者我可以加載權重文件,創建一個沒有模塊前綴的新的有序字典,然後加載它。

第二種解決方法如下所示。

# original saved file with DataParallel 
state_dict = torch.load('myfile.pth.tar') 
# create new OrderedDict that does not contain `module.` 
from collections import OrderedDict 
new_state_dict = OrderedDict() 
for k, v in state_dict.items(): 
    name = k[7:] # remove `module.` 
    new_state_dict[name] = v 
# load params 
model.load_state_dict(new_state_dict) 

參考:https://discuss.pytorch.org/t/solved-keyerror-unexpected-key-module-encoder-embedding-weight-in-state-dict/1686