When working with models, it is very common to get a few steps when making a prediction. For example a common scenario is: Normalize, feed through model, inverse normalization. The wrapper helper function can call such a chain of methods.
Below is an example of the above steps wrapped into a method call ev(...) (short for evaluate).
import numpy as np
import pypr.preprocessing as preproc
import pypr.ann as ann
import pypr.optimization as opt
from pypr.helpers.wrappers import *
from pypr.helpers.modelwithdata import *
D = np.loadtxt('data/shipfuel.csv.gz', skiprows=1, delimiter=',')
names = {'waterspeed':0, 'fuel':1, 'trim':2, 'windspeed':3, 'windangle':4,
'pitch':5, 'portrudder':6, 'starboardrudder':7, 'heeling':8,
'draft':9 }
targetCol = [1]; inputCol = [0, 2, 3, 4, 5, 6, 7, 8, 9]
T = D[:, targetCol]
X = D[:, inputCol]
# Normalize:
normX = preproc.Normalizer(X)
normT = preproc.Normalizer(T)
Xn = normX.transform(X)
Tn = normT.transform(T)
# Setup model:
nn = ann.WeightDecayANN([len(inputCol), 2, 1])
nn.v = 0.1 # Weight decay, just a guess, should actually be found
dm = ModelWithData(nn, Xn, Tn)
# Train model:
err = opt.minimize(dm.get_parameters(), dm.err_func, dm.err_func_d, 10)
# Wrap model for easy use:
ev = wrap_model(X, (normX.transform, nn.forward, normT.invtransform), **names)
print "ev(X) =", ev(X)
print "ev(waterspeed=20) =", ev(waterspeed=20)
print "ev(X, waterspeed=20) =", ev(X, waterspeed=20)
Wrap a chain of function calls. It is typical to call a series of functions to obtain a result from a model. For example a preprocessing function, a model call, and a post-processing function. X0 is a defalt input for the function chain.
Parameters : | X0 : np array
functions : list of functions
**indices : dictionary, optional
|
---|---|
Returns : | eval_wrap : function
|