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 [45]:
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.26286653  0.47004796  0.26708551]
 [ 0.41964958  0.27437192  0.3059785 ]]
------------- bSoft --------------
[[ 0.26286653  0.47004796  0.26708551]
 [ 0.41964958  0.27437192  0.3059785 ]]
------------- cFunc --------------
[[ 0.3512183   0.35484886  0.35260266]
 [ 0.36439652  0.35483717  0.36235211]]
------------- sCostFunc --------------
2.14025563262
2.14025563262

In [34]:
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 [46]:
help(tf.placeholder)


Help on function placeholder in module tensorflow.python.ops.array_ops:

placeholder(dtype, shape=None, name=None)
    Inserts a placeholder for a tensor that will be always fed.
    
    **Important**: This tensor will produce an error if evaluated. Its value must
    be fed using the `feed_dict` optional argument to `Session.run()`,
    `Tensor.eval()`, or `Operation.run()`.
    
    For example:
    
    ```python
    x = tf.placeholder(tf.float32, shape=(1024, 1024))
    y = tf.matmul(x, x)
    
    with tf.Session() as sess:
      print(sess.run(y))  # ERROR: will fail because x was not fed.
    
      rand_array = np.random.rand(1024, 1024)
      print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.
    ```
    
    Args:
      dtype: The type of elements in the tensor to be fed.
      shape: The shape of the tensor to be fed (optional). If the shape is not
        specified, you can feed a tensor of any shape.
      name: A name for the operation (optional).
    
    Returns:
      A `Tensor` that may be used as a handle for feeding a value, but not
      evaluated directly.


In [57]:
A=np.random.random([2,3])
print A
B=np.reshape(A,(6,1))
print B
C=A.reshape(6).T
print C


[[ 0.09729655  0.98874998  0.12292489]
 [ 0.13269659  0.38520956  0.84808923]]
[[ 0.09729655]
 [ 0.98874998]
 [ 0.12292489]
 [ 0.13269659]
 [ 0.38520956]
 [ 0.84808923]]
[ 0.09729655  0.98874998  0.12292489  0.13269659  0.38520956  0.84808923]

In [51]:
help(np.reshape)


Help on function reshape in module numpy.core.fromnumeric:

reshape(a, newshape, order='C')
    Gives a new shape to an array without changing its data.
    
    Parameters
    ----------
    a : array_like
        Array to be reshaped.
    newshape : int or tuple of ints
        The new shape should be compatible with the original shape. If
        an integer, then the result will be a 1-D array of that length.
        One shape dimension can be -1. In this case, the value is
        inferred from the length of the array and remaining dimensions.
    order : {'C', 'F', 'A'}, optional
        Read the elements of `a` using this index order, and place the
        elements into the reshaped array using this index order.  'C'
        means to read / write the elements using C-like index order,
        with the last axis index changing fastest, back to the first
        axis index changing slowest. 'F' means to read / write the
        elements using Fortran-like index order, with the first index
        changing fastest, and the last index changing slowest. Note that
        the 'C' and 'F' options take no account of the memory layout of
        the underlying array, and only refer to the order of indexing.
        'A' means to read / write the elements in Fortran-like index
        order if `a` is Fortran *contiguous* in memory, C-like order
        otherwise.
    
    Returns
    -------
    reshaped_array : ndarray
        This will be a new view object if possible; otherwise, it will
        be a copy.  Note there is no guarantee of the *memory layout* (C- or
        Fortran- contiguous) of the returned array.
    
    See Also
    --------
    ndarray.reshape : Equivalent method.
    
    Notes
    -----
    It is not always possible to change the shape of an array without
    copying the data. If you want an error to be raise if the data is copied,
    you should assign the new shape to the shape attribute of the array::
    
     >>> a = np.zeros((10, 2))
     # A transpose make the array non-contiguous
     >>> b = a.T
     # Taking a view makes it possible to modify the shape without modifying
     # the initial object.
     >>> c = b.view()
     >>> c.shape = (20)
     AttributeError: incompatible shape for a non-contiguous array
    
    The `order` keyword gives the index ordering both for *fetching* the values
    from `a`, and then *placing* the values into the output array.
    For example, let's say you have an array:
    
    >>> a = np.arange(6).reshape((3, 2))
    >>> a
    array([[0, 1],
           [2, 3],
           [4, 5]])
    
    You can think of reshaping as first raveling the array (using the given
    index order), then inserting the elements from the raveled array into the
    new array using the same kind of index ordering as was used for the
    raveling.
    
    >>> np.reshape(a, (2, 3)) # C-like index ordering
    array([[0, 1, 2],
           [3, 4, 5]])
    >>> np.reshape(np.ravel(a), (2, 3)) # equivalent to C ravel then C reshape
    array([[0, 1, 2],
           [3, 4, 5]])
    >>> np.reshape(a, (2, 3), order='F') # Fortran-like index ordering
    array([[0, 4, 3],
           [2, 1, 5]])
    >>> np.reshape(np.ravel(a, order='F'), (2, 3), order='F')
    array([[0, 4, 3],
           [2, 1, 5]])
    
    Examples
    --------
    >>> a = np.array([[1,2,3], [4,5,6]])
    >>> np.reshape(a, 6)
    array([1, 2, 3, 4, 5, 6])
    >>> np.reshape(a, 6, order='F')
    array([1, 4, 2, 5, 3, 6])
    
    >>> np.reshape(a, (3,-1))       # the unspecified value is inferred to be 2
    array([[1, 2],
           [3, 4],
           [5, 6]])


In [58]:
from scipy import interpolate

In [59]:
help(interpolate.interp1d)


Help on class interp1d in module scipy.interpolate.interpolate:

class interp1d(scipy.interpolate.polyint._Interpolator1D)
 |  Interpolate a 1-D function.
 |  
 |  `x` and `y` are arrays of values used to approximate some function f:
 |  ``y = f(x)``.  This class returns a function whose call method uses
 |  interpolation to find the value of new points.
 |  
 |  Note that calling `interp1d` with NaNs present in input values results in
 |  undefined behaviour.
 |  
 |  Parameters
 |  ----------
 |  x : (N,) array_like
 |      A 1-D array of real values.
 |  y : (...,N,...) array_like
 |      A N-D array of real values. The length of `y` along the interpolation
 |      axis must be equal to the length of `x`.
 |  kind : str or int, optional
 |      Specifies the kind of interpolation as a string
 |      ('linear', 'nearest', 'zero', 'slinear', 'quadratic', 'cubic'
 |      where 'zero', 'slinear', 'quadratic' and 'cubic' refer to a spline
 |      interpolation of zeroth, first, second or third order) or as an
 |      integer specifying the order of the spline interpolator to use.
 |      Default is 'linear'.
 |  axis : int, optional
 |      Specifies the axis of `y` along which to interpolate.
 |      Interpolation defaults to the last axis of `y`.
 |  copy : bool, optional
 |      If True, the class makes internal copies of x and y.
 |      If False, references to `x` and `y` are used. The default is to copy.
 |  bounds_error : bool, optional
 |      If True, a ValueError is raised any time interpolation is attempted on
 |      a value outside of the range of x (where extrapolation is
 |      necessary). If False, out of bounds values are assigned `fill_value`.
 |      By default, an error is raised unless `fill_value="extrapolate"`.
 |  fill_value : array-like or (array-like, array_like) or "extrapolate", optional
 |      - if a ndarray (or float), this value will be used to fill in for
 |        requested points outside of the data range. If not provided, then
 |        the default is NaN. The array-like must broadcast properly to the
 |        dimensions of the non-interpolation axes.
 |      - If a two-element tuple, then the first element is used as a
 |        fill value for ``x_new < x[0]`` and the second element is used for
 |        ``x_new > x[-1]``. Anything that is not a 2-element tuple (e.g.,
 |        list or ndarray, regardless of shape) is taken to be a single
 |        array-like argument meant to be used for both bounds as
 |        ``below, above = fill_value, fill_value``.
 |  
 |        .. versionadded:: 0.17.0
 |      - If "extrapolate", then points outside the data range will be
 |        extrapolated.
 |  
 |        .. versionadded:: 0.17.0
 |  assume_sorted : bool, optional
 |      If False, values of `x` can be in any order and they are sorted first.
 |      If True, `x` has to be an array of monotonically increasing values.
 |  
 |  Methods
 |  -------
 |  __call__
 |  
 |  See Also
 |  --------
 |  splrep, splev
 |      Spline interpolation/smoothing based on FITPACK.
 |  UnivariateSpline : An object-oriented wrapper of the FITPACK routines.
 |  interp2d : 2-D interpolation
 |  
 |  Examples
 |  --------
 |  >>> import matplotlib.pyplot as plt
 |  >>> from scipy import interpolate
 |  >>> x = np.arange(0, 10)
 |  >>> y = np.exp(-x/3.0)
 |  >>> f = interpolate.interp1d(x, y)
 |  
 |  >>> xnew = np.arange(0, 9, 0.1)
 |  >>> ynew = f(xnew)   # use interpolation function returned by `interp1d`
 |  >>> plt.plot(x, y, 'o', xnew, ynew, '-')
 |  >>> plt.show()
 |  
 |  Method resolution order:
 |      interp1d
 |      scipy.interpolate.polyint._Interpolator1D
 |      __builtin__.object
 |  
 |  Methods defined here:
 |  
 |  __init__(self, x, y, kind='linear', axis=-1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False)
 |      Initialize a 1D linear interpolation class.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  fill_value
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from scipy.interpolate.polyint._Interpolator1D:
 |  
 |  __call__(self, x)
 |      Evaluate the interpolant
 |      
 |      Parameters
 |      ----------
 |      x : array_like
 |          Points to evaluate the interpolant at.
 |      
 |      Returns
 |      -------
 |      y : array_like
 |          Interpolated values. Shape is determined by replacing
 |          the interpolation axis in the original array with the shape of x.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from scipy.interpolate.polyint._Interpolator1D:
 |  
 |  dtype


In [ ]: