fastNLP.models

fastNLP.models.base_model

class fastNLP.models.base_model.BaseModel[source]

Base PyTorch model for all models.

class fastNLP.models.base_model.NaiveClassifier(in_feature_dim, out_feature_dim)[source]
forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

fastNLP.models.biaffine_parser

class fastNLP.models.biaffine_parser.ArcBiaffine(hidden_size, bias=True)[source]

helper module for Biaffine Dependency Parser predicting arc

forward(head, dep)[source]

:param head arc-head tensor = [batch, length, emb_dim] :param dep arc-dependent tensor = [batch, length, emb_dim]

:return output tensor = [bacth, length, length]

class fastNLP.models.biaffine_parser.BiaffineParser(word_vocab_size, word_emb_dim, pos_vocab_size, pos_emb_dim, word_hid_dim, pos_hid_dim, rnn_layers, rnn_hidden_size, arc_mlp_size, label_mlp_size, num_label, dropout, use_var_lstm=False, use_greedy_infer=False)[source]

Biaffine Dependency Parser implemantation. refer to ` Deep Biaffine Attention for Neural Dependency Parsing (Dozat and Manning, 2016) <https://arxiv.org/abs/1611.01734>`_ .

forward(word_seq, pos_seq, word_seq_origin_len, gold_heads=None, **_)[source]
Parameters:
  • word_seq – [batch_size, seq_len] sequence of word’s indices
  • pos_seq – [batch_size, seq_len] sequence of word’s indices
  • word_seq_origin_len – [batch_size, seq_len] sequence of length masks
  • gold_heads – [batch_size, seq_len] sequence of golden heads
Return dict:

parsing results arc_pred: [batch_size, seq_len, seq_len] label_pred: [batch_size, seq_len, seq_len] mask: [batch_size, seq_len] head_pred: [batch_size, seq_len] if gold_heads is not provided, predicting the heads

loss(arc_pred, label_pred, head_indices, head_labels, mask, **_)[source]

Compute loss.

Parameters:
  • arc_pred – [batch_size, seq_len, seq_len]
  • label_pred – [batch_size, seq_len, n_tags]
  • head_indices – [batch_size, seq_len]
  • head_labels – [batch_size, seq_len]
  • mask – [batch_size, seq_len]
Returns:

loss value

predict(word_seq, pos_seq, word_seq_origin_len)[source]
Parameters:
  • word_seq
  • pos_seq
  • word_seq_origin_len
Returns:

head_pred: [B, L] label_pred: [B, L] seq_len: [B,]

class fastNLP.models.biaffine_parser.GraphParser[source]

Graph based Parser helper class, support greedy decoding and MST(Maximum Spanning Tree) decoding

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class fastNLP.models.biaffine_parser.LabelBilinear(in1_features, in2_features, num_label, bias=True)[source]

helper module for Biaffine Dependency Parser predicting label

forward(x1, x2)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

fastNLP.models.biaffine_parser.mst(scores)[source]

with some modification to support parser output for MST decoding https://github.com/tdozat/Parser/blob/0739216129cd39d69997d28cbc4133b360ea3934/lib/models/nn.py#L692

fastNLP.models.char_language_model

class fastNLP.models.char_language_model.CharLM(char_emb_dim, word_emb_dim, vocab_size, num_char)[source]

CNN + highway network + LSTM # Input:

4D tensor with shape [batch_size, in_channel, height, width]
# Output:
2D Tensor with shape [batch_size, vocab_size]
# Arguments:
char_emb_dim: the size of each character’s attention word_emb_dim: the size of each word’s attention vocab_size: num of unique words num_char: num of characters use_gpu: True or False
forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class fastNLP.models.char_language_model.Highway(input_size)[source]

Highway network

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

fastNLP.models.cnn_text_classification

class fastNLP.models.cnn_text_classification.CNNText(embed_num, embed_dim, num_classes, kernel_nums=(3, 4, 5), kernel_sizes=(3, 4, 5), padding=0, dropout=0.5)[source]

Text classification model by character CNN, the implementation of paper ‘Yoon Kim. 2014. Convolution Neural Networks for Sentence Classification.’

forward(word_seq)[source]
Parameters:word_seq – torch.LongTensor, [batch_size, seq_len]
Return output:dict of torch.LongTensor, [batch_size, num_classes]
predict(word_seq)[source]
Parameters:word_seq – torch.LongTensor, [batch_size, seq_len]
Return predict:dict of torch.LongTensor, [batch_size, seq_len]

fastNLP.models.sequence_modeling

class fastNLP.models.sequence_modeling.AdvSeqLabel(args, emb=None, id2words=None)[source]

Advanced Sequence Labeling Model

forward(word_seq, word_seq_origin_len, truth=None)[source]
Parameters:
  • word_seq – LongTensor, [batch_size, mex_len]
  • word_seq_origin_len – LongTensor, [batch_size, ]
  • truth – LongTensor, [batch_size, max_len]
Return y:

If truth is None, return list of [decode path(list)]. Used in testing and predicting. If truth is not None, return loss, a scalar. Used in training.

loss(**kwargs)[source]

Since the loss has been computed in forward(), this function simply returns x.

class fastNLP.models.sequence_modeling.SeqLabeling(args)[source]

PyTorch Network for sequence labeling

decode(x, pad=True)[source]
Parameters:
  • x – FloatTensor, [batch_size, max_len, tag_size]
  • pad – pad the output sequence to equal lengths
Return prediction:
 

list of [decode path(list)]

forward(word_seq, word_seq_origin_len, truth=None)[source]
Parameters:
  • word_seq – LongTensor, [batch_size, mex_len]
  • word_seq_origin_len – LongTensor, [batch_size,], the origin lengths of the sequences.
  • truth – LongTensor, [batch_size, max_len]
Return y:

If truth is None, return list of [decode path(list)]. Used in testing and predicting. If truth is not None, return loss, a scalar. Used in training.

loss(x, y)[source]

Since the loss has been computed in forward(), this function simply returns x.

fastNLP.models.snli

class fastNLP.models.snli.SNLI(args, init_embedding=None)[source]

PyTorch Network for SNLI.

forward(premise, hypothesis, premise_len, hypothesis_len)[source]

Forward function

Parameters:
  • premise – A Tensor represents premise: [batch size(B), premise seq len(PL), hidden size(H)].
  • hypothesis – A Tensor represents hypothesis: [B, hypothesis seq len(HL), H].
  • premise_len – A Tensor record which is a real word and which is a padding word in premise: [B, PL].
  • hypothesis_len – A Tensor record which is a real word and which is a padding word in hypothesis: [B, HL].
Returns:

prediction: A Tensor of classification result: [B, n_labels(N)].