In [38]:
import OVFM.Model as mdl
import OVFM.FeatureMap as fm
import OVFM.DataGeneration as dg
import OVFM.Risk as rsk
import OVFM.LearningRate as lr
import OVFM.SGD as sgd
import OVFM.FISTA as fista

import numpy as np

from bokeh.plotting import figure, HBox, output_file, output_notebook, show, VBox
from bokeh.models import Range1d, HoverTool
from collections import OrderedDict 

import time

In [39]:
n = 10000
d = 2
D = 1000
p = 2

split = 0.75

In [40]:
#Data generation R2 -> R2
A = 2 * np.random.rand( p, p ) - 1
A = np.dot( A, A.T ) / np.linalg.norm( A ) ** 2
# A = np.eye( p )
# A = np.ones( ( p, p ) )
print 'A='
print A

P = np.zeros( ( p, d, d ) )
for i in xrange( 0, d ):
    P[ i, :, : ] = 2 * np.random.rand( p, p ) - 1
    # P[ i, :, : ] = np.eye( d )
    # P[ i, i, i ] = 0
    # P[ i, :, : ] = np.ones( ( d, d ) )

# P[ 0, :, : ] = np.ones( ( d, d ) )
# P[ 1, :, : ] = np.eye( d )

print 'P='
print P


A=
[[ 0.17300858 -0.34256172]
 [-0.34256172  0.82699142]]
P=
[[[ 0.90985884 -0.32691735]
  [-0.23962849  0.65207616]]

 [[-0.57083531  0.09170504]
  [ 0.0430357   0.37456537]]]

In [41]:
t = time.clock( )
features = dg.generate_inputs( n, d, 'cube' )
model    = mdl.Model( fm.DeepTransformableFF( 1.0 / d, d, D, P, A ) )
targets  = dg.generate_outputs( features, model )
print( 'Generate time: ', time.clock( ) - t )


('Generate time: ', 1.6260560000000055)

In [42]:
t = time.clock( )
split = split * n
modelt = mdl.Model( fm.DeepTransformableFF( 1.0 / d, d, D, P, A ) )
risk  = rsk.Lasso( 1e-10 )

l = lr.Constant( 1.0 )
optimizer = sgd.SGD( risk, l, 10, 5, 0 )
optimizer.fit( modelt, features[ :split, : ], targets[ :split, : ] )

pred = modelt( features[ split:, : ] )

print( 'Learn and predict time: ', time.clock( ) - t )
print 'Test error:', ( ( pred - targets[ split:, : ] ) ** 2 ).sum( ) / targets.size, model.sparsity


('Learn and predict time: ', 14.244304999999997)
Test error: 1.91989827615e-05 0.0

In [43]:
%%capture

TOOLS="pan,wheel_zoom,box_zoom,reset,hover"

alpha = 0.6

output_file( 'DataGeneration.html' )

p1 = figure( title="Input space", tools=TOOLS )
colors = [
    "#%02x%02x%02x" % (r, g, 150) for r, g in zip( np.floor( 255 * np.abs( features[ :split, 0 ] ) ), np.floor( 255 * np.abs( features[ :split, 1 ] ) ) )
]
p1.circle( features[ :split, 0 ], features[ :split, 1 ], fill_color=colors, fill_alpha=alpha, line_color=None )
p1.select( dict( type=HoverTool ) ).tooltips
hover.tooltips = OrderedDict([
    ("(x,y)", "($x, $y)")
])

p2 = figure( title="Output space", tools=TOOLS )
p2.circle( targets[ :split, 0 ], targets[ :split, 1 ], fill_color=colors, fill_alpha=alpha, line_color=None )


p5 = figure( title="Ground truth input space", tools=TOOLS )
colors = [
    "#%02x%02x%02x" % (r, g, 150) for r, g in zip( np.floor( 255 * np.abs( features[ split:, 0 ] ) ), np.floor( 255 * np.abs( features[ split:, 1 ] ) ) )
]
p5.circle( features[ split:, 0 ], features[ split:, 1 ], fill_color=colors, fill_alpha=alpha, line_color=None )

p6 = figure( title="Ground truth output space", tools=TOOLS )
p6.circle( targets[ split:, 0 ], targets[ split:, 1 ], fill_color=colors, fill_alpha=alpha, line_color=None )

p7 = figure( title="Prediction output space", tools=TOOLS )
p7.circle( pred[ :, 0 ], pred[ :, 1 ], fill_color=colors, fill_alpha=alpha, line_color=None )


show( VBox( HBox( p1, p2 ), HBox( p5, p6, p7 ) ) )

In [97]: