In [9]:
import tensorflow as tf
import numpy as np

In [14]:
#help(tf.nn.softmax)
#help(tf.multiply)
help(tf.log)


Help on function log in module tensorflow.python.ops.gen_math_ops:

log(x, name=None)
    Computes natural logarithm of x element-wise.
    
    I.e., \\(y = \log_e x\\).
    
    Args:
      x: A `Tensor`. Must be one of the following types: `half`, `float32`, `float64`, `complex64`, `complex128`.
      name: A name for the operation (optional).
    
    Returns:
      A `Tensor`. Has the same type as `x`.


In [32]:
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

mTest=2
nTest=3

aTest=np.random.random([mTest,nTest])
bTest=np.random.random([mTest,nTest])
#bTest=aTest

aTestSoftmax=tf.nn.softmax(aTest)
bTestSoftmax=tf.nn.softmax(bTest)

costFunc=-aTestSoftmax*tf.log(bTestSoftmax)

sumCostFunc=tf.reduce_sum(costFunc)

init=tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    aSoft=sess.run(aTestSoftmax)
    bSoft=sess.run(bTestSoftmax)
    cFunc=sess.run(costFunc)
    sCostFunc=sess.run(sumCostFunc)
    
    print('------------- aSoft --------------')
    print aSoft
    print('------------- bSoft --------------')
    print bSoft
    print('------------- cFunc --------------')
    print cFunc
    print('------------- sCostFunc --------------')
    print sCostFunc
    
    print np.sum(cFunc)


------------- aSoft --------------
[[ 0.350667    0.21688397  0.43244904]
 [ 0.38298218  0.42570304  0.19131478]]
------------- bSoft --------------
[[ 0.2846697   0.22042224  0.49490806]
 [ 0.17726964  0.41510121  0.40762915]]
------------- cFunc --------------
[[ 0.44058704  0.32797417  0.30417742]
 [ 0.66259109  0.37429212  0.1716854 ]]
------------- sCostFunc --------------
2.28130722602
2.28130722602

In [22]:
import numpy as np
import matplotlib.pyplot as plt

p=np.linspace(1e-9,1-1e-9,100)
logP=-p*np.log2(p)

q=1-p
logQ=-q*np.log2(q)

#print p
#print(np.log2(p))
#print logP

plt.figure(1)
plt.hold
plt.plot(p,logP,'b')
plt.plot(p,logQ,'r')
plt.plot(p,logP+logQ,'g')
plt.grid('on')
plt.show()



In [27]:
import numpy as np
import matplotlib.pyplot as plt

p=np.linspace(1e-9,1-1e-9,100)
logP=-p*np.log(p)

q=1-p
logQ=-q*np.log(q)

#print p
#print(np.log2(p))
#print logP

plt.figure(1)
plt.hold
plt.plot(p,logP,'b')
plt.plot(p,logQ,'r')
plt.plot(p,logP+logQ,'g')
plt.grid('on')
plt.show()



In [80]:
print -np.log2(0.1)


3.32192809489

In [81]:
print(-(0.1*np.log2(0.1)+0.9*np.log2(0.9)))


0.468995593589

In [82]:
print(-(0.5*np.log2(0.5)+0.5*np.log2(0.5)))


1.0

In [86]:
tf.log(tf.constant([2.]))


Out[86]:
<tf.Tensor 'Log_34:0' shape=(1,) dtype=float32>

In [ ]: