fastNLP.models¶
fastNLP 在 models
模块中内置了如 CNNText
、
SeqLabeling
等完整的模型,以供用户直接使用。
-
class
fastNLP.models.
CNNText
(embed, num_classes, kernel_nums=(30, 40, 50), kernel_sizes=(1, 3, 5), dropout=0.5)[源代码]¶ 别名
fastNLP.models.CNNText
fastNLP.models.cnn_text_classification.CNNText
使用CNN进行文本分类的模型 'Yoon Kim. 2014. Convolution Neural Networks for Sentence Classification.'
-
__init__
(embed, num_classes, kernel_nums=(30, 40, 50), kernel_sizes=(1, 3, 5), dropout=0.5)[源代码]¶ 参数: - embed (tuple(int,int),torch.FloatTensor,nn.Embedding,numpy.ndarray) -- Embedding的大小(传入tuple(int, int), 第一个int为vocab_zie, 第二个int为embed_dim); 如果为Tensor, Embedding, ndarray等则直接使用该值初始化Embedding
- num_classes (int) -- 一共有多少类
- kernel_sizes (int,tuple(int)) -- 输出channel的kernel大小。
- dropout (float) -- Dropout的大小
-
-
class
fastNLP.models.
SeqLabeling
(embed, hidden_size, num_classes)[源代码]¶ 基类
fastNLP.models.BaseModel
别名
fastNLP.models.SeqLabeling
fastNLP.models.sequence_labeling.SeqLabeling
一个基础的Sequence labeling的模型。 用于做sequence labeling的基础类。结构包含一层Embedding,一层LSTM(单向,一层),一层FC,以及一层CRF。-
__init__
(embed, hidden_size, num_classes)[源代码]¶ 参数: - embed (tuple(int,int),torch.FloatTensor,nn.Embedding,numpy.ndarray) -- Embedding的大小(传入tuple(int, int), 第一个int为vocab_zie, 第二个int为embed_dim); 如果为Tensor, embedding, ndarray等则直接使用该值初始化Embedding
- hidden_size (int) -- LSTM隐藏层的大小
- num_classes (int) -- 一共有多少类
-
forward
(words, seq_len, target)[源代码]¶ 参数: - words (torch.LongTensor) -- [batch_size, max_len],序列的index
- seq_len (torch.LongTensor) -- [batch_size,], 这个序列的长度
- target (torch.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.
-
-
class
fastNLP.models.
AdvSeqLabel
(embed, hidden_size, num_classes, dropout=0.3, id2words=None, encoding_type='bmes')[源代码]¶ 别名
fastNLP.models.AdvSeqLabel
fastNLP.models.sequence_labeling.AdvSeqLabel
更复杂的Sequence Labelling模型。结构为Embedding, LayerNorm, 双向LSTM(两层),FC,LayerNorm,DropOut,FC,CRF。
-
__init__
(embed, hidden_size, num_classes, dropout=0.3, id2words=None, encoding_type='bmes')[源代码]¶ 参数: - embed (tuple(int,int),torch.FloatTensor,nn.Embedding,numpy.ndarray) -- Embedding的大小(传入tuple(int, int), 第一个int为vocab_zie, 第二个int为embed_dim); 如果为Tensor, Embedding, ndarray等则直接使用该值初始化Embedding
- hidden_size (int) -- LSTM的隐层大小
- num_classes (int) -- 有多少个类
- dropout (float) -- LSTM中以及DropOut层的drop概率
- id2words (dict) -- tag id转为其tag word的表。用于在CRF解码时防止解出非法的顺序,比如'BMES'这个标签规范中,'S' 不能出现在'B'之后。这里也支持类似与'B-NN',即'-'前为标签类型的指示,后面为具体的tag的情况。这里不但会保证 'B-NN'后面不为'S-NN'还会保证'B-NN'后面不会出现'M-xx'(任何非'M-NN'和'E-NN'的情况。)
- encoding_type (str) -- 支持"BIO", "BMES", "BEMSO", 只有在id2words不为None的情况有用。
-
-
class
fastNLP.models.
ESIM
(embed, hidden_size=None, num_labels=3, dropout_rate=0.3, dropout_embed=0.1)[源代码]¶ 基类
fastNLP.models.BaseModel
别名
fastNLP.models.ESIM
fastNLP.models.snli.ESIM
ESIM model的一个PyTorch实现 论文参见: https://arxiv.org/pdf/1609.06038.pdf
-
class
fastNLP.models.
StarTransEnc
(embed, hidden_size, num_layers, num_head, head_dim, max_len, emb_dropout, dropout)[源代码]¶ 别名
fastNLP.models.StarTransEnc
fastNLP.models.star_transformer.StarTransEnc
带word embedding的Star-Transformer Encoder
-
__init__
(embed, hidden_size, num_layers, num_head, head_dim, max_len, emb_dropout, dropout)[源代码]¶ 参数: - embed -- 单词词典, 可以是 tuple, 包括(num_embedings, embedding_dim), 即 embedding的大小和每个词的维度. 也可以传入 nn.Embedding 对象,此时就以传入的对象作为embedding
- hidden_size -- 模型中特征维度.
- num_layers -- 模型层数.
- num_head -- 模型中multi-head的head个数.
- head_dim -- 模型中multi-head中每个head特征维度.
- max_len -- 模型能接受的最大输入长度.
- emb_dropout -- 词嵌入的dropout概率.
- dropout -- 模型除词嵌入外的dropout概率.
-
-
class
fastNLP.models.
STSeqLabel
(embed, num_cls, hidden_size=300, num_layers=4, num_head=8, head_dim=32, max_len=512, cls_hidden_size=600, emb_dropout=0.1, dropout=0.1)[源代码]¶ 别名
fastNLP.models.STSeqLabel
fastNLP.models.star_transformer.STSeqLabel
用于序列标注的Star-Transformer模型
-
__init__
(embed, num_cls, hidden_size=300, num_layers=4, num_head=8, head_dim=32, max_len=512, cls_hidden_size=600, emb_dropout=0.1, dropout=0.1)[源代码]¶ 参数: - embed -- 单词词典, 可以是 tuple, 包括(num_embedings, embedding_dim), 即 embedding的大小和每个词的维度. 也可以传入 nn.Embedding 对象, 此时就以传入的对象作为embedding
- num_cls -- 输出类别个数
- hidden_size -- 模型中特征维度. Default: 300
- num_layers -- 模型层数. Default: 4
- num_head -- 模型中multi-head的head个数. Default: 8
- head_dim -- 模型中multi-head中每个head特征维度. Default: 32
- max_len -- 模型能接受的最大输入长度. Default: 512
- cls_hidden_size -- 分类器隐层维度. Default: 600
- emb_dropout -- 词嵌入的dropout概率. Default: 0.1
- dropout -- 模型除词嵌入外的dropout概率. Default: 0.1
-
-
class
fastNLP.models.
STNLICls
(embed, num_cls, hidden_size=300, num_layers=4, num_head=8, head_dim=32, max_len=512, cls_hidden_size=600, emb_dropout=0.1, dropout=0.1)[源代码]¶ 别名
fastNLP.models.STNLICls
fastNLP.models.star_transformer.STNLICls
用于自然语言推断(NLI)的Star-Transformer
-
__init__
(embed, num_cls, hidden_size=300, num_layers=4, num_head=8, head_dim=32, max_len=512, cls_hidden_size=600, emb_dropout=0.1, dropout=0.1)[源代码]¶ 参数: - embed -- 单词词典, 可以是 tuple, 包括(num_embedings, embedding_dim), 即 embedding的大小和每个词的维度. 也可以传入 nn.Embedding 对象, 此时就以传入的对象作为embedding
- num_cls -- 输出类别个数
- hidden_size -- 模型中特征维度. Default: 300
- num_layers -- 模型层数. Default: 4
- num_head -- 模型中multi-head的head个数. Default: 8
- head_dim -- 模型中multi-head中每个head特征维度. Default: 32
- max_len -- 模型能接受的最大输入长度. Default: 512
- cls_hidden_size -- 分类器隐层维度. Default: 600
- emb_dropout -- 词嵌入的dropout概率. Default: 0.1
- dropout -- 模型除词嵌入外的dropout概率. Default: 0.1
-
-
class
fastNLP.models.
STSeqCls
(embed, num_cls, hidden_size=300, num_layers=4, num_head=8, head_dim=32, max_len=512, cls_hidden_size=600, emb_dropout=0.1, dropout=0.1)[源代码]¶ 别名
fastNLP.models.STSeqCls
fastNLP.models.star_transformer.STSeqCls
用于分类任务的Star-Transformer
-
__init__
(embed, num_cls, hidden_size=300, num_layers=4, num_head=8, head_dim=32, max_len=512, cls_hidden_size=600, emb_dropout=0.1, dropout=0.1)[源代码]¶ 参数: - embed -- 单词词典, 可以是 tuple, 包括(num_embedings, embedding_dim), 即 embedding的大小和每个词的维度. 也可以传入 nn.Embedding 对象, 此时就以传入的对象作为embedding
- num_cls -- 输出类别个数
- hidden_size -- 模型中特征维度. Default: 300
- num_layers -- 模型层数. Default: 4
- num_head -- 模型中multi-head的head个数. Default: 8
- head_dim -- 模型中multi-head中每个head特征维度. Default: 32
- max_len -- 模型能接受的最大输入长度. Default: 512
- cls_hidden_size -- 分类器隐层维度. Default: 600
- emb_dropout -- 词嵌入的dropout概率. Default: 0.1
- dropout -- 模型除词嵌入外的dropout概率. Default: 0.1
-
-
class
fastNLP.models.
BiaffineParser
(embed, pos_vocab_size, pos_emb_dim, num_label, rnn_layers=1, rnn_hidden_size=200, arc_mlp_size=100, label_mlp_size=100, dropout=0.3, encoder='lstm', use_greedy_infer=False)[源代码]¶ -
别名
fastNLP.models.BiaffineParser
fastNLP.models.biaffine_parser.BiaffineParser
Biaffine Dependency Parser 实现. 论文参考 Deep Biaffine Attention for Neural Dependency Parsing (Dozat and Manning, 2016) .-
__init__
(embed, pos_vocab_size, pos_emb_dim, num_label, rnn_layers=1, rnn_hidden_size=200, arc_mlp_size=100, label_mlp_size=100, dropout=0.3, encoder='lstm', use_greedy_infer=False)[源代码]¶ 参数: - embed -- 单词词典, 可以是 tuple, 包括(num_embedings, embedding_dim), 即 embedding的大小和每个词的维度. 也可以传入 nn.Embedding 对象, 此时就以传入的对象作为embedding
- pos_vocab_size -- part-of-speech 词典大小
- pos_emb_dim -- part-of-speech 向量维度
- num_label -- 边的类别个数
- rnn_layers -- rnn encoder的层数
- rnn_hidden_size -- rnn encoder 的隐状态维度
- arc_mlp_size -- 边预测的MLP维度
- label_mlp_size -- 类别预测的MLP维度
- dropout -- dropout概率.
- encoder -- encoder类别, 可选 ('lstm', 'var-lstm', 'transformer'). Default: lstm
- use_greedy_infer -- 是否在inference时使用贪心算法.
若
False
, 使用更加精确但相对缓慢的MST算法. Default:False
-
forward
(words1, words2, seq_len, target1=None)[源代码]¶ 模型forward阶段
参数: - words1 -- [batch_size, seq_len] 输入word序列
- words2 -- [batch_size, seq_len] 输入pos序列
- seq_len -- [batch_size, seq_len] 输入序列长度
- target1 -- [batch_size, seq_len] 输入真实标注的heads, 仅在训练阶段有效,
用于训练label分类器. 若为
None
, 使用预测的heads输入到label分类器 Default:None
Return dict: parsing 结果:
pred1: [batch_size, seq_len, seq_len] 边预测logits pred2: [batch_size, seq_len, num_label] label预测logits pred3: [batch_size, seq_len] heads的预测结果, 在 ``target1=None`` 时预测
-
static
loss
(pred1, pred2, target1, target2, seq_len)[源代码]¶ 计算parser的loss
参数: - pred1 -- [batch_size, seq_len, seq_len] 边预测logits
- pred2 -- [batch_size, seq_len, num_label] label预测logits
- target1 -- [batch_size, seq_len] 真实边的标注
- target2 -- [batch_size, seq_len] 真实类别的标注
- seq_len -- [batch_size, seq_len] 真实目标的长度
Return loss: scalar
-
-
class
fastNLP.models.
GraphParser
[源代码]¶ 基类
fastNLP.models.BaseModel
别名
fastNLP.models.GraphParser
fastNLP.models.biaffine_parser.GraphParser
基于图的parser base class, 支持贪婪解码和最大生成树解码
-
class
fastNLP.models.
BertForSequenceClassification
(embed: fastNLP.embeddings.bert_embedding.BertEmbedding, num_labels: int = 2, dropout=0.1)[源代码]¶ 基类
fastNLP.models.BaseModel
别名
fastNLP.models.BertForSequenceClassification
fastNLP.models.bert.BertForSequenceClassification
BERT model for classification.-
__init__
(embed: fastNLP.embeddings.bert_embedding.BertEmbedding, num_labels: int = 2, dropout=0.1)[源代码]¶ 参数: - embed (fastNLP.embeddings.BertEmbedding) -- 下游模型的编码器(encoder).
- num_labels (int) -- 文本分类类别数目,默认值为2.
- dropout (float) -- dropout的大小,默认值为0.1.
-
forward
(words)[源代码]¶ 参数: words (torch.LongTensor) -- [batch_size, seq_len] 返回: { fastNLP.Const.OUTPUT
: logits}: torch.Tensor [batch_size, num_labels]
-
predict
(words)[源代码]¶ 参数: words (torch.LongTensor) -- [batch_size, seq_len] 返回: { fastNLP.Const.OUTPUT
: logits}: torch.LongTensor [batch_size]
-
-
class
fastNLP.models.
BertForSentenceMatching
(embed: fastNLP.embeddings.bert_embedding.BertEmbedding, num_labels: int = 2, dropout=0.1)[源代码]¶ 基类
fastNLP.models.BaseModel
别名
fastNLP.models.BertForSentenceMatching
fastNLP.models.bert.BertForSentenceMatching
BERT model for sentence matching.-
__init__
(embed: fastNLP.embeddings.bert_embedding.BertEmbedding, num_labels: int = 2, dropout=0.1)[源代码]¶ 参数: - embed (fastNLP.embeddings.BertEmbedding) -- 下游模型的编码器(encoder).
- num_labels (int) -- Matching任务类别数目,默认值为2.
- dropout (float) -- dropout的大小,默认值为0.1.
-
forward
(words)[源代码]¶ 参数: words (torch.LongTensor) -- [batch_size, seq_len] 返回: { fastNLP.Const.OUTPUT
: logits}: torch.Tensor [batch_size, num_labels]
-
predict
(words)[源代码]¶ 参数: words (torch.LongTensor) -- [batch_size, seq_len] 返回: { fastNLP.Const.OUTPUT
: logits}: torch.LongTensor [batch_size]
-
-
class
fastNLP.models.
BertForMultipleChoice
(embed: fastNLP.embeddings.bert_embedding.BertEmbedding, num_choices=2, dropout=0.1)[源代码]¶ 基类
fastNLP.models.BaseModel
别名
fastNLP.models.BertForMultipleChoice
fastNLP.models.bert.BertForMultipleChoice
BERT model for multiple choice.-
__init__
(embed: fastNLP.embeddings.bert_embedding.BertEmbedding, num_choices=2, dropout=0.1)[源代码]¶ 参数: - embed (fastNLP.embeddings.BertEmbedding) -- 下游模型的编码器(encoder).
- num_choices (int) -- 多选任务选项数目,默认值为2.
- dropout (float) -- dropout的大小,默认值为0.1.
-
forward
(words)[源代码]¶ 参数: words (torch.LongTensor) -- [batch_size, num_choices, seq_len] 返回: { fastNLP.Const.OUTPUT
: logits}: torch.LongTensor [batch_size, num_choices]
-
predict
(words)[源代码]¶ 参数: words (torch.LongTensor) -- [batch_size, num_choices, seq_len] 返回: { fastNLP.Const.OUTPUT
: logits}: torch.LongTensor [batch_size]
-
-
class
fastNLP.models.
BertForTokenClassification
(embed: fastNLP.embeddings.bert_embedding.BertEmbedding, num_labels, dropout=0.1)[源代码]¶ 基类
fastNLP.models.BaseModel
别名
fastNLP.models.BertForTokenClassification
fastNLP.models.bert.BertForTokenClassification
BERT model for token classification.-
__init__
(embed: fastNLP.embeddings.bert_embedding.BertEmbedding, num_labels, dropout=0.1)[源代码]¶ 参数: - embed (fastNLP.embeddings.BertEmbedding) -- 下游模型的编码器(encoder).
- num_labels (int) -- 序列标注标签数目,无默认值.
- dropout (float) -- dropout的大小,默认值为0.1.
-
forward
(words)[源代码]¶ 参数: words (torch.LongTensor) -- [batch_size, seq_len] 返回: { fastNLP.Const.OUTPUT
: logits}: torch.Tensor [batch_size, seq_len, num_labels]
-
predict
(words)[源代码]¶ 参数: words (torch.LongTensor) -- [batch_size, seq_len] 返回: { fastNLP.Const.OUTPUT
: logits}: torch.LongTensor [batch_size, seq_len]
-
-
class
fastNLP.models.
BertForQuestionAnswering
(embed: fastNLP.embeddings.bert_embedding.BertEmbedding)[源代码]¶ 基类
fastNLP.models.BaseModel
别名
fastNLP.models.BertForQuestionAnswering
fastNLP.models.bert.BertForQuestionAnswering
用于做Q&A的Bert模型,如果是Squad2.0请将BertEmbedding的include_cls_sep设置为True,Squad1.0或CMRC则设置为False-
__init__
(embed: fastNLP.embeddings.bert_embedding.BertEmbedding)[源代码]¶ 参数: - embed (fastNLP.embeddings.BertEmbedding) -- 下游模型的编码器(encoder).
- num_labels (int) -- 抽取式QA列数,默认值为2(即第一列为start_span, 第二列为end_span).
-