Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
hapi
提交
a13654b9
H
hapi
项目概览
PaddlePaddle
/
hapi
通知
11
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
7
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hapi
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
7
合并请求
7
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
a13654b9
编写于
5月 13, 2020
作者:
G
guosheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix example codes in hapi.text.
test=develop
上级
1f63da12
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
21 addition
and
16 deletion
+21
-16
hapi/text/text.py
hapi/text/text.py
+21
-16
未找到文件。
hapi/text/text.py
浏览文件 @
a13654b9
...
@@ -474,7 +474,7 @@ class RNN(Layer):
...
@@ -474,7 +474,7 @@ class RNN(Layer):
inputs = paddle.rand((2, 4, 32))
inputs = paddle.rand((2, 4, 32))
cell = StackedLSTMCell(input_size=32, hidden_size=64)
cell = StackedLSTMCell(input_size=32, hidden_size=64)
rnn = RNN(cell=cell
, inputs=inputs
)
rnn = RNN(cell=cell)
outputs, _ = rnn(inputs) # [2, 4, 64]
outputs, _ = rnn(inputs) # [2, 4, 64]
"""
"""
...
@@ -771,7 +771,7 @@ class StackedLSTMCell(RNNCell):
...
@@ -771,7 +771,7 @@ class StackedLSTMCell(RNNCell):
inputs = paddle.rand((2, 4, 32))
inputs = paddle.rand((2, 4, 32))
cell = StackedLSTMCell(input_size=32, hidden_size=64)
cell = StackedLSTMCell(input_size=32, hidden_size=64)
rnn = RNN(cell=cell
, inputs=inputs
)
rnn = RNN(cell=cell)
outputs, _ = rnn(inputs) # [2, 4, 64]
outputs, _ = rnn(inputs) # [2, 4, 64]
"""
"""
...
@@ -1001,7 +1001,7 @@ class BidirectionalRNN(Layer):
...
@@ -1001,7 +1001,7 @@ class BidirectionalRNN(Layer):
.. code-block:: python
.. code-block:: python
import paddle
import paddle
from paddle.incubate.hapi.text import
BasicLSTMCell, StackedRNNCell
from paddle.incubate.hapi.text import
StackedLSTMCell, BidirectionalRNN
inputs = paddle.rand((2, 4, 32))
inputs = paddle.rand((2, 4, 32))
cell_fw = StackedLSTMCell(32, 64)
cell_fw = StackedLSTMCell(32, 64)
...
@@ -1362,11 +1362,11 @@ class StackedGRUCell(RNNCell):
...
@@ -1362,11 +1362,11 @@ class StackedGRUCell(RNNCell):
import paddle
import paddle
import paddle.fluid as fluid
import paddle.fluid as fluid
from paddle.incubate.hapi.text import Stacked
LSTM
Cell, RNN
from paddle.incubate.hapi.text import Stacked
GRU
Cell, RNN
inputs = paddle.rand((2, 4, 32))
inputs = paddle.rand((2, 4, 32))
cell = StackedGRUCell(input_size=32, hidden_size=64)
cell = StackedGRUCell(input_size=32, hidden_size=64)
rnn = RNN(cell=cell
, inputs=inputs
)
rnn = RNN(cell=cell)
outputs, _ = rnn(inputs) # [2, 4, 64]
outputs, _ = rnn(inputs) # [2, 4, 64]
"""
"""
...
@@ -1502,7 +1502,7 @@ class GRU(Layer):
...
@@ -1502,7 +1502,7 @@ class GRU(Layer):
import paddle
import paddle
import paddle.fluid as fluid
import paddle.fluid as fluid
from paddle.incubate.hapi.text import
LSTM
from paddle.incubate.hapi.text import
GRU
inputs = paddle.rand((2, 4, 32))
inputs = paddle.rand((2, 4, 32))
gru = GRU(input_size=32, hidden_size=64, num_layers=2)
gru = GRU(input_size=32, hidden_size=64, num_layers=2)
...
@@ -1625,7 +1625,7 @@ class BidirectionalGRU(Layer):
...
@@ -1625,7 +1625,7 @@ class BidirectionalGRU(Layer):
from paddle.incubate.hapi.text import BidirectionalGRU
from paddle.incubate.hapi.text import BidirectionalGRU
inputs = paddle.rand((2, 4, 32))
inputs = paddle.rand((2, 4, 32))
gru = BidirectionalGRU(input_size=32, hidden_size=64, num_layers=2)
bi_
gru = BidirectionalGRU(input_size=32, hidden_size=64, num_layers=2)
outputs, _ = bi_gru(inputs) # [2, 4, 128]
outputs, _ = bi_gru(inputs) # [2, 4, 128]
"""
"""
...
@@ -1779,6 +1779,7 @@ class DynamicDecode(Layer):
...
@@ -1779,6 +1779,7 @@ class DynamicDecode(Layer):
import paddle
import paddle
import paddle.fluid as fluid
import paddle.fluid as fluid
from paddle.fluid.layers import BeamSearchDecoder
from paddle.incubate.hapi.text import StackedLSTMCell, DynamicDecode
from paddle.incubate.hapi.text import StackedLSTMCell, DynamicDecode
vocab_size, d_model, = 100, 32
vocab_size, d_model, = 100, 32
...
@@ -2693,7 +2694,7 @@ class MultiHeadAttention(Layer):
...
@@ -2693,7 +2694,7 @@ class MultiHeadAttention(Layer):
query = paddle.rand((2, 4, 128))
query = paddle.rand((2, 4, 128))
# self attention bias: [batch_size, n_head, src_len, src_len]
# self attention bias: [batch_size, n_head, src_len, src_len]
attn_bias = paddle.rand((2, 2, 4, 4))
attn_bias = paddle.rand((2, 2, 4, 4))
multi_head_attn = MultiHeadAttention(64, 64,
2, 128
)
multi_head_attn = MultiHeadAttention(64, 64,
128, n_head=2
)
output = multi_head_attn(query, attn_bias=attn_bias) # [2, 4, 128]
output = multi_head_attn(query, attn_bias=attn_bias) # [2, 4, 128]
"""
"""
...
@@ -2976,8 +2977,8 @@ class TransformerEncoderLayer(Layer):
...
@@ -2976,8 +2977,8 @@ class TransformerEncoderLayer(Layer):
enc_input = paddle.rand((2, 4, 128))
enc_input = paddle.rand((2, 4, 128))
# self attention bias: [batch_size, n_head, src_len, src_len]
# self attention bias: [batch_size, n_head, src_len, src_len]
attn_bias = paddle.rand((2, 2, 4, 4))
attn_bias = paddle.rand((2, 2, 4, 4))
encoder_layer = TransformerEncoderLayer(2,
2,
64, 64, 128, 512)
encoder_layer = TransformerEncoderLayer(2, 64, 64, 128, 512)
enc_output = encoder_layer(
inputs
, attn_bias) # [2, 4, 128]
enc_output = encoder_layer(
enc_input
, attn_bias) # [2, 4, 128]
"""
"""
def
__init__
(
self
,
def
__init__
(
self
,
...
@@ -3080,7 +3081,7 @@ class TransformerEncoder(Layer):
...
@@ -3080,7 +3081,7 @@ class TransformerEncoder(Layer):
# self attention bias: [batch_size, n_head, src_len, src_len]
# self attention bias: [batch_size, n_head, src_len, src_len]
attn_bias = paddle.rand((2, 2, 4, 4))
attn_bias = paddle.rand((2, 2, 4, 4))
encoder = TransformerEncoder(2, 2, 64, 64, 128, 512)
encoder = TransformerEncoder(2, 2, 64, 64, 128, 512)
enc_output = encoder(
inputs
, attn_bias) # [2, 4, 128]
enc_output = encoder(
enc_input
, attn_bias) # [2, 4, 128]
"""
"""
def
__init__
(
self
,
def
__init__
(
self
,
...
@@ -3536,6 +3537,7 @@ class LinearChainCRF(Layer):
...
@@ -3536,6 +3537,7 @@ class LinearChainCRF(Layer):
.. code-block:: python
.. code-block:: python
import numpy as np
import paddle
import paddle
import paddle.fluid as fluid
import paddle.fluid as fluid
from paddle.incubate.hapi.text import LinearChainCRF
from paddle.incubate.hapi.text import LinearChainCRF
...
@@ -3544,9 +3546,10 @@ class LinearChainCRF(Layer):
...
@@ -3544,9 +3546,10 @@ class LinearChainCRF(Layer):
emission = paddle.rand((2, 8, 5))
emission = paddle.rand((2, 8, 5))
# label: [batch_size, sequence_length, num_tags]
# label: [batch_size, sequence_length, num_tags]
# dummy label just for example usage
# dummy label just for example usage
label = fluid.layers.ones((2, 8, 5), dtype='int64')
label = paddle.ones((2, 8), dtype='int64')
length = fluid.layers.assign(np.array([6, 8]).astype('int64'))
crf = LinearChainCRF(size=5)
crf = LinearChainCRF(size=5)
cost = crf(emission, label) # [2, 1]
cost = crf(emission, label
, length
) # [2, 1]
"""
"""
def
__init__
(
self
,
size
,
param_attr
=
None
,
dtype
=
'float32'
):
def
__init__
(
self
,
size
,
param_attr
=
None
,
dtype
=
'float32'
):
...
@@ -3667,9 +3670,10 @@ class CRFDecoding(Layer):
...
@@ -3667,9 +3670,10 @@ class CRFDecoding(Layer):
from paddle.incubate.hapi.text import CRFDecoding
from paddle.incubate.hapi.text import CRFDecoding
# emission: [batch_size, sequence_length, num_tags]
# emission: [batch_size, sequence_length, num_tags]
emission = paddle.rand((2, 8, 5))
emission = paddle.rand((2, 8, 5))
length = fluid.layers.assign(np.array([6, 8]).astype('int64'))
crf_decoding = CRFDecoding(size=5)
crf_decoding = CRFDecoding(size=5)
cost = crf_decoding(emission) # [2, 8]
cost = crf_decoding(emission
, length
) # [2, 8]
"""
"""
def
__init__
(
self
,
size
,
param_attr
=
None
,
dtype
=
'float32'
):
def
__init__
(
self
,
size
,
param_attr
=
None
,
dtype
=
'float32'
):
...
@@ -3836,7 +3840,8 @@ class SequenceTagging(Layer):
...
@@ -3836,7 +3840,8 @@ class SequenceTagging(Layer):
from paddle.incubate.hapi.text import SequenceTagging
from paddle.incubate.hapi.text import SequenceTagging
# word: [batch_size, sequence_length]
# word: [batch_size, sequence_length]
word = fluid.layers.ones([2, 8]) # dummy input just for example
# dummy input just for example
word = paddle.ones((2, 8), dtype='int64')
length = fluid.layers.assign(np.array([6, 8]).astype('int64'))
length = fluid.layers.assign(np.array([6, 8]).astype('int64'))
seq_tagger = SequenceTagging(vocab_size=100, num_labels=5)
seq_tagger = SequenceTagging(vocab_size=100, num_labels=5)
outputs = seq_tagger(word, length)
outputs = seq_tagger(word, length)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录