In [1]:
from sklearn.datasets import fetch_olivetti_faces
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import train_test_split

import OVFM.Model as md
import OVFM.FeatureMap as fm
import OVFM.Risk as rsk
import OVFM.LearningRate as lr
import OVFM.DataGeneration as dg
import OVFM.SGD as sgd

import numpy as np
import scipy as sp
import time as time

import matplotlib.pyplot as plt
import matplotlib.cm as cm

from sklearn.ensemble import ExtraTreesRegressor
from sklearn.linear_model import RidgeCV

In [2]:
def gaussian( X, Y, mean_X, mean_Y, scale = 1 ):
    Xc = X - mean_X
    Yc = Y - mean_Y
    return np.pi ** 2 * np.exp( - scale / 2 * ( Xc ** 2 + Yc ** 2 )  ) / np.sqrt( scale )

def uniform_X( d, n ):
    return 5 * ( 2 * np.random.rand( d, n ) - 1 )

def mesh2array( X, Y ):
    arr = np.empty( ( X.size, 2 ) )
    arr[ :, 0 ] = X.ravel( )
    arr[ :, 1 ] = Y.ravel( )

    return arr

def array2mesh( arr, side = None ):
    if side == None:
        side = int( np.sqrt( arr.shape[ 0 ] ) )
    X = arr[ :, 0 ].reshape( ( side, side ) )
    Y = arr[ :, 1 ].reshape( ( side, side ) )

    return X, Y

def MSE( p, y ):
    return np.mean( ( p - y ) ** 2 )

def MSE_DIR( p, y ):
    return np.mean( ( ( p.T / np.linalg.norm( p, axis = 1 ) ).T - ( y.T / np.linalg.norm( y, axis = 1 ) ).T ) ** 2 )

def MSE_NORM( p, y ):
    return np.mean( ( np.linalg.norm( p, axis = 1 ) - np.linalg.norm( y, axis = 1 ) ) ** 2 )

def RMSE( p, y ):
    return np.sqrt( MSE( p, y ) )

def RMSE_DIR( p, y ):
    return np.sqrt( MSE_DIR( p, y ) )

def RMSE_NORM( p, y ):
    return np.sqrt( MSE_NORM( p, y ) )

In [12]:
# Generate data
d = 2
D = 1000
p = 2
x = np.arange( -1, 1, .002 )
y = np.arange( -1, 1, .002 )
X, Y = np.meshgrid( x, y )
loc = 25
space = 0.5
field = gaussian( X, Y, -space, 0, loc ) + gaussian( X, Y, space, 0, loc ) - gaussian( X, Y, 0, space, loc ) - gaussian( X, Y, 0, -space, loc )
V, U = np.gradient( field )

In [5]:
# Plot data
fig = plt.figure()
plt.streamplot( X, Y, U, V, color = np.sqrt( U ** 2 + V ** 2 ), linewidth = .5, cmap=plt.cm.jet, density = 10, arrowstyle=u'->' )
fig.savefig( 'stream.eps', format='eps' )

In [40]:
alpha = .5
fig = plt.figure()
US, VS = alpha * U + ( alpha - 1 ) * V, alpha * V + ( 1 - alpha ) * U
plt.streamplot( X, Y, US, VS, color = np.sqrt( U ** 2 + V ** 2 ), linewidth = .5, cmap=plt.cm.jet, density = 10, arrowstyle=u'->' )
fig.savefig( 'stream.eps', format='eps' )
# plt.show( )

In [15]:
x = mesh2array( X, Y )
y = mesh2array( U, V )

In [23]:
A = 2 * np.random.rand( p, p ) - 1
A = np.dot( A, A.T )
A = A / np.linalg.norm( A, 2 )
yn = y + 1. * np.std( y ) * np.random.multivariate_normal( np.zeros( p ), A, y.shape[ 0 ] )

X_train, X_test, y_train, y_test = train_test_split( x, yn, train_size = .5 )
print X_train.shape
# X_train = x[ x[ :, 0 ] < 4.8, : ]
# y_train = y[ x[ :, 0 ] < 4.8, : ]
# X_test = x[ x[ :, 0 ] >= 4.8, : ]
# y_test = y[ x[ :, 0 ] >= 4.8, : ]


(500000, 2)

In [18]:
scaler_X = StandardScaler( )
X_train_s = scaler_X.fit_transform( X_train )
X_test_s = scaler_X.transform( X_test )

scaler_y = StandardScaler( )
y_train_s = scaler_y.fit_transform( y_train )
y_test_s = scaler_y.transform( y_test )

In [24]:
A = np.cov( y_train.T )
A = A / np.linalg.norm( A )
# A = np.eye( p )
sigma = .1
gamma = 1. / ( 2 * sigma ** 2 )
model = md.Model( fm.DecomposableFF( gamma, d, 1000, A, eps_or_rank = 0 ) )

In [25]:
lbda1 = 0.00
lbda2 = 0.00
eps = 0.0
risk = rsk.ElasticNet( lbda1, lbda2, eps )
lb = lr.Constant( 0. ) # no bias
lc = lr.Constant( .1 )
opt = sgd.SGD( risk, 500000, lc, lb, 10, 0 )
model.reset( )
start = time.time( )
opt.fit( model, X_train.T, y_train.T )
stop = time.time( )

start_pred = time.clock( )
pred_train = model( X_train.T ).T
pred_test = model( X_test.T ).T
print lbda1, lbda2, RMSE( pred_train, y_train ), RMSE( pred_test, y_test ), stop - start
print lbda1, lbda2, RMSE_DIR( pred_train, y_train ), RMSE_DIR( pred_test, y_test ), stop - start
print lbda1, lbda2, RMSE_NORM( pred_train, y_train ), RMSE_NORM( pred_test, y_test ), stop - start
stop_pred = time.clock( )
print stop_pred - start_pred


0.0 0.0 0.0038596805109 0.00385170661455 316.920077085
0.0 0.0 0.664576351524 0.664432636181 316.920077085
0.0 0.0 0.00423241703249 0.00422600404062 316.920077085
40.23101

In [ ]:
A = np.eye( p )
# A = np.eye( p )
sigma = .1
gamma = 1. / ( 2 * sigma ** 2 )
model = md.Model( fm.DecomposableFF( gamma, d, 1000, A, eps_or_rank = 0 ) )

In [ ]:
lbda1 = 0.00
lbda2 = 0.00
eps = 0.0
risk = rsk.ElasticNet( lbda1, lbda2, eps )
lb = lr.Constant( 0. ) # no bias
lc = lr.Constant( .1 )
opt = sgd.SGD( risk, 900000, lc, lb, 10, 0 )
model.reset( )
start = time.time( )
opt.fit( model, X_train.T, y_train.T )
stop = time.time( )

start_pred = time.clock( )
pred_train = model( X_train.T ).T
pred_test = model( X_test.T ).T
print lbda1, lbda2, RMSE( pred_train, y_train ), RMSE( pred_test, y_test ), stop - start
print lbda1, lbda2, RMSE_DIR( pred_train, y_train ), RMSE_DIR( pred_test, y_test ), stop - start
print lbda1, lbda2, RMSE_NORM( pred_train, y_train ), RMSE_NORM( pred_test, y_test ), stop - start
stop_pred = time.clock( )
print stop_pred - start_pred

In [26]:
sigma = .1
gamma = 1. / ( 2 * sigma ** 2 )
model = md.Model( fm.CurlFreeFF( gamma, d, 1000 ) )

In [32]:
lbda1 = 0.01
lbda2 = 0.00
eps = 0.0
risk = rsk.ElasticNet( lbda1, lbda2, eps )
lb = lr.Constant( 0. ) # no bias
lc = lr.InvScaling( .1, lbda1, 1 )
opt = sgd.SGD( risk, 500000, lc, lb, 10, 0 )
model.reset( )
start = time.time( )
opt.fit( model, X_train.T, y_train.T )
stop = time.time( )

start_pred = time.clock( )
pred_train = model( X_train.T ).T
pred_test = model( X_test.T ).T
print lbda1, lbda2, RMSE( pred_train, y_train ), RMSE( pred_test, y_test ), stop - start
print lbda1, lbda2, RMSE_DIR( pred_train, y_train ), RMSE_DIR( pred_test, y_test ), stop - start
print lbda1, lbda2, RMSE_NORM( pred_train, y_train ), RMSE_NORM( pred_test, y_test ), stop - start
stop_pred = time.clock( )
print stop_pred - start_pred


0.01 0.0 0.00385459484033 0.00384633140676 347.991292
0.01 0.0 0.662776275088 0.663382345928 347.991292
0.01 0.0 0.00428668460874 0.00427994714536 347.991292
45.495225

In [ ]:
print y_test.shape

In [33]:
UT, VT = array2mesh( model( mesh2array( X, Y ).T ).T )

In [34]:
fig = plt.figure( )
plt.streamplot( X, Y, UT, VT, color = np.sqrt( UT ** 2 + VT ** 2 ), linewidth = .5, cmap=plt.cm.autumn, density = 10, arrowstyle=u'->' )
plt.show( )

In [ ]:
plt.imshow( np.sqrt( ( UT - U ) ** 2 + ( VT - V ) ** 2 ) )
plt.show( )

In [ ]:
d = 2
D = 1000
p = 2
x = np.arange( -1, 1, .01 )
y = np.arange( -1, 1, .01 )
X, Y = np.meshgrid( x, y )
loc = 40
space = 0.3
field = gaussian( X, Y, -space, 0, loc ) + gaussian( X, Y, space, 0, loc ) + gaussian( X, Y, 0, space, loc ) + gaussian( X, Y, 0, -space, loc )
V, U = np.gradient( field )
# plt.imshow( np.sqrt( ( U ) ** 2 + ( V ) ** 2 ) )
plt.imshow( field )
plt.show( )

In [ ]:
plt.imshow( np.sqrt( ( U ) ** 2 + ( V ) ** 2 ) )
plt.show( )

In [ ]:
print model.feature_map.kernel_approx( X_train[ 0:1, : ].T, X_train[ 0:1, : ].T )
print model.feature_map.kernel_exact( X_train[ 0:1, : ].T, X_train[ 0:1, : ].T )

In [ ]:
np.dot( model.feature_map._mapf( X_train[ 0:1, : ].T ).T, model.feature_map._mapf( X_train[ 1:2, : ].T ) )

In [ ]:
gamma = model.feature_map.gamma
x1 = X_train[ 0:1, : ].T
x2 = X_train[ 1:2, : ].T
feature = fm.GaussianFF( gamma, d, 1 )
kernel_scalar = feature.kernel_exact( x1, np.asarray( x2 ) )[ :, :, None, None ]
delta = np.subtract( x1[ :, None, : ], x2[ :, :, None ] )

In [ ]:
np.asarray( np.transpose( \
            2 * gamma * kernel_scalar \
            * ( np.eye( d )[ None, None, :, : ] - 2 * ( gamma * delta[ :, None, :, : ] * delta[ None, :, :, : ] ).transpose( ( 2, 3, 0, 1 ) ) ), \
            ( 0, 2, 1, 3 ) \
        ) ).reshape( ( d * x1.shape[ 1 ], d * x2.shape[ 1 ] ) )

In [ ]:
kernel_scalar

In [ ]:
x1 = X_train[ 0:1, : ].T
x2 = X_train[ 1:2, : ].T
np.dot( ( model.feature_map.Z * model.feature_map._mapf( x1 ) ).T, model.feature_map.Z * model.feature_map._mapf( x2 ) )

In [ ]:
np.dot( model.feature_map._mapf( x1 ).T, model.feature_map._mapf( x2 ) )

In [ ]:


In [ ]:


In [ ]:
from sklearn.ensemble import ExtraTreesRegressor

In [ ]:
est = ExtraTreesRegressor( n_estimators = 250 )

In [ ]:
start_pred = time.clock( )
est.fit( X_train, y_train )
stop_pred = time.clock( )
print stop_pred - start_pred

In [ ]:
print RMSE( est.predict( X_train ), y_train )
print RMSE( est.predict( X_test ), y_test )

In [41]:
print gamma


50.0

In [ ]: