In [1]:
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 [2]:
n = 10000
d = 2
D = 1000
p = 2
split = 0.75
In [3]:
#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
In [8]:
t = time.clock( )
features = dg.generate_inputs( n, d, 'sphere' )
model = mdl.Model( fm.DeepTransformableFF( 1.0 / d, d, D, P, A ) )
targets = dg.generate_outputs( features, model )
print( 'Generate time: ', time.clock( ) - t )
In [9]:
t = time.clock( )
split_n = 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_n, : ], targets[ :split_n, : ] )
pred = modelt( features[ split_n:, : ] )
print( 'Learn and predict time: ', time.clock( ) - t )
print 'Test error:', ( ( pred - targets[ split_n:, : ] ) ** 2 ).sum( ) / targets.size, model.sparsity
In [10]:
t = time.clock( )
split_n = split * n
modelt = mdl.Model( fm.DeepTransformableFF( 1.0 / d, d, D, np.array( [ np.eye( d ), np.eye( d ) ] ), np.eye( p ) ) )
risk = rsk.Lasso( 1e-10 )
l = lr.Constant( 1.0 )
optimizer = sgd.SGD( risk, l, 10, 5, 0 )
optimizer.fit( modelt, features[ :split_n, : ], targets[ :split_n, : ] )
predu = modelt( features[ split_n:, : ] )
print( 'Learn and predict time: ', time.clock( ) - t )
print 'Test error:', ( ( predu - targets[ split_n:, : ] ) ** 2 ).sum( ) / targets.size, model.sparsity
In [13]:
%%capture
TOOLS="pan,wheel_zoom,box_zoom,reset,hover,save"
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_n, 0 ] ) ), np.floor( 255 * np.abs( features[ :split_n, 1 ] ) ) )
]
p1.circle( features[ :split_n, 0 ], features[ :split_n, 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_n, 0 ], targets[ :split_n, 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_n:, 0 ] ) ), np.floor( 255 * np.abs( features[ split_n:, 1 ] ) ) )
]
p5.circle( features[ split_n:, 0 ], features[ split_n:, 1 ], fill_color=colors, fill_alpha=alpha, line_color=None )
p6 = figure( title="Ground truth output space", tools=TOOLS )
p6.circle( targets[ split_n:, 0 ], targets[ split_n:, 1 ], fill_color=colors, fill_alpha=alpha, line_color=None )
p7 = figure( title="Prediction output space (prior)", tools=TOOLS )
p7.circle( pred[ :, 0 ], pred[ :, 1 ], fill_color=colors, fill_alpha=alpha, line_color=None )
p8 = figure( title="Prediction output space (no prior)", tools=TOOLS )
p8.circle( predu[ :, 0 ], predu[ :, 1 ], fill_color=colors, fill_alpha=alpha, line_color=None )
show( VBox( HBox( p1, p2 ), HBox( p5, p6, p7, p8 ) ) )
In [7]:
n = 10000
d = 2
D = 1000
p = 2
split = 0.75
In [39]:
lbda = 1.0
A = lbda * np.eye( p ) + ( 1 - lbda ) * np.ones( ( p, p ) )
P = np.array( [ np.eye( d ), np.eye( d ) ] )
t = time.clock( )
features = dg.generate_inputs( n, d, 'sphere' )
model = mdl.Model( fm.DeepTransformableFF( 20.0 / d, d, D, P, A ) )
targets = dg.generate_outputs( features, model )
print( 'Generate time: ', time.clock( ) - t )
In [40]:
%%capture
TOOLS="pan,wheel_zoom,box_zoom,reset,hover,save"
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_n, 0 ] ) ), np.floor( 255 * np.abs( features[ :split_n, 1 ] ) ) )
]
p1.circle( features[ :split_n, 0 ], features[ :split_n, 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_n, 0 ], targets[ :split_n, 1 ], fill_color=colors, fill_alpha=alpha, line_color=None )
show( VBox( HBox( p1, p2 ) ) )
In [92]:
n = 10000
d = 2
D = 1000
p = 2
split = 0.75
In [93]:
lbda = 0.1
A = lbda * np.eye( p ) + ( 1 - lbda ) * np.ones( ( p, p ) )
P = np.array( [ [ [ 1.0, 0 ], [ 0, 0 ] ], [ [ 1.0, 0 ], [ 0, 0 ] ] ] )
t = time.clock( )
features = dg.generate_inputs( n, d, 'cube' )
model = mdl.Model( fm.DeepTransformableFF( 20.0 / d, d, D, P, A ) )
targets = dg.generate_outputs( features, model )
print( 'Generate time: ', time.clock( ) - t )
In [94]:
%%capture
TOOLS="pan,wheel_zoom,box_zoom,reset,hover,save"
alpha = 0.6
output_file( 'DataGeneration.html' )
p1 = figure( title="Multitask problem", tools=TOOLS )
p1.circle( features[ :split_n, 0 ], targets[ :split_n, 0 ], fill_alpha=alpha, line_color=None, fill_color='blue', legend='task 1' )
p1.circle( features[ :split_n, 0 ], targets[ :split_n, 1 ], fill_alpha=alpha, line_color=None, fill_color='red', legend='task 2' )
# p1.select( dict( type=HoverTool ) ).tooltips
# hover.tooltips = OrderedDict([
# ("(x,y)", "($x, $y)")
# ])
show( p1 )
In [91]:
In [91]:
In [ ]: