Browse Source

Merge pull request #205 from zide05/dev0.5.0

Dev0.5.0
tags/v0.4.10
ChenXin GitHub 5 years ago
parent
commit
cb92f285d1
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 21 additions and 7 deletions
  1. +2
    -1
      docs/source/tutorials/tutorial_4_loss_optimizer.rst
  2. +1
    -1
      docs/source/tutorials/tutorial_5_datasetiter.rst
  3. +1
    -0
      fastNLP/embeddings/bert_embedding.py
  4. +6
    -0
      fastNLP/embeddings/char_embedding.py
  5. +3
    -0
      fastNLP/embeddings/elmo_embedding.py
  6. +1
    -0
      fastNLP/embeddings/embedding.py
  7. +2
    -2
      fastNLP/embeddings/stack_embedding.py
  8. +5
    -3
      fastNLP/embeddings/static_embedding.py

+ 2
- 1
docs/source/tutorials/tutorial_4_loss_optimizer.rst View File

@@ -158,6 +158,7 @@ Vocabulary 的使用
损失函数
训练模型需要提供一个损失函数
,fastNLP中提供了直接可以导入使用的四种loss,分别为:
* :class:`~fastNLP.CrossEntropyLoss`:包装了torch.nn.functional.cross_entropy()函数,返回交叉熵损失(可以运用于多分类场景)
* :class:`~fastNLP.BCELoss`:包装了torch.nn.functional.binary_cross_entropy()函数,返回二分类的交叉熵
* :class:`~fastNLP.L1Loss`:包装了torch.nn.functional.l1_loss()函数,返回L1 损失
@@ -209,7 +210,7 @@ Vocabulary 的使用

#使用CNNText的时候第一个参数输入一个tuple,作为模型定义embedding的参数
#还可以传入 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
#这里只使用了optimizer_1作为优化器输入,感兴趣可以尝试optimizer_2或者其他优化器作为输入


+ 1
- 1
docs/source/tutorials/tutorial_5_datasetiter.rst View File

@@ -192,7 +192,7 @@ sampler
import time

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):
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)


+ 1
- 0
fastNLP/embeddings/bert_embedding.py View File

@@ -27,6 +27,7 @@ class BertEmbedding(ContextualEmbedding):

>>> import torch
>>> from fastNLP import Vocabulary
>>> from fastNLP.embeddings import BertEmbedding
>>> 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')
>>> words = torch.LongTensor([[vocab.to_index(word) for word in "The whether is good .".split()]])


+ 6
- 0
fastNLP/embeddings/char_embedding.py View File

@@ -24,6 +24,9 @@ class CNNCharEmbedding(TokenEmbedding):

Example::

>>> import torch
>>> from fastNLP import Vocabulary
>>> from fastNLP.embeddings import CNNCharEmbedding
>>> vocab = Vocabulary().add_word_lst("The whether is good .".split())
>>> embed = CNNCharEmbedding(vocab, embed_size=50)
>>> words = torch.LongTensor([[vocab.to_index(word) for word in "The whether is good .".split()]])
@@ -167,6 +170,9 @@ class LSTMCharEmbedding(TokenEmbedding):

Example::

>>> import torch
>>> from fastNLP import Vocabulary
>>> from fastNLP.embeddings import LSTMCharEmbedding
>>> vocab = Vocabulary().add_word_lst("The whether is good .".split())
>>> embed = LSTMCharEmbedding(vocab, embed_size=50)
>>> words = torch.LongTensor([[vocab.to_index(word) for word in "The whether is good .".split()]])


+ 3
- 0
fastNLP/embeddings/elmo_embedding.py View File

@@ -21,6 +21,9 @@ class ElmoEmbedding(ContextualEmbedding):

Example::
>>> import torch
>>> from fastNLP import Vocabulary
>>> from fastNLP.embeddings import ElmoEmbedding
>>> vocab = Vocabulary().add_word_lst("The whether is good .".split())
>>> # 使用不同层的concat的结果
>>> embed = ElmoEmbedding(vocab, model_dir_or_name='en', layers='1,2', requires_grad=False)


+ 1
- 0
fastNLP/embeddings/embedding.py View File

@@ -20,6 +20,7 @@ class Embedding(nn.Module):
Example::

>>> import numpy as np
>>> from fastNLP.embeddings import Embedding
>>> init_embed = (2000, 100)
>>> embed = Embedding(init_embed) # 随机初始化一个具有2000个词,每个词表示为100维的词向量
>>> init_embed = np.zeros((2000, 100))


+ 2
- 2
fastNLP/embeddings/stack_embedding.py View File

@@ -17,7 +17,7 @@ class StackEmbedding(TokenEmbedding):
>>> from fastNLP import Vocabulary
>>> from fastNLP.embeddings import StaticEmbedding
>>> 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)

:param embeds: 一个由若干个TokenEmbedding组成的list,要求每一个TokenEmbedding的词表都保持一致
@@ -91,4 +91,4 @@ class StackEmbedding(TokenEmbedding):
for embed in self.embeds:
outputs.append(embed(words))
outputs = self.dropout(torch.cat(outputs, dim=-1))
return outputs
return outputs

+ 5
- 3
fastNLP/embeddings/static_embedding.py View File

@@ -22,12 +22,14 @@ class StaticEmbedding(TokenEmbedding):
当前支持自动下载的预训练vector有以下的几种(待补充);

Example::

>>> from fastNLP import Vocabulary
>>> from fastNLP.embeddings import StaticEmbedding
>>> 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"])
>>> 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"在预训练词表中寻找它们的初始化表示。

>>> vocab = Vocabulary().add_word_lst(["The", "the", "THE"])


Loading…
Cancel
Save