如何获得Fastai文本模型的倒数第二层输出?

时间:2019-05-03 11:21:47

标签: python-3.x nlp transfer-learning fast-ai

learn = text_classifier_learner(data_clas, AWD_LSTM, drop_mult=0.7)
learn.fit_one_cycle(1, 1e-2)

我已经按照上面训练过fastai模型。我可以得到如下预测

preds, targets = learn.get_preds()

但是我反而想要模型learn的倒数第二层嵌入(这种做法在CNN模型中很常见)。你能帮我怎么做吗?

1 个答案:

答案 0 :(得分:0)

我不确定您是否要使用分类器,但是无论如何...

learn.model为您提供了模型架构。那么learn.model[0]将是模型另一部分的learn.model[1]编码器。

示例:

要访问SequentialEx(以下架构)中的第一个线性层,请使用以下命令进行操作 learn.model[0].layers[0].ff.layers[0]

SequentialRNN(
    (0): TransformerXL(
      (encoder): Embedding(60004, 410)
      (pos_enc): PositionalEncoding()
      (drop_emb): Dropout(p=0.03)
      (layers): ModuleList(
        (0): DecoderLayer(
          (mhra): MultiHeadRelativeAttention(
            (attention): Linear(in_features=410, out_features=1230, bias=False)
            (out): Linear(in_features=410, out_features=410, bias=False)
            (drop_att): Dropout(p=0.03)
            (drop_res): Dropout(p=0.03)
            (ln): LayerNorm(torch.Size([410]), eps=1e-05, elementwise_affine=True)
            (r_attn): Linear(in_features=410, out_features=410, bias=False)
          )
          (ff): SequentialEx(
            (layers): ModuleList(
              (0): Linear(in_features=410, out_features=2100, bias=True)
              (1): ReLU(inplace)
              (2): Dropout(p=0.03)
              (3): Linear(in_features=2100, out_features=410, bias=True)
              (4): Dropout(p=0.03)
              (5): MergeLayer()
              (6): LayerNorm(torch.Size([410]), eps=1e-05, elementwise_affine=True)
            )
          )
        )