Exemplo n.º 1
0
"""
This example shows that most computations can be performed by numpy functions
on arrays of UTPM objects.

Just bear in mind that is much faster use UTPM instances of matrices than numpy.ndarrays
with UTPM elements.

"""

import numpy, os
from algopy import CGraph, Function, UTPM, dot, qr, eigh, inv

N, D, P = 2, 2, 1
cg = CGraph()
x = numpy.array([Function(UTPM(numpy.random.rand(*(D, P)))) for n in range(N)])
A = numpy.outer(x, x)
A = numpy.exp(A)
y = numpy.dot(A, x)

cg.independentFunctionList = list(x)
cg.dependentFunctionList = list(y)

cg.plot(os.path.join(os.path.dirname(__file__), 'numpy_dot_graph.svg'))
Exemplo n.º 2
0
from algopy import CGraph, Function
cg = CGraph()
cg.trace_on()
x = Function(1)
y = Function(3)
z = x * y + x
cg.trace_off()
cg.independentFunctionList = [x,y]
cg.dependentFunctionList = [z]
print cg
cg.plot('example_tracer_cgraph.png')
Exemplo n.º 3
0
"""
This example shows that most computations can be performed by numpy functions
on arrays of UTPM objects.

Just bear in mind that is much faster use UTPM instances of matrices than numpy.ndarrays
with UTPM elements.

"""

import numpy, os
from algopy import CGraph, Function, UTPM, dot, qr, eigh, inv

N,D,P = 2,2,1
cg = CGraph()
x = numpy.array([ Function(UTPM(numpy.random.rand(*(D,P)))) for n in range(N)])
A = numpy.outer(x,x)
A = numpy.exp(A)
y = numpy.dot(A,x)

cg.independentFunctionList = list(x)
cg.dependentFunctionList = list(y)

cg.plot(os.path.join(os.path.dirname(__file__),'numpy_dot_graph.svg'))
Exemplo n.º 4
0
# forward mode with ALGOPY
utp = logp(x, mu, sigma).data[:, 0]
print(
    'function evaluation = %f\n1st directional derivative = %f\n2nd directional derivative = %f'
    % (utp[0], 1. * utp[1], 2. * utp[2]))

# finite differences solution:
print('finite differences derivative =\n',
      (logp(x, 3.5 + 10**-8, sigma) - logp(x, 3.5, sigma)) / 10**-8)

# trace function evaluation
cg = CGraph()
mu = Function(UTPM([[3.5], [1], [0]]))  #unknown variable
out = logp(x, mu, sigma)
cg.trace_off()
cg.independentFunctionList = [mu]
cg.dependentFunctionList = [out]
cg.plot(
    os.path.join(os.path.dirname(os.path.realpath(__file__)),
                 'posterior_log_probability_cgraph.png'))

# reverse mode with ALGOPY
outbar = UTPM([[1.], [0], [0]])
cg.pullback([outbar])

gradient = mu.xbar.data[0, 0]
Hess_vec = mu.xbar.data[1, 0]

print('gradient = ', gradient)
print('Hessian vector product = ', Hess_vec)
Exemplo n.º 5
0
from algopy import CGraph, Function
cg = CGraph()
cg.trace_on()
x = Function(1)
y = Function(3)
z = x * y + x
cg.trace_off()
cg.independentFunctionList = [x, y]
cg.dependentFunctionList = [z]
print(cg)
cg.plot('example_tracer_cgraph.png')
Exemplo n.º 6
0
print 'function evaluation =\n',logp(x,3.5,sigma)

# forward mode with ALGOPY
utp = logp(x, mu, sigma).data[:,0]
print 'function evaluation = %f\n1st directional derivative = %f\n2nd directional derivative = %f'%(utp[0], 1.*utp[1], 2.*utp[2])

# finite differences solution:
print 'finite differences derivative =\n',(logp(x,3.5+10**-8,sigma) - logp(x, 3.5, sigma))/10**-8

# trace function evaluation
cg = CGraph()
mu = Function(UTPM([[3.5],[1],[0]])) #unknown variable
out = logp(x, mu, sigma)
cg.trace_off()
cg.independentFunctionList = [mu]
cg.dependentFunctionList = [out]
cg.plot(os.path.join(os.path.dirname(os.path.realpath(__file__)),'posterior_log_probability_cgraph.png'))

# reverse mode with ALGOPY
outbar = UTPM([[1.],[0],[0]])
cg.pullback([outbar])
    
gradient =  mu.xbar.data[0,0]
Hess_vec =  mu.xbar.data[1,0]

print 'gradient = ', gradient
print 'Hessian vector product = ', Hess_vec