Browse Source

Merge branch 'dev' of github.com:choosewhatulike/fastNLP-private into dev

tags/v0.4.10
yh 6 years ago
parent
commit
4d08d6f058
4 changed files with 70 additions and 25 deletions
  1. +3
    -0
      docs/Makefile
  2. +0
    -3
      fastNLP/api/__init__.py
  3. +62
    -21
      fastNLP/api/api.py
  4. +5
    -1
      fastNLP/modules/aggregator/attention.py

+ 3
- 0
docs/Makefile View File

@@ -16,6 +16,9 @@ help:
apidoc:
@$(SPHINXAPIDOC) -f -o source ../fastNLP

server:
cd build/html && python -m http.server

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new


+ 0
- 3
fastNLP/api/__init__.py View File

@@ -1,4 +1 @@
"""
这是 API 部分的注释
"""
from .api import CWS, POS, Parser

+ 62
- 21
fastNLP/api/api.py View File

@@ -1,5 +1,39 @@
"""
API.API 的文档
api.api的介绍文档
直接缩进会把上面的文字变成标题

空行缩进的写法比较合理

比较合理
*这里是斜体内容*

**这里是粗体内容**

数学公式块

.. math::
E = mc^2
.. note::
注解型提示。
.. warning::
警告型提示。

.. seealso::
`参考与超链接 <https://willqvq.github.io/doc_guide/%E6%B3%A8%E9%87%8A%E6%8C%87%E5%AF%BC>`_

普通代码块需要空一行, Example::

from fitlog import fitlog
fitlog.commit()
普通下标和上标:

H\ :sub:`2`\ O

E = mc\ :sup:`2`

"""
import warnings
@@ -28,6 +62,9 @@ model_urls = {


class API:
"""
这是 API 类的文档
"""
def __init__(self):
self.pipeline = None
self._dict = None
@@ -73,8 +110,9 @@ class POS(API):
self.load(model_path, device)

def predict(self, content):
"""

"""predict函数的介绍,
函数介绍的第二句,这句话不会换行
:param content: list of list of str. Each string is a token(word).
:return answer: list of list of str. Each string is a tag.
"""
@@ -140,13 +178,14 @@ class POS(API):


class CWS(API):
def __init__(self, model_path=None, device='cpu'):
"""
中文分词高级接口。
"""
中文分词高级接口。

:param model_path: 当model_path为None,使用默认位置的model。如果默认位置不存在,则自动下载模型
:param device: str,可以为'cpu', 'cuda'或'cuda:0'等。会将模型load到相应device进行推断。
"""
:param model_path: 当model_path为None,使用默认位置的model。如果默认位置不存在,则自动下载模型
:param device: str,可以为'cpu', 'cuda'或'cuda:0'等。会将模型load到相应device进行推断。
"""
def __init__(self, model_path=None, device='cpu'):
super(CWS, self).__init__()
if model_path is None:
model_path = model_urls['cws']
@@ -187,18 +226,20 @@ class CWS(API):
def test(self, filepath):
"""
传入一个分词文件路径,返回该数据集上分词f1, precision, recall。
分词文件应该为:
1 编者按 编者按 NN O 11 nmod:topic
2 : : PU O 11 punct
3 7月 7月 NT DATE 4 compound:nn
4 12日 12日 NT DATE 11 nmod:tmod
5 , , PU O 11 punct

1 这 这 DT O 3 det
2 款 款 M O 1 mark:clf
3 飞行 飞行 NN O 8 nsubj
4 从 从 P O 5 case
5 外型 外型 NN O 8 nmod:prep
分词文件应该为::
1 编者按 编者按 NN O 11 nmod:topic
2 : : PU O 11 punct
3 7月 7月 NT DATE 4 compound:nn
4 12日 12日 NT DATE 11 nmod:tmod
5 , , PU O 11 punct
1 这 这 DT O 3 det
2 款 款 M O 1 mark:clf
3 飞行 飞行 NN O 8 nsubj
4 从 从 P O 5 case
5 外型 外型 NN O 8 nmod:prep
以空行分割两个句子,有内容的每行有7列。

:param filepath: str, 文件路径路径。


+ 5
- 1
fastNLP/modules/aggregator/attention.py View File

@@ -112,12 +112,15 @@ class MultiHeadAttention(nn.Module):
class BiAttention(nn.Module):
"""Bi Attention module
Calculate Bi Attention matrix `e`
.. math::
\begin{array}{ll} \\
e_ij = {a}^{\mathbf{T}}_{i}{b}_{j} \\
a_i =
b_j =
\end{array}
"""

def __init__(self):
@@ -131,7 +134,8 @@ class BiAttention(nn.Module):
:param torch.Tensor x1_len: [batch_size, x1_seq_len] 第一句的0/1mask矩阵
:param torch.Tensor x2_len: [batch_size, x2_seq_len] 第二句的0/1mask矩阵
:return: torch.Tensor out_x1: [batch_size, x1_seq_len, hidden_size] 第一句attend到的特征表示
torch.Tensor out_x2: [batch_size, x2_seq_len, hidden_size] 第一句attend到的特征表示
torch.Tensor out_x2: [batch_size, x2_seq_len, hidden_size] 第一句attend到的特征表示
"""

assert in_x1.size()[0] == in_x2.size()[0]


Loading…
Cancel
Save