@@ -26,8 +26,6 @@ from ..core import logger | |||||
class BertEmbedding(ContextualEmbedding): | class BertEmbedding(ContextualEmbedding): | ||||
""" | """ | ||||
别名::class:`fastNLP.embeddings.BertEmbedding` :class:`fastNLP.embeddings.bert_embedding.BertEmbedding` | |||||
使用BERT对words进行编码的Embedding。建议将输入的words长度限制在430以内,而不要使用512(根据预训练模型参数,可能有变化)。这是由于 | 使用BERT对words进行编码的Embedding。建议将输入的words长度限制在430以内,而不要使用512(根据预训练模型参数,可能有变化)。这是由于 | ||||
预训练的bert模型长度限制为512个token,而因为输入的word是未进行word piece分割的(word piece的分割有BertEmbedding在输入word | 预训练的bert模型长度限制为512个token,而因为输入的word是未进行word piece分割的(word piece的分割有BertEmbedding在输入word | ||||
时切分),在分割之后长度可能会超过最大长度限制。 | 时切分),在分割之后长度可能会超过最大长度限制。 | ||||
@@ -24,8 +24,6 @@ from ..core import logger | |||||
class CNNCharEmbedding(TokenEmbedding): | class CNNCharEmbedding(TokenEmbedding): | ||||
""" | """ | ||||
别名::class:`fastNLP.embeddings.CNNCharEmbedding` :class:`fastNLP.embeddings.char_embedding.CNNCharEmbedding` | |||||
使用CNN生成character embedding。CNN的结构为, embed(x) -> Dropout(x) -> CNN(x) -> activation(x) -> pool -> fc -> Dropout. | 使用CNN生成character embedding。CNN的结构为, embed(x) -> Dropout(x) -> CNN(x) -> activation(x) -> pool -> fc -> Dropout. | ||||
不同的kernel大小的fitler结果是concat起来然后通过一层fully connected layer, 然后输出word的表示。 | 不同的kernel大小的fitler结果是concat起来然后通过一层fully connected layer, 然后输出word的表示。 | ||||
@@ -179,8 +177,6 @@ class CNNCharEmbedding(TokenEmbedding): | |||||
class LSTMCharEmbedding(TokenEmbedding): | class LSTMCharEmbedding(TokenEmbedding): | ||||
""" | """ | ||||
别名::class:`fastNLP.embeddings.LSTMCharEmbedding` :class:`fastNLP.embeddings.char_embedding.LSTMCharEmbedding` | |||||
使用LSTM的方式对character进行encode. embed(x) -> Dropout(x) -> LSTM(x) -> activation(x) -> pool -> Dropout | 使用LSTM的方式对character进行encode. embed(x) -> Dropout(x) -> LSTM(x) -> activation(x) -> pool -> Dropout | ||||
Example:: | Example:: | ||||
@@ -22,8 +22,6 @@ from ..core import logger | |||||
class ElmoEmbedding(ContextualEmbedding): | class ElmoEmbedding(ContextualEmbedding): | ||||
""" | """ | ||||
别名::class:`fastNLP.embeddings.ElmoEmbedding` :class:`fastNLP.embeddings.elmo_embedding.ElmoEmbedding` | |||||
使用ELMo的embedding。初始化之后,只需要传入words就可以得到对应的embedding。当前支持的使用名称初始化的模型有以下的这些(待补充) | 使用ELMo的embedding。初始化之后,只需要传入words就可以得到对应的embedding。当前支持的使用名称初始化的模型有以下的这些(待补充) | ||||
Example:: | Example:: | ||||
@@ -17,8 +17,6 @@ from .utils import get_embeddings | |||||
class Embedding(nn.Module): | class Embedding(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.embeddings.Embedding` :class:`fastNLP.embeddings.embedding.Embedding` | |||||
词向量嵌入,支持输入多种方式初始化. 可以通过self.num_embeddings获取词表大小; self.embedding_dim获取embedding的维度. | 词向量嵌入,支持输入多种方式初始化. 可以通过self.num_embeddings获取词表大小; self.embedding_dim获取embedding的维度. | ||||
Example:: | Example:: | ||||
@@ -17,8 +17,6 @@ from .embedding import TokenEmbedding | |||||
class StackEmbedding(TokenEmbedding): | class StackEmbedding(TokenEmbedding): | ||||
""" | """ | ||||
别名::class:`fastNLP.embeddings.StackEmbedding` :class:`fastNLP.embeddings.stack_embedding.StackEmbedding` | |||||
支持将多个embedding集合成一个embedding。 | 支持将多个embedding集合成一个embedding。 | ||||
Example:: | Example:: | ||||
@@ -24,8 +24,6 @@ from ..core import logger | |||||
class StaticEmbedding(TokenEmbedding): | class StaticEmbedding(TokenEmbedding): | ||||
""" | """ | ||||
别名::class:`fastNLP.embeddings.StaticEmbedding` :class:`fastNLP.embeddings.static_embedding.StaticEmbedding` | |||||
StaticEmbedding组件. 给定预训练embedding的名称或路径,根据vocab从embedding中抽取相应的数据(只会将出现在vocab中的词抽取出来, | StaticEmbedding组件. 给定预训练embedding的名称或路径,根据vocab从embedding中抽取相应的数据(只会将出现在vocab中的词抽取出来, | ||||
如果没有找到,则会随机初始化一个值(但如果该word是被标记为no_create_entry的话,则不会单独创建一个值,而是会被指向unk的index))。 | 如果没有找到,则会随机初始化一个值(但如果该word是被标记为no_create_entry的话,则不会单独创建一个值,而是会被指向unk的index))。 | ||||
当前支持自动下载的预训练vector有以下的几种(待补充); | 当前支持自动下载的预训练vector有以下的几种(待补充); | ||||
@@ -15,8 +15,6 @@ from typing import Union | |||||
def allowed_transitions(tag_vocab:Union[Vocabulary, dict], encoding_type=None, include_start_end=False): | def allowed_transitions(tag_vocab:Union[Vocabulary, dict], encoding_type=None, include_start_end=False): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.allowed_transitions` :class:`fastNLP.modules.decoder.allowed_transitions` | |||||
给定一个id到label的映射表,返回所有可以跳转的(from_tag_id, to_tag_id)列表。 | 给定一个id到label的映射表,返回所有可以跳转的(from_tag_id, to_tag_id)列表。 | ||||
:param ~fastNLP.Vocabulary,dict tag_vocab: 支持类型为tag或tag-label。只有tag的,比如"B", "M"; 也可以是"B-NN", "M-NN", | :param ~fastNLP.Vocabulary,dict tag_vocab: 支持类型为tag或tag-label。只有tag的,比如"B", "M"; 也可以是"B-NN", "M-NN", | ||||
@@ -12,8 +12,6 @@ from ..utils import initial_parameter | |||||
class MLP(nn.Module): | class MLP(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.MLP` :class:`fastNLP.modules.decoder.MLP` | |||||
多层感知器 | 多层感知器 | ||||
:param List[int] size_layer: 一个int的列表,用来定义MLP的层数,列表中的数字为每一层是hidden数目。MLP的层数为 len(size_layer) - 1 | :param List[int] size_layer: 一个int的列表,用来定义MLP的层数,列表中的数字为每一层是hidden数目。MLP的层数为 len(size_layer) - 1 | ||||
@@ -8,8 +8,6 @@ import torch | |||||
def viterbi_decode(logits, transitions, mask=None, unpad=False): | def viterbi_decode(logits, transitions, mask=None, unpad=False): | ||||
r""" | r""" | ||||
别名::class:`fastNLP.modules.viterbi_decode` :class:`fastNLP.modules.decoder.viterbi_decode` | |||||
给定一个特征矩阵以及转移分数矩阵,计算出最佳的路径以及对应的分数 | 给定一个特征矩阵以及转移分数矩阵,计算出最佳的路径以及对应的分数 | ||||
:param torch.FloatTensor logits: batch_size x max_len x num_tags,特征矩阵。 | :param torch.FloatTensor logits: batch_size x max_len x num_tags,特征矩阵。 | ||||
@@ -45,7 +45,6 @@ class DotAttention(nn.Module): | |||||
class MultiHeadAttention(nn.Module): | class MultiHeadAttention(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.MultiHeadAttention` :class:`fastNLP.modules.encoder.MultiHeadAttention` | |||||
:param input_size: int, 输入维度的大小。同时也是输出维度的大小。 | :param input_size: int, 输入维度的大小。同时也是输出维度的大小。 | ||||
:param key_size: int, 每个head的维度大小。 | :param key_size: int, 每个head的维度大小。 | ||||
@@ -348,8 +348,6 @@ class BertPooler(nn.Module): | |||||
class BertModel(nn.Module): | class BertModel(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.BertModel` :class:`fastNLP.modules.encoder.BertModel` | |||||
BERT(Bidirectional Embedding Representations from Transformers). | BERT(Bidirectional Embedding Representations from Transformers). | ||||
用预训练权重矩阵来建立BERT模型:: | 用预训练权重矩阵来建立BERT模型:: | ||||
@@ -13,8 +13,6 @@ from ..utils import initial_parameter | |||||
# from torch.nn.init import xavier_uniform | # from torch.nn.init import xavier_uniform | ||||
class ConvolutionCharEncoder(nn.Module): | class ConvolutionCharEncoder(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.ConvolutionCharEncoder` :class:`fastNLP.modules.encoder.ConvolutionCharEncoder` | |||||
char级别的卷积编码器. | char级别的卷积编码器. | ||||
:param int char_emb_size: char级别embedding的维度. Default: 50 | :param int char_emb_size: char级别embedding的维度. Default: 50 | ||||
@@ -60,11 +58,7 @@ class ConvolutionCharEncoder(nn.Module): | |||||
class LSTMCharEncoder(nn.Module): | class LSTMCharEncoder(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.LSTMCharEncoder` :class:`fastNLP.modules.encoder.LSTMCharEncoder` | |||||
char级别基于LSTM的encoder. | char级别基于LSTM的encoder. | ||||
""" | """ | ||||
def __init__(self, char_emb_size=50, hidden_size=None, initial_method=None): | def __init__(self, char_emb_size=50, hidden_size=None, initial_method=None): | ||||
@@ -10,8 +10,6 @@ import torch.nn.functional as F | |||||
class ConvMaxpool(nn.Module): | class ConvMaxpool(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.ConvMaxpool` :class:`fastNLP.modules.encoder.ConvMaxpool` | |||||
集合了Convolution和Max-Pooling于一体的层。给定一个batch_size x max_len x input_size的输入,返回batch_size x | 集合了Convolution和Max-Pooling于一体的层。给定一个batch_size x max_len x input_size的输入,返回batch_size x | ||||
sum(output_channels) 大小的matrix。在内部,是先使用CNN给输入做卷积,然后经过activation激活层,在通过在长度(max_len) | sum(output_channels) 大小的matrix。在内部,是先使用CNN给输入做卷积,然后经过activation激活层,在通过在长度(max_len) | ||||
这一维进行max_pooling。最后得到每个sample的一个向量表示。 | 这一维进行max_pooling。最后得到每个sample的一个向量表示。 | ||||
@@ -14,8 +14,6 @@ import torch.nn.utils.rnn as rnn | |||||
class LSTM(nn.Module): | class LSTM(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.LSTM` :class:`fastNLP.modules.encoder.LSTM` | |||||
LSTM 模块, 轻量封装的Pytorch LSTM. 在提供seq_len的情况下,将自动使用pack_padded_sequence; 同时默认将forget gate的bias初始化 | LSTM 模块, 轻量封装的Pytorch LSTM. 在提供seq_len的情况下,将自动使用pack_padded_sequence; 同时默认将forget gate的bias初始化 | ||||
为1; 且可以应对DataParallel中LSTM的使用问题。 | 为1; 且可以应对DataParallel中LSTM的使用问题。 | ||||
@@ -12,8 +12,6 @@ import torch.nn as nn | |||||
class MaxPool(nn.Module): | class MaxPool(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.MaxPool` :class:`fastNLP.modules.encoder.MaxPool` | |||||
Max-pooling模块。 | Max-pooling模块。 | ||||
:param stride: 窗口移动大小,默认为kernel_size | :param stride: 窗口移动大小,默认为kernel_size | ||||
@@ -61,8 +59,6 @@ class MaxPool(nn.Module): | |||||
class MaxPoolWithMask(nn.Module): | class MaxPoolWithMask(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.MaxPoolWithMask` :class:`fastNLP.modules.encoder.MaxPoolWithMask` | |||||
带mask矩阵的max pooling。在做max-pooling的时候不会考虑mask值为0的位置。 | 带mask矩阵的max pooling。在做max-pooling的时候不会考虑mask值为0的位置。 | ||||
""" | """ | ||||
@@ -101,8 +97,6 @@ class KMaxPool(nn.Module): | |||||
class AvgPool(nn.Module): | class AvgPool(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.AvgPool` :class:`fastNLP.modules.encoder.AvgPool` | |||||
给定形如[batch_size, max_len, hidden_size]的输入,在最后一维进行avg pooling. 输出为[batch_size, hidden_size] | 给定形如[batch_size, max_len, hidden_size]的输入,在最后一维进行avg pooling. 输出为[batch_size, hidden_size] | ||||
""" | """ | ||||
@@ -128,8 +122,6 @@ class AvgPool(nn.Module): | |||||
class AvgPoolWithMask(nn.Module): | class AvgPoolWithMask(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.AvgPoolWithMask` :class:`fastNLP.modules.encoder.AvgPoolWithMask` | |||||
给定形如[batch_size, max_len, hidden_size]的输入,在最后一维进行avg pooling. 输出为[batch_size, hidden_size], pooling | 给定形如[batch_size, max_len, hidden_size]的输入,在最后一维进行avg pooling. 输出为[batch_size, hidden_size], pooling | ||||
的时候只会考虑mask为1的位置 | 的时候只会考虑mask为1的位置 | ||||
""" | """ | ||||
@@ -14,9 +14,6 @@ from torch.nn import functional as F | |||||
class StarTransformer(nn.Module): | class StarTransformer(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.StarTransformer` :class:`fastNLP.modules.encoder.StarTransformer` | |||||
Star-Transformer 的encoder部分。 输入3d的文本输入, 返回相同长度的文本编码 | Star-Transformer 的encoder部分。 输入3d的文本输入, 返回相同长度的文本编码 | ||||
paper: https://arxiv.org/abs/1902.09113 | paper: https://arxiv.org/abs/1902.09113 | ||||
@@ -10,9 +10,6 @@ from .attention import MultiHeadAttention | |||||
class TransformerEncoder(nn.Module): | class TransformerEncoder(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.TransformerEncoder` :class:`fastNLP.modules.encoder.TransformerEncoder` | |||||
transformer的encoder模块,不包含embedding层 | transformer的encoder模块,不包含embedding层 | ||||
:param int num_layers: transformer的层数 | :param int num_layers: transformer的层数 | ||||
@@ -223,8 +223,6 @@ class VarRNNBase(nn.Module): | |||||
class VarLSTM(VarRNNBase): | class VarLSTM(VarRNNBase): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.VarLSTM` :class:`fastNLP.modules.encoder.VarLSTM` | |||||
Variational Dropout LSTM. | Variational Dropout LSTM. | ||||
:param input_size: 输入 `x` 的特征维度 | :param input_size: 输入 `x` 的特征维度 | ||||
@@ -248,8 +246,6 @@ class VarLSTM(VarRNNBase): | |||||
class VarRNN(VarRNNBase): | class VarRNN(VarRNNBase): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.VarRNN` :class:`fastNLP.modules.encoder.VarRNN` | |||||
Variational Dropout RNN. | Variational Dropout RNN. | ||||
:param input_size: 输入 `x` 的特征维度 | :param input_size: 输入 `x` 的特征维度 | ||||
@@ -273,8 +269,6 @@ class VarRNN(VarRNNBase): | |||||
class VarGRU(VarRNNBase): | class VarGRU(VarRNNBase): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.VarGRU` :class:`fastNLP.modules.encoder.VarGRU` | |||||
Variational Dropout GRU. | Variational Dropout GRU. | ||||
:param input_size: 输入 `x` 的特征维度 | :param input_size: 输入 `x` 的特征维度 | ||||
@@ -11,11 +11,7 @@ from reproduction.utils import check_dataloader_paths, get_tokenizer | |||||
class SSTLoader(DataSetLoader): | class SSTLoader(DataSetLoader): | ||||
URL = 'https://nlp.stanford.edu/sentiment/trainDevTestTrees_PTB.zip' | |||||
DATA_DIR = 'sst/' | |||||
""" | """ | ||||
别名::class:`fastNLP.io.SSTLoader` :class:`fastNLP.io.dataset_loader.SSTLoader` | |||||
读取SST数据集, DataSet包含fields:: | 读取SST数据集, DataSet包含fields:: | ||||
words: list(str) 需要分类的文本 | words: list(str) 需要分类的文本 | ||||
target: str 文本的标签 | target: str 文本的标签 | ||||
@@ -23,6 +19,10 @@ class SSTLoader(DataSetLoader): | |||||
:param subtree: 是否将数据展开为子树,扩充数据量. Default: ``False`` | :param subtree: 是否将数据展开为子树,扩充数据量. Default: ``False`` | ||||
:param fine_grained: 是否使用SST-5标准,若 ``False`` , 使用SST-2。Default: ``False`` | :param fine_grained: 是否使用SST-5标准,若 ``False`` , 使用SST-2。Default: ``False`` | ||||
""" | """ | ||||
URL = 'https://nlp.stanford.edu/sentiment/trainDevTestTrees_PTB.zip' | |||||
DATA_DIR = 'sst/' | |||||
def __init__(self, subtree=False, fine_grained=False): | def __init__(self, subtree=False, fine_grained=False): | ||||
self.subtree = subtree | self.subtree = subtree | ||||
tag_v = {'0': 'very negative', '1': 'negative', '2': 'neutral', | tag_v = {'0': 'very negative', '1': 'negative', '2': 'neutral', | ||||
@@ -17,8 +17,6 @@ from .weight_drop import WeightDrop | |||||
class LSTM(nn.Module): | class LSTM(nn.Module): | ||||
""" | """ | ||||
别名::class:`fastNLP.modules.LSTM` :class:`fastNLP.modules.encoder.lstm.LSTM` | |||||
LSTM 模块, 轻量封装的Pytorch LSTM. 在提供seq_len的情况下,将自动使用pack_padded_sequence; 同时默认将forget gate的bias初始化 | LSTM 模块, 轻量封装的Pytorch LSTM. 在提供seq_len的情况下,将自动使用pack_padded_sequence; 同时默认将forget gate的bias初始化 | ||||
为1; 且可以应对DataParallel中LSTM的使用问题。 | 为1; 且可以应对DataParallel中LSTM的使用问题。 | ||||