@@ -35,11 +35,11 @@ class LinearChainCrf(nn.Layer):
35
35
See https://repository.upenn.edu/cgi/viewcontent.cgi?article=1162&context=cis_papers for reference.
36
36
37
37
Args:
38
- num_labels (` int`):
38
+ num_labels (int):
39
39
The label number.
40
- crf_lr (` float` , optional):
40
+ crf_lr (float, optional):
41
41
The crf layer learning rate. Defaults to ``0.1``.
42
- with_start_stop_tag (` bool` , optional):
42
+ with_start_stop_tag (bool, optional):
43
43
If set to True, the start tag and stop tag will be considered, the transitions params will be a tensor with a shape of `[num_labels+2, num_labels+2]`.
44
44
Else, the transitions params will be a tensor with a shape of `[num_labels, num_labels]`.
45
45
"""
@@ -105,14 +105,13 @@ def forward(self, inputs, lengths):
105
105
Further, We can get F(n) is a recursive formula with F(n-1).
106
106
107
107
Args:
108
- inputs (` Tensor`):
108
+ inputs (Tensor):
109
109
The input predicted tensor. Its dtype is float32 and has a shape of `[batch_size, sequence_length, num_tags]`.
110
- lengths (` Tensor`):
110
+ lengths (Tensor):
111
111
The input length. Its dtype is int64 and has a shape of `[batch_size]`.
112
112
113
113
Returns:
114
- norm_score (`Tensor`):
115
- The normalizers tensor. Its dtype is float32 and has a shape of `[batch_size]`.
114
+ Tensor: Returns the normalizers tensor `norm_score`. Its dtype is float32 and has a shape of `[batch_size]`.
116
115
"""
117
116
batch_size , seq_len , n_labels = inputs .shape
118
117
inputs_t_exp = inputs .transpose ([1 , 0 , 2 ]).unsqueeze (- 1 )
@@ -154,16 +153,15 @@ def gold_score(self, inputs, labels, lengths):
154
153
$$ score(x,y) = \\ sum_i Emit(x_i,y_i) + Trans(y_{i-1}, y_i) $$
155
154
156
155
Args:
157
- inputs (` Tensor`):
156
+ inputs (Tensor):
158
157
The input predicted tensor. Its dtype is float32 and has a shape of `[batch_size, sequence_length, num_tags]`.
159
- labels (` Tensor`) :
158
+ labels (Tensor):
160
159
The input label tensor. Its dtype is int64 and has a shape of `[batch_size, sequence_length]`
161
- lengths (` Tensor`):
160
+ lengths (Tensor):
162
161
The input length. Its dtype is int64 and has a shape of `[batch_size]`.
163
162
164
163
Returns:
165
- unnorm_score (`Tensor`):
166
- The unnormalized sequence scores tensor. Its dtype is float32 and has a shape of `[batch_size]`.
164
+ Tensor: Returns the unnormalized sequence scores tensor `unnorm_score`. Its dtype is float32 and has a shape of `[batch_size]`.
167
165
"""
168
166
unnorm_score = self ._point_score (
169
167
inputs , labels , lengths ) + self ._trans_score (labels , lengths )
@@ -268,7 +266,7 @@ class LinearChainCrfLoss(nn.Layer):
268
266
The negative log-likelihood for linear chain Conditional Random Field (CRF).
269
267
270
268
Args:
271
- crf (` LinearChainCrf`):
269
+ crf (LinearChainCrf):
272
270
The `LinearChainCrf` network object. Its parameter will be used to calculate the loss.
273
271
"""
274
272
@@ -286,16 +284,16 @@ def forward(self, inputs, lengths, labels, old_version_labels=None):
286
284
then we have $$ loss = -logp(y|x) = -log(exp(score(x,y))/Z(x)) = -score(x,y) + logZ(x) $$
287
285
288
286
Args:
289
- inputs (` Tensor`):
287
+ inputs (Tensor):
290
288
The input predicted tensor. Its dtype is float32 and has a shape of `[batch_size, sequence_length, num_tags]`.
291
- lengths (` Tensor`):
289
+ lengths (Tensor):
292
290
The input length. Its dtype is int64 and has a shape of `[batch_size]`.
293
- labels (` Tensor` ) :
291
+ labels (Tensor) :
294
292
The input label tensor. Its dtype is int64 and has a shape of `[batch_size, sequence_length]`
295
- old_version_labels (` Tensor` , optional): Unnecessary parameter for compatibility with older versions. Defaults to ``None``.
293
+ old_version_labels (Tensor, optional): Unnecessary parameter for compatibility with older versions. Defaults to ``None``.
296
294
297
295
Returns:
298
- loss (` Tensor`) : The crf loss. Its dtype is float32 and has a shape of `[batch_size]`.
296
+ Tensor: The crf loss. Its dtype is float32 and has a shape of `[batch_size]`.
299
297
"""
300
298
# Note: When closing to convergence, the loss could be a small negative number. This may caused by underflow when calculating exp in logsumexp.
301
299
# We add relu here to avoid negative loss. In theory, the crf loss must be greater than or equal to 0, relu will not impact on it.
@@ -318,9 +316,9 @@ class ViterbiDecoder(nn.Layer):
318
316
ViterbiDecoder can decode the highest scoring sequence of tags, it should only be used at test time.
319
317
320
318
Args:
321
- transitions (` Tensor`):
319
+ transitions (Tensor):
322
320
The transition matrix. Its dtype is float32 and has a shape of `[num_tags, num_tags]`.
323
- with_start_stop_tag (` bool` , optional):
321
+ with_start_stop_tag (bool, optional):
324
322
If set to True, the last row and the last column of transitions will be considered as start tag,
325
323
the the penultimate row and the penultimate column of transitions will be considered as stop tag.
326
324
Else, all the rows and columns will be considered as the real tag. Defaults to ``None``.
@@ -363,15 +361,16 @@ def forward(self, inputs, lengths):
363
361
Decode the highest scoring sequence of tags.
364
362
365
363
Args:
366
- inputs (` Tensor`):
364
+ inputs (Tensor):
367
365
The unary emission tensor. Its dtype is float32 and has a shape of `[batch_size, sequence_length, num_tags]`.
368
- length (` Tensor`):
366
+ length (Tensor):
369
367
The input length tensor storing real length of each sequence for correctness. Its dtype is int64 and has a shape of `[batch_size]`.
368
+
370
369
Returns:
371
- scores(`Tensor`):
372
- The scores tensor containing the score for the Viterbi sequence. Its dtype is float32 and has a shape of `[batch_size]`.
373
- paths(`Tensor`):
374
- The paths tensor containing the highest scoring tag indices. Its dtype is int64 and has a shape of `[batch_size, sequence_length`] .
370
+ tuple: Returns tuple (scores, paths). The `scores` tensor containing the score for the Viterbi sequence.
371
+ Its dtype is float32 and has a shape of `[batch_size]`.
372
+ The ` paths` tensor containing the highest scoring tag indices.
373
+ Its dtype is int64 and has a shape of `[batch_size, sequence_length]` .
375
374
"""
376
375
input_shape = paddle .shape (inputs )
377
376
batch_size = input_shape [0 ]
0 commit comments