@@ -158,6 +158,7 @@ Vocabulary 的使用 | |||||
损失函数 | 损失函数 | ||||
训练模型需要提供一个损失函数 | 训练模型需要提供一个损失函数 | ||||
,fastNLP中提供了直接可以导入使用的四种loss,分别为: | ,fastNLP中提供了直接可以导入使用的四种loss,分别为: | ||||
* :class:`~fastNLP.CrossEntropyLoss`:包装了torch.nn.functional.cross_entropy()函数,返回交叉熵损失(可以运用于多分类场景) | * :class:`~fastNLP.CrossEntropyLoss`:包装了torch.nn.functional.cross_entropy()函数,返回交叉熵损失(可以运用于多分类场景) | ||||
* :class:`~fastNLP.BCELoss`:包装了torch.nn.functional.binary_cross_entropy()函数,返回二分类的交叉熵 | * :class:`~fastNLP.BCELoss`:包装了torch.nn.functional.binary_cross_entropy()函数,返回二分类的交叉熵 | ||||
* :class:`~fastNLP.L1Loss`:包装了torch.nn.functional.l1_loss()函数,返回L1 损失 | * :class:`~fastNLP.L1Loss`:包装了torch.nn.functional.l1_loss()函数,返回L1 损失 | ||||
@@ -209,7 +210,7 @@ Vocabulary 的使用 | |||||
#使用CNNText的时候第一个参数输入一个tuple,作为模型定义embedding的参数 | #使用CNNText的时候第一个参数输入一个tuple,作为模型定义embedding的参数 | ||||
#还可以传入 kernel_nums, kernel_sizes, padding, dropout的自定义值 | #还可以传入 kernel_nums, kernel_sizes, padding, dropout的自定义值 | ||||
model_cnn = CNNText((len(vocab),EMBED_DIM), num_classes=3, padding=2, dropout=0.1) | |||||
model_cnn = CNNText((len(vocab),EMBED_DIM), num_classes=3, dropout=0.1) | |||||
#如果在定义trainer的时候没有传入optimizer参数,模型默认的优化器为torch.optim.Adam且learning rate为lr=4e-3 | #如果在定义trainer的时候没有传入optimizer参数,模型默认的优化器为torch.optim.Adam且learning rate为lr=4e-3 | ||||
#这里只使用了optimizer_1作为优化器输入,感兴趣可以尝试optimizer_2或者其他优化器作为输入 | #这里只使用了optimizer_1作为优化器输入,感兴趣可以尝试optimizer_2或者其他优化器作为输入 | ||||
@@ -192,7 +192,7 @@ sampler | |||||
import time | import time | ||||
embed_dim = 100 | embed_dim = 100 | ||||
model = CNNText((len(vocab),embed_dim), num_classes=3, padding=2, dropout=0.1) | |||||
model = CNNText((len(vocab),embed_dim), num_classes=3, dropout=0.1) | |||||
def train(epoch, data, devdata): | def train(epoch, data, devdata): | ||||
optimizer = torch.optim.Adam(model.parameters(), lr=0.001) | optimizer = torch.optim.Adam(model.parameters(), lr=0.001) | ||||
@@ -27,6 +27,7 @@ class BertEmbedding(ContextualEmbedding): | |||||
>>> import torch | >>> import torch | ||||
>>> from fastNLP import Vocabulary | >>> from fastNLP import Vocabulary | ||||
>>> from fastNLP.embeddings import BertEmbedding | |||||
>>> vocab = Vocabulary().add_word_lst("The whether is good .".split()) | >>> vocab = Vocabulary().add_word_lst("The whether is good .".split()) | ||||
>>> embed = BertEmbedding(vocab, model_dir_or_name='en-base-uncased', requires_grad=False, layers='4,-2,-1') | >>> embed = BertEmbedding(vocab, model_dir_or_name='en-base-uncased', requires_grad=False, layers='4,-2,-1') | ||||
>>> words = torch.LongTensor([[vocab.to_index(word) for word in "The whether is good .".split()]]) | >>> words = torch.LongTensor([[vocab.to_index(word) for word in "The whether is good .".split()]]) | ||||
@@ -24,6 +24,9 @@ class CNNCharEmbedding(TokenEmbedding): | |||||
Example:: | Example:: | ||||
>>> import torch | |||||
>>> from fastNLP import Vocabulary | |||||
>>> from fastNLP.embeddings import CNNCharEmbedding | |||||
>>> vocab = Vocabulary().add_word_lst("The whether is good .".split()) | >>> vocab = Vocabulary().add_word_lst("The whether is good .".split()) | ||||
>>> embed = CNNCharEmbedding(vocab, embed_size=50) | >>> embed = CNNCharEmbedding(vocab, embed_size=50) | ||||
>>> words = torch.LongTensor([[vocab.to_index(word) for word in "The whether is good .".split()]]) | >>> words = torch.LongTensor([[vocab.to_index(word) for word in "The whether is good .".split()]]) | ||||
@@ -167,6 +170,9 @@ class LSTMCharEmbedding(TokenEmbedding): | |||||
Example:: | Example:: | ||||
>>> import torch | |||||
>>> from fastNLP import Vocabulary | |||||
>>> from fastNLP.embeddings import LSTMCharEmbedding | |||||
>>> vocab = Vocabulary().add_word_lst("The whether is good .".split()) | >>> vocab = Vocabulary().add_word_lst("The whether is good .".split()) | ||||
>>> embed = LSTMCharEmbedding(vocab, embed_size=50) | >>> embed = LSTMCharEmbedding(vocab, embed_size=50) | ||||
>>> words = torch.LongTensor([[vocab.to_index(word) for word in "The whether is good .".split()]]) | >>> words = torch.LongTensor([[vocab.to_index(word) for word in "The whether is good .".split()]]) | ||||
@@ -21,6 +21,9 @@ class ElmoEmbedding(ContextualEmbedding): | |||||
Example:: | Example:: | ||||
>>> import torch | |||||
>>> from fastNLP import Vocabulary | |||||
>>> from fastNLP.embeddings import ElmoEmbedding | |||||
>>> vocab = Vocabulary().add_word_lst("The whether is good .".split()) | >>> vocab = Vocabulary().add_word_lst("The whether is good .".split()) | ||||
>>> # 使用不同层的concat的结果 | >>> # 使用不同层的concat的结果 | ||||
>>> embed = ElmoEmbedding(vocab, model_dir_or_name='en', layers='1,2', requires_grad=False) | >>> embed = ElmoEmbedding(vocab, model_dir_or_name='en', layers='1,2', requires_grad=False) | ||||
@@ -20,6 +20,7 @@ class Embedding(nn.Module): | |||||
Example:: | Example:: | ||||
>>> import numpy as np | >>> import numpy as np | ||||
>>> from fastNLP.embeddings import Embedding | |||||
>>> init_embed = (2000, 100) | >>> init_embed = (2000, 100) | ||||
>>> embed = Embedding(init_embed) # 随机初始化一个具有2000个词,每个词表示为100维的词向量 | >>> embed = Embedding(init_embed) # 随机初始化一个具有2000个词,每个词表示为100维的词向量 | ||||
>>> init_embed = np.zeros((2000, 100)) | >>> init_embed = np.zeros((2000, 100)) | ||||
@@ -17,7 +17,7 @@ class StackEmbedding(TokenEmbedding): | |||||
>>> from fastNLP import Vocabulary | >>> from fastNLP import Vocabulary | ||||
>>> from fastNLP.embeddings import StaticEmbedding | >>> from fastNLP.embeddings import StaticEmbedding | ||||
>>> vocab = Vocabulary().add_word_lst("The whether is good .".split()) | >>> vocab = Vocabulary().add_word_lst("The whether is good .".split()) | ||||
>>> embed_1 = StaticEmbedding(vocab, model_dir_or_name='en-glove-6b-50', requires_grad=True) | |||||
>>> embed_1 = StaticEmbedding(vocab, model_dir_or_name='en-glove-6b-50d', requires_grad=True) | |||||
>>> embed_2 = StaticEmbedding(vocab, model_dir_or_name='en-word2vec-300', requires_grad=True) | >>> embed_2 = StaticEmbedding(vocab, model_dir_or_name='en-word2vec-300', requires_grad=True) | ||||
:param embeds: 一个由若干个TokenEmbedding组成的list,要求每一个TokenEmbedding的词表都保持一致 | :param embeds: 一个由若干个TokenEmbedding组成的list,要求每一个TokenEmbedding的词表都保持一致 | ||||
@@ -91,4 +91,4 @@ class StackEmbedding(TokenEmbedding): | |||||
for embed in self.embeds: | for embed in self.embeds: | ||||
outputs.append(embed(words)) | outputs.append(embed(words)) | ||||
outputs = self.dropout(torch.cat(outputs, dim=-1)) | outputs = self.dropout(torch.cat(outputs, dim=-1)) | ||||
return outputs | |||||
return outputs |
@@ -22,12 +22,14 @@ class StaticEmbedding(TokenEmbedding): | |||||
当前支持自动下载的预训练vector有以下的几种(待补充); | 当前支持自动下载的预训练vector有以下的几种(待补充); | ||||
Example:: | Example:: | ||||
>>> from fastNLP import Vocabulary | |||||
>>> from fastNLP.embeddings import StaticEmbedding | |||||
>>> vocab = Vocabulary().add_word_lst("The whether is good .".split()) | >>> vocab = Vocabulary().add_word_lst("The whether is good .".split()) | ||||
>>> embed = StaticEmbedding(vocab, model_dir_or_name='en-glove-50') | |||||
>>> embed = StaticEmbedding(vocab, model_dir_or_name='en-glove-50d') | |||||
>>> vocab = Vocabulary().add_word_lst(["The", 'the', "THE"]) | >>> vocab = Vocabulary().add_word_lst(["The", 'the', "THE"]) | ||||
>>> embed = StaticEmbedding(vocab, model_dir_or_name="en-glove-50", lower=True) | |||||
>>> embed = StaticEmbedding(vocab, model_dir_or_name="en-glove-50d", lower=True) | |||||
>>> # "the", "The", "THE"它们共用一个vector,且将使用"the"在预训练词表中寻找它们的初始化表示。 | >>> # "the", "The", "THE"它们共用一个vector,且将使用"the"在预训练词表中寻找它们的初始化表示。 | ||||
>>> vocab = Vocabulary().add_word_lst(["The", "the", "THE"]) | >>> vocab = Vocabulary().add_word_lst(["The", "the", "THE"]) | ||||