問題1 - 最後一時間步
這是我用它來獲得最後的時間步長的輸出的代碼。我不知道是否有更簡單的解決方案。如果是這樣,我想知道它。我跟着這discussion並抓住我的last_timestep
方法的相關代碼片段。這是我的前鋒。
class BaselineRNN(nn.Module):
def __init__(self, **kwargs):
...
def last_timestep(self, unpacked, lengths):
# Index of the last output for each sequence.
idx = (lengths - 1).view(-1, 1).expand(unpacked.size(0),
unpacked.size(2)).unsqueeze(1)
return unpacked.gather(1, idx).squeeze()
def forward(self, x, lengths):
embs = self.embedding(x)
# pack the batch
packed = pack_padded_sequence(embs, list(lengths.data),
batch_first=True)
out_packed, (h, c) = self.rnn(packed)
out_unpacked, _ = pad_packed_sequence(out_packed, batch_first=True)
# get the outputs from the last *non-masked* timestep for each sentence
last_outputs = self.last_timestep(out_unpacked, lengths)
# project to the classes using a linear layer
logits = self.linear(last_outputs)
return logits
問題2 - 蒙面交叉熵損失
是,默認情況下,補零個時間步長(目標)的問題。但是,掩蓋它們非常容易。你有兩個選擇,取決於你使用的PyTorch版本。
PyTorch 0.2.0:現在pytorch載體直接在CrossEntropyLoss掩蔽,用ignore_index
參數。例如,在語言模型或seq2seq,其中i加零填充,我屏蔽零個填充字(目標)簡單地是這樣的:
loss_function = nn.CrossEntropyLoss(ignore_index = 0)
PyTorch 0.1.12和較老:在較舊版本的PyTorch中,掩蔽不被支持,因此您必須實施自己的解決方法。我使用的解決方案是masked_cross_entropy.py,jihunchoi。您可能也對這discussion感興趣。
我想你的解決方案,我得到了錯誤: 文件 「/root/PycharmProjects/skip-thoughts.torch/pytorch/tmpRNN.py」,13號線,在last_timestep 回報unpacked.gather( 1,idx).squeeze() 文件「/ usr/local/lib/python3。5/dist-packages/torch/autograd/variable.py「,第684行收集 return Gather.apply(self,dim,index) RuntimeError:save_for_backward只能保存輸入或輸出張量,但參數0不會滿足這個條件 – chenfei