PLearn 0.1
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Static Protected Member Functions | Private Types | Private Member Functions | Private Attributes
PLearn::ManifoldParzen2 Class Reference

#include <ManifoldParzen2.h>

Inheritance diagram for PLearn::ManifoldParzen2:
Inheritance graph
[legend]
Collaboration diagram for PLearn::ManifoldParzen2:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ManifoldParzen2 ()
 ManifoldParzen2 (int the_nneighbors, int the_ncomponents, bool use_last_eigenvalue=true, real scale_factor=1)
virtual void build ()
 Simply calls inherited::build() then build_().
virtual void makeDeepCopyFromShallowCopy (CopiesMap &copies)
 Transforms a shallow copy into a deep copy.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual ManifoldParzen2deepCopy (CopiesMap &copies) const
virtual void train ()
 trains the model
virtual void computeOutput (const Vec &input, Vec &output) const
 Produce outputs according to what is specified in outputs_def.
virtual int outputsize () const
 Returned value depends on outputs_def.
real evaluate (const Vec x1, const Vec x2, real scale=1)
real evaluate_i_j (int i, int j, real scale=1)

Static Public Member Functions

static string _classname_ ()
 Declares name and deepCopy methods.
static OptionList_getOptionList_ ()
static RemoteMethodMap_getRemoteMethodMap_ ()
static Object_new_instance_for_typemap_ ()
static bool _isa_ (const Object *o)
static void _static_initialize_ ()
static const PPathdeclaringFile ()

Public Attributes

int nneighbors
 If you change one of these, you must retrain.
int ncomponents
 how many components do we want to remember from the PCA
bool use_last_eigenval
real scale_factor
real global_lambda0
bool learn_mu

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

static void declareOptions (OptionList &ol)
 Declares this class' options.

Private Types

typedef GaussMix inherited

Private Member Functions

int find_nearest_neighbor (VMat data, Vec x) const
void build_ ()
 This does the actual building.

Private Attributes

Vec eigenval_copy
Vec row
Vec t_row
VMat reference_set
TMat< realmu_temp
TVec< realtemp_eigv

Detailed Description

Definition at line 44 of file ManifoldParzen2.h.


Member Typedef Documentation

Reimplemented from PLearn::GaussMix.

Definition at line 49 of file ManifoldParzen2.h.


Constructor & Destructor Documentation

PLearn::ManifoldParzen2::ManifoldParzen2 ( )

Definition at line 60 of file ManifoldParzen2.cc.

References PLearn::PLearner::nstages, and PLearn::GaussMix::type.

    : nneighbors(4),
      ncomponents(1),
      use_last_eigenval(true),
      scale_factor(1),
      learn_mu(false)
{
    type = "general";
    nstages = 1;
}
PLearn::ManifoldParzen2::ManifoldParzen2 ( int  the_nneighbors,
int  the_ncomponents,
bool  use_last_eigenvalue = true,
real  scale_factor = 1 
)

Definition at line 71 of file ManifoldParzen2.cc.

References PLearn::PLearner::nstages, and PLearn::GaussMix::type.

    : nneighbors(the_nneighbors),ncomponents(the_ncomponents),use_last_eigenval(true),scale_factor(the_scale_factor)
{
    type = "general";
    nstages = 1;
}

Member Function Documentation

string PLearn::ManifoldParzen2::_classname_ ( ) [static]

Declares name and deepCopy methods.

Reimplemented from PLearn::GaussMix.

Definition at line 55 of file ManifoldParzen2.cc.

OptionList & PLearn::ManifoldParzen2::_getOptionList_ ( ) [static]

Reimplemented from PLearn::GaussMix.

Definition at line 55 of file ManifoldParzen2.cc.

RemoteMethodMap & PLearn::ManifoldParzen2::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::GaussMix.

Definition at line 55 of file ManifoldParzen2.cc.

bool PLearn::ManifoldParzen2::_isa_ ( const Object o) [static]

Reimplemented from PLearn::GaussMix.

Definition at line 55 of file ManifoldParzen2.cc.

Object * PLearn::ManifoldParzen2::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::GaussMix.

Definition at line 55 of file ManifoldParzen2.cc.

StaticInitializer ManifoldParzen2::_static_initializer_ & PLearn::ManifoldParzen2::_static_initialize_ ( ) [static]

Reimplemented from PLearn::GaussMix.

Definition at line 55 of file ManifoldParzen2.cc.

void PLearn::ManifoldParzen2::build ( ) [virtual]

Simply calls inherited::build() then build_().

Reimplemented from PLearn::GaussMix.

Definition at line 78 of file ManifoldParzen2.cc.

References PLearn::GaussMix::build(), and build_().

Referenced by train().

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::ManifoldParzen2::build_ ( ) [private]

This does the actual building.

Reimplemented from PLearn::GaussMix.

Definition at line 130 of file ManifoldParzen2.cc.

Referenced by build().

{}

Here is the caller graph for this function:

string PLearn::ManifoldParzen2::classname ( ) const [virtual]

Reimplemented from PLearn::GaussMix.

Definition at line 55 of file ManifoldParzen2.cc.

void PLearn::ManifoldParzen2::computeOutput ( const Vec input,
Vec output 
) const [virtual]

Produce outputs according to what is specified in outputs_def.

Reimplemented from PLearn::PDistribution.

Definition at line 371 of file ManifoldParzen2.cc.

References PLearn::GaussMix::center, PLearn::PDistribution::computeOutput(), PLearn::dot(), PLearn::GaussMix::eigenvectors, find_nearest_neighbor(), i, PLearn::TMat< T >::length(), PLearn::TVec< T >::length(), mu_temp, PLearn::PDistribution::outputs_def, PLearn::TMat< T >::resize(), PLearn::TVec< T >::resize(), row, PLearn::sign(), t_row, temp_eigv, PLearn::TVec< T >::toMat(), PLearn::tostring(), and PLearn::TMat< T >::width().

{
    switch(outputs_def[0])
    {
    case 'r':
    {
        int i, last_i=-1;
        int nstep = 100000;
        real step = 0.001;
        int save_every = 100;
        string fsave = "";
        string musave = "";
        VMat temp;
        t_row.resize(input.length());
        row.resize(input.length());
        t_row << input;
        mu_temp.resize(center.length(),center.width());
        temp_eigv.resize(input.length());
        mu_temp << center;
        for(int s=0; s<nstep;s++)
        {
            i = find_nearest_neighbor(new MemoryVMatrix(mu_temp),t_row);

            if(s % save_every == 0)
            {
                fsave = "mp_walk_" + tostring(s) + ".amat";
                temp = new MemoryVMatrix(t_row.toMat(1,t_row.length()));
                temp->saveAMAT(fsave,false,true);

                musave = "mp_mu_" + tostring(s) + ".amat";
                temp = new MemoryVMatrix(mu_temp(i).toMat(1,mu_temp(i).length()));
                temp->saveAMAT(musave,false,true);

            }
            temp_eigv << eigenvectors[i](0);
            real sign = (last_i == -1 || dot(eigenvectors[i](0),eigenvectors[last_i](0)) >= 0 ? 1 : -1);
            t_row += step*sign*temp_eigv ;
            last_i = i;
        }
        output << t_row;
        break;
    }
    default:
        inherited::computeOutput(input,output);
    }
}

Here is the call graph for this function:

void PLearn::ManifoldParzen2::declareOptions ( OptionList ol) [static, protected]

Declares this class' options.

Reimplemented from PLearn::GaussMix.

Definition at line 87 of file ManifoldParzen2.cc.

References PLearn::GaussMix::alpha_min, PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::GaussMix::declareOptions(), global_lambda0, PLearn::GaussMix::kmeans_iterations, PLearn::GaussMix::L, learn_mu, PLearn::OptionBase::learntoption, PLearn::GaussMix::n_eigen, ncomponents, nneighbors, PLearn::OptionBase::nosave, PLearn::redeclareOption(), scale_factor, PLearn::GaussMix::type, and use_last_eigenval.

{
    declareOption(ol,"nneighbors", &ManifoldParzen2::nneighbors, OptionBase::buildoption,
                  "Number of neighbors for covariance matrix estimation.");

    declareOption(ol,"ncomponents", &ManifoldParzen2::ncomponents, OptionBase::buildoption,
                  "Number of components to store from the PCA.");

    declareOption(ol,"use_last_eigenval", &ManifoldParzen2::use_last_eigenval, OptionBase::buildoption,
                  "Indication that the last eigenvalue should be used for the remaining directions' variance.");

    declareOption(ol,"learn_mu", &ManifoldParzen2::learn_mu, OptionBase::buildoption,
                  "Indication that the difference vector between the training points and the gaussians should be learned.\n"
                  "By default, the gaussians are centered at the training points.");

    declareOption(ol,"global_lambda0", &ManifoldParzen2::global_lambda0, OptionBase::buildoption,
                  "If use_last_eigenvalue is false, used value for the minimum variance in all directions");

    declareOption(ol,"scale_factor", &ManifoldParzen2::scale_factor, OptionBase::buildoption,
                  "Scale factor");

    // Now call the parent class' declareOptions
    inherited::declareOptions(ol);

    // Redeclare some parent's options.

    redeclareOption(ol,"L", &ManifoldParzen2::L, OptionBase::learntoption,
                    "Automatically set (to train_set->length()).");

    redeclareOption(ol,"n_eigen", &ManifoldParzen2::n_eigen, OptionBase::learntoption,
                    "Automatically set during training.");

    redeclareOption(ol,"type", &ManifoldParzen2::type, OptionBase::nosave,
                    "Automatically set (to 'general').");

    redeclareOption(ol,"alpha_min", &ManifoldParzen2::alpha_min, OptionBase::nosave,
                    "Not used.");

    redeclareOption(ol,"kmeans_iterations", &ManifoldParzen2::kmeans_iterations, OptionBase::nosave,
                    "Not used.");

}

Here is the call graph for this function:

static const PPath& PLearn::ManifoldParzen2::declaringFile ( ) [inline, static]

Reimplemented from PLearn::GaussMix.

Definition at line 117 of file ManifoldParzen2.h.

ManifoldParzen2 * PLearn::ManifoldParzen2::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::GaussMix.

Definition at line 55 of file ManifoldParzen2.cc.

real PLearn::ManifoldParzen2::evaluate ( const Vec  x1,
const Vec  x2,
real  scale = 1 
)

Definition at line 307 of file ManifoldParzen2.cc.

References PLearn::GaussMix::computeLogLikelihood(), eigenval_copy, PLearn::GaussMix::eigenvalues, PLearn::exp(), PLearn::fast_exact_is_equal(), find_nearest_neighbor(), i, and PLearn::PLearner::train_set.

{
    real ret;
    int i = find_nearest_neighbor(train_set,x2);

    if(fast_exact_is_equal(scale, 1))
        ret = computeLogLikelihood(x1,i);
    else
    {
        eigenval_copy << eigenvalues(i);
        eigenvalues(i) *= scale;

        // Maybe sigma_min should be adjusted!

        ret = computeLogLikelihood(x1,i);

        eigenvalues(i) << eigenval_copy;
    }
    return exp(ret);

    /*
      row = x1 - mu(i);
      ret = 0.5 * scale * pownorm(row)/eigenvalues(i,n_eigen_computed - 1);
      for (int k = 0; k < n_eigen_computed - 1; k++) {
      ret += 0.5 * scale * (1.0 / eigenvalues(i,k) - 1.0/eigenvalues(i,n_eigen_computed-1)) * square(dot(eigenvectors[i](k), row));
      }
      return ret;
    */
}

Here is the call graph for this function:

real PLearn::ManifoldParzen2::evaluate_i_j ( int  i,
int  j,
real  scale = 1 
)

Definition at line 337 of file ManifoldParzen2.cc.

References PLearn::GaussMix::center, PLearn::GaussMix::computeLogLikelihood(), eigenval_copy, PLearn::GaussMix::eigenvalues, PLearn::exp(), and PLearn::fast_exact_is_equal().

{
    real ret;

    if(fast_exact_is_equal(scale, 1))
        ret = computeLogLikelihood(center(i),j);
    else
    {
        eigenval_copy << eigenvalues(j);
        eigenvalues(j) *= scale;

        // Maybe sigma_min should be adjusted!

        ret = computeLogLikelihood(center(i),j);

        eigenvalues(j) << eigenval_copy;
    }
    return exp(ret);

    /*
      row = mu(i) - mu(j);
      ret = scale * pownorm(row)/eigenvalues(j,n_eigen_computed - 1);
      for (int k = 0; k < n_eigen_computed - 1; k++) {
      ret += scale * (1.0 / eigenvalues(j,k) - 1.0/eigenvalues(j,n_eigen_computed-1)) * square(dot(eigenvectors[j](k), row));
      }
      return ret;
    */
}

Here is the call graph for this function:

int PLearn::ManifoldParzen2::find_nearest_neighbor ( VMat  data,
Vec  x 
) const [private]

Definition at line 289 of file ManifoldParzen2.cc.

References PLearn::dist(), PLearn::distance(), i, PLearn::is_missing(), PLearn::VMat::length(), MISSING_VALUE, and row.

Referenced by computeOutput(), and evaluate().

{
    int ret = -1;
    real distance = MISSING_VALUE;
    real temp;
    for(int i=0; i<data->length(); i++)
    {
        data->getRow(i,row);
        temp = dist(row,x,2);
        if(is_missing(distance) || temp<distance)
        {
            distance = temp;
            ret = i;
        }
    }
    return ret;
}

Here is the call graph for this function:

Here is the caller graph for this function:

OptionList & PLearn::ManifoldParzen2::getOptionList ( ) const [virtual]

Reimplemented from PLearn::GaussMix.

Definition at line 55 of file ManifoldParzen2.cc.

OptionMap & PLearn::ManifoldParzen2::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::GaussMix.

Definition at line 55 of file ManifoldParzen2.cc.

RemoteMethodMap & PLearn::ManifoldParzen2::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::GaussMix.

Definition at line 55 of file ManifoldParzen2.cc.

void PLearn::ManifoldParzen2::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::GaussMix.

Definition at line 133 of file ManifoldParzen2.cc.

References PLearn::GaussMix::makeDeepCopyFromShallowCopy().

{
    inherited::makeDeepCopyFromShallowCopy(copies);

    // ### Call deepCopyField on all "pointer-like" fields
    // ### that you wish to be deepCopied rather than
    // ### shallow-copied.
    // ### ex:
    // deepCopyField(trainvec, copies);

    // ### Remove this line when you have fully implemented this method.
    //PLERROR("ManifoldParzen2::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
}

Here is the call graph for this function:

int PLearn::ManifoldParzen2::outputsize ( ) const [virtual]

Returned value depends on outputs_def.

Reimplemented from PLearn::GaussMix.

Definition at line 421 of file ManifoldParzen2.cc.

References PLearn::GaussMix::eigenvectors, PLearn::PDistribution::outputs_def, and PLearn::GaussMix::outputsize().

{
    switch(outputs_def[0])
    {
    case 'r':
        return eigenvectors[0].width();
    default:
        return inherited::outputsize();
    }
}

Here is the call graph for this function:

void PLearn::ManifoldParzen2::train ( ) [virtual]

trains the model

Reimplemented from PLearn::GaussMix.

Definition at line 209 of file ManifoldParzen2.cc.

References PLearn::GaussMix::alpha, build(), PLearn::GaussMix::center, PLearn::computeLocalPrincipalComponents(), eigenval_copy, PLearn::GaussMix::eigenvalues, PLearn::GaussMix::eigenvectors, PLearn::fast_exact_is_equal(), global_lambda0, i, PLearn::GaussMix::L, learn_mu, PLearn::TVec< T >::length(), PLearn::VMat::length(), PLearn::min(), PLearn::GaussMix::n_eigen, PLearn::GaussMix::n_eigen_computed, ncomponents, nneighbors, PLERROR, PLearn::TVec< T >::resize(), PLearn::GaussMix::resizeDataBeforeTraining(), row, scale_factor, PLearn::PLearner::stage, PLearn::PLearner::train_set, use_last_eigenval, w, and PLearn::VMat::width().

{
    Mat trainset(train_set);
    int l = train_set.length();
    int w = train_set.width();
    eigenval_copy.resize(ncomponents+1);
    row.resize(w);

    L = l;
    // D = ncomponents;  // This appears to cause a bug, D is expected by GaussMix to be the total input dimensionality, which may be different from ncomponents (Pascal V.)
    n_eigen = ncomponents;
    GaussMix::build(); // TODO Still needed?
    resizeDataBeforeTraining();
    // setMixtureTypeGeneral(l, ncomponents, w); // TODO Remove this line when it works.

    // storage for neighbors
    Mat delta_neighbors(nneighbors, w);
    Vec eigvals(ncomponents+1);
    Mat components_eigenvecs(ncomponents+1,w);
    for(int i=0; i<l; i++)
    {
        if(i%100==0)
            cerr << "[SEQUENTIAL TRAIN: processing pattern #" << i << "/" << l << "]\n";

        // center is sample
        Vec center_ = center(i);

        if(!learn_mu)
            center_ << trainset(i);

        if(use_last_eigenval)
            computeLocalPrincipalComponents(trainset, i, delta_neighbors,
                    eigvals, components_eigenvecs, center_, learn_mu);
        else
            computeLocalPrincipalComponents(trainset, i, delta_neighbors,
                    eigvals, components_eigenvecs, center_, learn_mu, global_lambda0);

        eigvals *= scale_factor;

//    cout<<delta_neighbors<<endl;

        /* What is this d all about??? (-- Pascal V.)
        real d=0;
        for(int k=0;k<delta_neighbors.length();k++)
            d+=dist(delta_neighbors(k),Vec(D,0.0),2);
        d/=delta_neighbors.length();
        */

        // find out eigenvalue (a.k.a lambda0) that will be used for all inputsize_-K directions
        real lambda0;
        if(use_last_eigenval)
        {
            // take last (smallest) eigenvalue as a variance in the non-principal directions
            // (but if it is 0 because of linear dependencies in the data, take the
            // last, i.e. smallest, non-zero eigenvalue).
            int last=min(ncomponents,eigvals.length()-1);
            lambda0 = eigvals[last];
            while (fast_exact_is_equal(lambda0, 0) && last>0)
                lambda0 = eigvals[--last];
            // the sigma-square for all remaining dimensions
            if (fast_exact_is_equal(lambda0, 0))
                PLERROR("All (%i) principal components have zero variance!?",eigvals.length());
        }
        else 
            lambda0 = global_lambda0;

        alpha[i] = 1.0 / l;
        n_eigen = eigvals.length() - 1;
        //GaussMix::build();
        //resizeStuffBeforeTraining();
        eigenvalues(i) << eigvals;
        eigenvalues(i, n_eigen_computed - 1) = lambda0; 
        eigenvectors[i] << components_eigenvecs;
//    setGaussianGeneral(i, 1.0/l, center, eigvals.subVec(0,eigvals.length()-1), components_eigenvecs.subMatRows(0,eigvals.length()-1), lambda0);
    }
    stage = 1;
    // precomputeStuff(); TODO Put back?
    build();
}

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::GaussMix.

Definition at line 117 of file ManifoldParzen2.h.

Definition at line 51 of file ManifoldParzen2.h.

Referenced by evaluate(), evaluate_i_j(), and train().

Definition at line 75 of file ManifoldParzen2.h.

Referenced by declareOptions(), and train().

Definition at line 76 of file ManifoldParzen2.h.

Referenced by declareOptions(), and train().

Definition at line 55 of file ManifoldParzen2.h.

Referenced by computeOutput().

how many components do we want to remember from the PCA

Definition at line 71 of file ManifoldParzen2.h.

Referenced by declareOptions(), and train().

If you change one of these, you must retrain.

how many neighbors should we consider

Definition at line 70 of file ManifoldParzen2.h.

Referenced by declareOptions(), and train().

Definition at line 54 of file ManifoldParzen2.h.

Vec PLearn::ManifoldParzen2::row [mutable, private]

Definition at line 52 of file ManifoldParzen2.h.

Referenced by computeOutput(), find_nearest_neighbor(), and train().

Definition at line 74 of file ManifoldParzen2.h.

Referenced by declareOptions(), and train().

Vec PLearn::ManifoldParzen2::t_row [mutable, private]

Definition at line 53 of file ManifoldParzen2.h.

Referenced by computeOutput().

Definition at line 56 of file ManifoldParzen2.h.

Referenced by computeOutput().

Definition at line 73 of file ManifoldParzen2.h.

Referenced by declareOptions(), and train().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines