1
1
"""
2
- Compute the Perplexity which useful in predicting language model
2
+ Compute the Perplexity which useful in predicting language model
3
3
accuracy in Natural Language Processing (NLP.)
4
4
Perplexity is measure of how certain the model in its predictions.
5
5
6
- Formula :
6
+ Formula :
7
7
8
8
Perplexity Loss = exp(-1/N (Σ ln(p(x)))
9
9
@@ -29,45 +29,45 @@ def perplexity_loss(y_true: np.ndarray, y_pred: np.ndarray) -> float:
29
29
Perplexity loss between y_true and y_pred.
30
30
31
31
>>> y_true = np.array([[1, 4], [2, 3]])
32
- >>> y_pred = np.array( \
33
- [[[0.28, 0.19, 0.21 , 0.15, 0.15], \
34
- [0.24, 0.19, 0.09, 0.18, 0.27]], \
35
- [[0.03, 0.26, 0.21, 0.18, 0.30], \
36
- [0.28, 0.10, 0.33, 0.15, 0.12]]]\
37
- )
32
+ >>> y_pred = np.array(
33
+ ... [[[0.28, 0.19, 0.21 , 0.15, 0.15],
34
+ ... [0.24, 0.19, 0.09, 0.18, 0.27]],
35
+ ... [[0.03, 0.26, 0.21, 0.18, 0.30],
36
+ ... [0.28, 0.10, 0.33, 0.15, 0.12]]]
37
+ ... )
38
38
>>> perplexity_loss(y_true, y_pred)
39
39
5.024732177979022
40
40
>>> y_true = np.array([[1, 4], [2, 3]])
41
- >>> y_pred = np.array( \
42
- [[[0.28, 0.19, 0.21 , 0.15, 0.15], \
43
- [0.24, 0.19, 0.09, 0.18, 0.27], \
44
- [0.30, 0.10, 0.20, 0.15, 0.25]], \
45
- [[0.03, 0.26, 0.21, 0.18, 0.30], \
46
- [0.28, 0.10, 0.33, 0.15, 0.12], \
47
- [0.30, 0.10, 0.20, 0.15, 0.25]],] \
48
- )
41
+ >>> y_pred = np.array(
42
+ ... [[[0.28, 0.19, 0.21 , 0.15, 0.15],
43
+ ... [0.24, 0.19, 0.09, 0.18, 0.27],
44
+ ... [0.30, 0.10, 0.20, 0.15, 0.25]],
45
+ ... [[0.03, 0.26, 0.21, 0.18, 0.30],
46
+ ... [0.28, 0.10, 0.33, 0.15, 0.12],
47
+ ... [0.30, 0.10, 0.20, 0.15, 0.25]],]
48
+ ... )
49
49
>>> perplexity_loss(y_true, y_pred)
50
50
Traceback (most recent call last):
51
51
...
52
52
ValueError: Sentence length of y_true and y_pred must be equal.
53
53
>>> y_true = np.array([[1, 4], [2, 11]])
54
- >>> y_pred = np.array( \
55
- [[[0.28, 0.19, 0.21 , 0.15, 0.15], \
56
- [0.24, 0.19, 0.09, 0.18, 0.27]], \
57
- [[0.03, 0.26, 0.21, 0.18, 0.30], \
58
- [0.28, 0.10, 0.33, 0.15, 0.12]]]\
59
- )
54
+ >>> y_pred = np.array(
55
+ ... [[[0.28, 0.19, 0.21 , 0.15, 0.15],
56
+ ... [0.24, 0.19, 0.09, 0.18, 0.27]],
57
+ ... [[0.03, 0.26, 0.21, 0.18, 0.30],
58
+ ... [0.28, 0.10, 0.33, 0.15, 0.12]]]
59
+ ... )
60
60
>>> perplexity_loss(y_true, y_pred)
61
61
Traceback (most recent call last):
62
62
...
63
63
ValueError: Label value must not be greater than vocabulary size.
64
64
>>> y_true = np.array([[1, 4]])
65
- >>> y_pred = np.array( \
66
- [[[0.28, 0.19, 0.21 , 0.15, 0.15], \
67
- [0.24, 0.19, 0.09, 0.18, 0.27]], \
68
- [[0.03, 0.26, 0.21, 0.18, 0.30], \
69
- [0.28, 0.10, 0.33, 0.15, 0.12]]]\
70
- )
65
+ >>> y_pred = np.array(
66
+ ... [[[0.28, 0.19, 0.21 , 0.15, 0.15],
67
+ ... [0.24, 0.19, 0.09, 0.18, 0.27]],
68
+ ... [[0.03, 0.26, 0.21, 0.18, 0.30],
69
+ ... [0.28, 0.10, 0.33, 0.15, 0.12]]]
70
+ ... )
71
71
>>> perplexity_loss(y_true, y_pred)
72
72
Traceback (most recent call last):
73
73
...
0 commit comments