@ -662,6 +662,7 @@ class Categorical(Distribution):
Args :
Args :
logits ( list | numpy . ndarray | Tensor ) : The logits input of categorical distribution . The data type is float32 or float64 .
logits ( list | numpy . ndarray | Tensor ) : The logits input of categorical distribution . The data type is float32 or float64 .
name ( str , optional ) : Name for the operation ( optional , default is None ) . For more information , please refer to : ref : ` api_guide_Name ` .
Examples :
Examples :
. . code - block : : python
. . code - block : : python
@ -669,41 +670,46 @@ class Categorical(Distribution):
import paddle
import paddle
from paddle . distribution import Categorical
from paddle . distribution import Categorical
paddle . manual_seed ( 100 ) # on CPU device
x = paddle . rand ( [ 6 ] )
x = paddle . rand ( [ 6 ] )
print ( x . numpy ( ) )
print ( x . numpy ( ) )
# [0.32564053, 0.99334985, 0.99034804,
# [0.5535528 0.20714243 0.01162981
# 0.09053693, 0.30820143, 0.19095989]
# 0.51577556 0.36369765 0.2609165 ]
paddle . manual_seed ( 200 ) # on CPU device
y = paddle . rand ( [ 6 ] )
y = paddle . rand ( [ 6 ] )
print ( y . numpy ( ) )
print ( y . numpy ( ) )
# [0.6365463 , 0.7278677 , 0.90260243,
# [0.77663314 0.90824795 0.15685187
# 0.5226815 , 0.35837543, 0.13981032 ]
# 0.04279523 0.34468332 0.7955718 ]
cat = Categorical ( x )
cat = Categorical ( x )
cat2 = Categorical ( y )
cat2 = Categorical ( y )
paddle . manual_seed ( 1000 ) # on CPU device
cat . sample ( [ 2 , 3 ] )
cat . sample ( [ 2 , 3 ] )
# [[5, 1, 1 ],
# [[0, 0, 5 ],
# [0, 1, 2 ]]
# [3, 4, 5 ]]
cat . entropy ( )
cat . entropy ( )
# [1.7 188 7]
# [1.7 7528 ]
cat . kl_divergence ( cat2 )
cat . kl_divergence ( cat2 )
# [0.0278455 ]
# [0.071952 ]
value = paddle . to_tensor ( [ 2 , 1 , 3 ] )
value = paddle . to_tensor ( [ 2 , 1 , 3 ] )
cat . probs ( value )
cat . probs ( value )
# [0.341613 0.342648 0.03123 ]
# [0.00608027 0.108298 0.269656 ]
cat . log_prob ( value )
cat . log_prob ( value )
# [-1.07408 -1.07105 -3.46638 ]
# [-5.10271 -2.22287 -1.31061 ]
"""
"""
def __init__ ( self , logits , name = None ) :
def __init__ ( self , logits , name = None ) :
"""
"""
Args :
Args :
logits ( list | numpy . ndarray | Variable ) : The logits input of categorical distribution . The data type is float32 or float64 .
logits ( list | numpy . ndarray | Tensor ) : The logits input of categorical distribution . The data type is float32 or float64 .
name ( str , optional ) : Name for the operation ( optional , default is None ) . For more information , please refer to : ref : ` api_guide_Name ` .
"""
"""
if not in_dygraph_mode ( ) :
if not in_dygraph_mode ( ) :
check_type ( logits , ' logits ' , ( np . ndarray , tensor . Variable , list ) ,
check_type ( logits , ' logits ' , ( np . ndarray , tensor . Variable , list ) ,
@ -738,16 +744,18 @@ class Categorical(Distribution):
import paddle
import paddle
from paddle . distribution import Categorical
from paddle . distribution import Categorical
paddle . manual_seed ( 100 ) # on CPU device
x = paddle . rand ( [ 6 ] )
x = paddle . rand ( [ 6 ] )
print ( x . numpy ( ) )
print ( x . numpy ( ) )
# [0.32564053, 0.99334985, 0.99034804,
# [0.5535528 0.20714243 0.01162981
# 0.09053693, 0.30820143, 0.19095989 ]
# 0.51577556 0.36369765 0.2609165 ]
cat = Categorical ( x )
cat = Categorical ( x )
paddle . manual_seed ( 1000 ) # on CPU device
cat . sample ( [ 2 , 3 ] )
cat . sample ( [ 2 , 3 ] )
# [[5, 1, 1 ],
# [[0, 0, 5 ],
# [0, 1, 2 ]]
# [3, 4, 5 ]]
"""
"""
name = self . name + ' _sample '
name = self . name + ' _sample '
@ -775,7 +783,7 @@ class Categorical(Distribution):
other ( Categorical ) : instance of Categorical . The data type is float32 .
other ( Categorical ) : instance of Categorical . The data type is float32 .
Returns :
Returns :
Variable : kl - divergence between two Categorical distributions .
Tensor : kl - divergence between two Categorical distributions .
Examples :
Examples :
. . code - block : : python
. . code - block : : python
@ -783,20 +791,23 @@ class Categorical(Distribution):
import paddle
import paddle
from paddle . distribution import Categorical
from paddle . distribution import Categorical
paddle . manual_seed ( 100 ) # on CPU device
x = paddle . rand ( [ 6 ] )
x = paddle . rand ( [ 6 ] )
print ( x . numpy ( ) )
print ( x . numpy ( ) )
# [0.32564053, 0.99334985, 0.99034804,
# [0.5535528 0.20714243 0.01162981
# 0.09053693, 0.30820143, 0.19095989]
# 0.51577556 0.36369765 0.2609165 ]
paddle . manual_seed ( 200 ) # on CPU device
y = paddle . rand ( [ 6 ] )
y = paddle . rand ( [ 6 ] )
print ( y . numpy ( ) )
print ( y . numpy ( ) )
# [0.6365463 , 0.7278677 , 0.90260243,
# [0.77663314 0.90824795 0.15685187
# 0.5226815 , 0.35837543, 0.13981032 ]
# 0.04279523 0.34468332 0.7955718 ]
cat = Categorical ( x )
cat = Categorical ( x )
cat2 = Categorical ( y )
cat2 = Categorical ( y )
cat . kl_divergence ( cat2 )
cat . kl_divergence ( cat2 )
# [0.0278455 ]
# [0.071952 ]
"""
"""
name = self . name + ' _kl_divergence '
name = self . name + ' _kl_divergence '
@ -823,7 +834,7 @@ class Categorical(Distribution):
""" Shannon entropy in nats.
""" Shannon entropy in nats.
Returns :
Returns :
Variable : Shannon entropy of Categorical distribution . The data type is float32 .
Tensor : Shannon entropy of Categorical distribution . The data type is float32 .
Examples :
Examples :
. . code - block : : python
. . code - block : : python
@ -831,15 +842,16 @@ class Categorical(Distribution):
import paddle
import paddle
from paddle . distribution import Categorical
from paddle . distribution import Categorical
paddle . manual_seed ( 100 ) # on CPU device
x = paddle . rand ( [ 6 ] )
x = paddle . rand ( [ 6 ] )
print ( x . numpy ( ) )
print ( x . numpy ( ) )
# [0.32564053, 0.99334985, 0.99034804,
# [0.5535528 0.20714243 0.01162981
# 0.09053693, 0.30820143, 0.19095989 ]
# 0.51577556 0.36369765 0.2609165 ]
cat = Categorical ( x )
cat = Categorical ( x )
cat . entropy ( )
cat . entropy ( )
# [1.7 188 7]
# [1.7 7528 ]
"""
"""
name = self . name + ' _entropy '
name = self . name + ' _entropy '
@ -875,16 +887,17 @@ class Categorical(Distribution):
import paddle
import paddle
from paddle . distribution import Categorical
from paddle . distribution import Categorical
paddle . manual_seed ( 100 ) # on CPU device
x = paddle . rand ( [ 6 ] )
x = paddle . rand ( [ 6 ] )
print ( x . numpy ( ) )
print ( x . numpy ( ) )
# [0.32564053, 0.99334985, 0.99034804,
# [0.5535528 0.20714243 0.01162981
# 0.09053693, 0.30820143, 0.19095989 ]
# 0.51577556 0.36369765 0.2609165 ]
cat = Categorical ( x )
cat = Categorical ( x )
value = paddle . to_tensor ( [ 2 , 1 , 3 ] )
value = paddle . to_tensor ( [ 2 , 1 , 3 ] )
cat . probs ( value )
cat . probs ( value )
# [0.341613 0.342648 0.03123 ]
# [0.00608027 0.108298 0.269656 ]
"""
"""
name = self . name + ' _probs '
name = self . name + ' _probs '
@ -940,17 +953,17 @@ class Categorical(Distribution):
import paddle
import paddle
from paddle . distribution import Categorical
from paddle . distribution import Categorical
paddle . manual_seed ( 100 ) # on CPU device
x = paddle . rand ( [ 6 ] )
x = paddle . rand ( [ 6 ] )
print ( x . numpy ( ) )
print ( x . numpy ( ) )
# [0.32564053, 0.99334985, 0.99034804,
# [0.5535528 0.20714243 0.01162981
# 0.09053693, 0.30820143, 0.19095989 ]
# 0.51577556 0.36369765 0.2609165 ]
cat = Categorical ( x )
cat = Categorical ( x )
value = paddle . to_tensor ( [ 2 , 1 , 3 ] )
value = paddle . to_tensor ( [ 2 , 1 , 3 ] )
cat . log_prob ( value )
cat . log_prob ( value )
# [-1.07408 -1.07105 -3.46638 ]
# [-5.10271 -2.22287 -1.31061 ]
"""
"""
name = self . name + ' _log_prob '
name = self . name + ' _log_prob '