In [1]:
import OVFM.FeatureMap as fm
import OVFM.Model as mdl
from OVFM import DataGeneration as dg
import numpy as np
import time

from bokeh.plotting import figure, output_file, output_notebook, show, VBox, HBox, vplot
from bokeh.models import Range1d

In [2]:
def gaussian_bound( D, sigma, diam, d, epsilon ):
    return ( 2 ** 8 ) * ( ( sigma * diam / epsilon ) ** 2 ) * np.exp( - D * epsilon ** 2 / ( 4 * ( d + 2 ) ) )

In [3]:
i_dim   = 20
nb_data = 100
exp_dim = 1000

#generate
t1 = time.clock( )
features = dg.generate_inputs( nb_data, i_dim )
print 'Generate time: ', time.clock( ) - t1

# AE = np.cov( targets.T )
sup_boundG = np.empty( 10 )

phi = fm.GaussianFF( 1, i_dim, 1 )
G = phi.kernel_exact( features )

t1 = time.clock( )
for idx_exp, exp in enumerate( np.logspace( 1, 5, 10, base = 10 ) ):
    phi = fm.GaussianFF( 1, i_dim, int( exp ) )
    OVFM_G = phi.kernel_approx( features )
    sup_boundG[ idx_exp ] = ( np.abs( OVFM_G - G ) ).max( )

print 'Approximation time: ', time.clock( ) - t1
print sup_boundG

output_file( "KernelApproximation.html" )

p1 = figure( title="Kernel Approximation", x_axis_type='log' )

D = np.logspace( 1, 8, 10, base = 10 )
p1.square( D, sup_boundG, line_color="blue", fill_color = None )
p1.line( D, sup_boundG, line_color="blue", legend = 'Gaussian kernel' )
# p1.line( D, gaussian_bound( D, np.sqrt( 2 ), np.sqrt( 8 ), i_dim, 0.01 ), line_color="red", legend = 'Theoretical bound' )

p1.xaxis.axis_label = 'D'
p1.yaxis.axis_label = 'max_{x,y} |S(x,y)-K(x,y)|'

show( vplot( p1 ) )


Generate time:  0.001262
Approximation time:  1.333757
[ 0.85464147  0.46905766  0.29056544  0.19190097  0.10558305  0.07506985
  0.04043058  0.02469282  0.01323001  0.00937031]

In [4]:
i_dim   = 4
o_dim   = 4
nb_data = 100
exp_dim = 1000

#generate
t1 = time.clock( )
features = dg.generate_inputs( nb_data, i_dim )
print 'Generate time: ', time.clock( ) - t1

# AE = np.cov( targets.T )
sup_boundDC = np.empty( 10 )

A = np.random.rand( o_dim, o_dim )
A = np.dot( A, A.T )

phi = fm.DecomposableFF( 1, i_dim, 1, A = A )
G = phi.kernel_exact( features )

t1 = time.clock( )
for idx_exp, exp in enumerate( np.logspace( 1, 5, 10, base = 10 ) ):
    phi = fm.DecomposableFF( 1, i_dim, int( exp ), A = A )
    OVFM_G = phi.kernel_approx( features )
    sup_boundDC[ idx_exp ] = ( np.abs( OVFM_G - G ) ).max( )

print 'Approximation time: ', time.clock( ) - t1
print sup_boundDC


Generate time:  0.000519
Approximation time:  1.308329
[ 1.3219164   0.78525123  0.44831403  0.33352134  0.16497026  0.11089193
  0.09354511  0.04011033  0.02767225  0.0154696 ]

In [11]:
dim     = 4
nb_data = 100
exp_dim = 1000

#generate
t1 = time.clock( )
features = dg.generate_inputs( nb_data, dim )
print 'Generate time: ', time.clock( ) - t1

# AE = np.cov( targets.T )
sup_boundCF = np.empty( 10 )

phi = fm.CurlFreeFF( 1, dim, 1 )
G = phi.kernel_exact( features )

t1 = time.clock( )
for idx_exp, exp in enumerate( np.logspace( 1, 5, 10, base = 10 ) ):
    phi = fm.CurlFreeFF( 1, dim, int( exp ) )
    OVFM_G = phi.kernel_approx( features )
    sup_boundCF[ idx_exp ] = ( np.abs( OVFM_G - G ) ).max( )

print 'Approximation time: ', time.clock( ) - t1
print sup_boundCF


Generate time:  0.000288000000012
Approximation time:  7.747965
[ 2.01392226  1.57375918  0.97062359  0.65037143  0.42841834  0.1939562
  0.17326998  0.08256125  0.04701454  0.02881823]

In [9]:
dim     = 4
nb_data = 100
exp_dim = 1000

#generate
t1 = time.clock( )
features = dg.generate_inputs( nb_data, dim )
print 'Generate time: ', time.clock( ) - t1

# AE = np.cov( targets.T )
sup_boundDF = np.empty( 10 )

phi = fm.DivFreeFF( 1, dim, 1 )
G = phi.kernel_exact( features )

t1 = time.clock( )
for idx_exp, exp in enumerate( np.logspace( 1, 5, 10, base = 10 ) ):
    phi = fm.DivFreeFF( 1, dim, int( exp ) )
    OVFM_G = phi.kernel_approx( features )
    sup_boundDF[ idx_exp ] = ( np.abs( OVFM_G - G ) ).max( )

print 'Approximation time: ', time.clock( ) - t1
print sup_boundDF


Generate time:  0.000253000000001
Approximation time:  40.15869
[ 4.45589501  3.46565276  2.02651712  1.26382709  0.77289582  0.56291106
  0.37188896  0.22594743  0.09619222  0.07437939]

In [6]:
dim     = 3
nb_data = 100
exp_dim = 1000

#generate
t1 = time.clock( )
features = dg.generate_inputs( nb_data, dim )
print 'Generate time: ', time.clock( ) - t1

# AE = np.cov( targets.T )
sup_boundTF = np.empty( 10 )

phi = fm.TransformableFF( 1, dim, 1 )
G = phi.kernel_exact( features )

t1 = time.clock( )
for idx_exp, exp in enumerate( np.logspace( 1, 5, 10, base = 10 ) ):
    phi = fm.TransformableFF( 1, dim, int( exp ) )
    OVFM_G = phi.kernel_approx( features )
    sup_boundTF[ idx_exp ] = ( np.abs( OVFM_G - G ) ).max( )

print 'Approximation time: ', time.clock( ) - t1
print sup_boundTF


Generate time:  0.000378
Approximation time:  5.364756
[ 0.24102004  0.15775418  0.11592097  0.06226187  0.06319042  0.00606803
  0.02315175  0.01017752  0.00417818  0.00120826]

In [7]:
i_dim   = 4
o_dim   = 4
nb_data = 100
exp_dim = 1000

A = 2 * np.random.rand( o_dim, o_dim ) - 1
A = np.dot( A, A.T ) / np.linalg.norm( A ) ** 2

P = np.zeros( ( o_dim, i_dim, i_dim ) )
for i in xrange( 0, i_dim ):
    P[ i, :, : ] = 2 * np.random.rand( o_dim, o_dim ) - 1

#generate
t1 = time.clock( )
features = dg.generate_inputs( nb_data, i_dim )
print 'Generate time: ', time.clock( ) - t1

# AE = np.cov( targets.T )
sup_boundDT = np.empty( 10 )

A = np.random.rand( o_dim, o_dim )
A = np.dot( A, A.T )

phi = fm.DeepTransformableFF( 1, i_dim, 1, P = P, A = A )
G = phi.kernel_exact( features )

t1 = time.clock( )
for idx_exp, exp in enumerate( np.logspace( 1, 5, 10, base = 10 ) ):
    phi = fm.DeepTransformableFF( 1, i_dim, int( exp ), P = P, A = A )
    OVFM_G = phi.kernel_approx( features )
    sup_boundDT[ idx_exp ] = ( np.abs( OVFM_G - G ) ).max( )

print 'Approximation time: ', time.clock( ) - t1
print sup_boundDT


Generate time:  0.000163000000001
Approximation time:  32.249423
[ 1.47129299  0.82110761  0.56964318  0.2694578   0.16872825  0.13384131
  0.06824949  0.03925027  0.02113226  0.01424357]

In [13]:
output_file( "KernelApproximation.html" )

p1 = figure( title="Kernel Approximation", x_axis_type='log' )

p1.square( np.logspace( 1, 5, 10, base = 10 ), sup_boundDC, line_color="blue", fill_color = None )
p1.square( np.logspace( 1, 5, 10, base = 10 ), sup_boundDT, line_color="green", fill_color = None )
p1.square( np.logspace( 1, 5, 10, base = 10 ), sup_boundCF, line_color="orange", fill_color = None )
p1.square( np.logspace( 1, 5, 10, base = 10 ), sup_boundDF, line_color="red", fill_color = None )
p1.square( np.logspace( 1, 5, 10, base = 10 ), sup_boundTF, line_color="black", fill_color = None )

p1.line( np.logspace( 1, 5, 10, base = 10 ), sup_boundDC, line_color="blue", legend = 'Decomposable' )
p1.line( np.logspace( 1, 5, 10, base = 10 ), sup_boundDT, line_color="green", legend = 'Deep Transformable' )
p1.line( np.logspace( 1, 5, 10, base = 10 ), sup_boundCF, line_color="orange", legend = 'Curl Free' )
p1.line( np.logspace( 1, 5, 10, base = 10 ), sup_boundDF, line_color="red", legend = 'Divergence Free' )
p1.line( np.logspace( 1, 5, 10, base = 10 ), sup_boundTF, line_color="black", legend = 'Transformable' )

p1.xaxis.axis_label = 'D'
p1.yaxis.axis_label = 'max_{x,z} |S(x,z)-K(x,z)|'

show( vplot( p1 ) )

In [7]:


In [ ]: