PLearn 0.1
|
The first sentence should be a BRIEF DESCRIPTION of what the class does. More...
#include <NxProfileLearner.h>
Public Member Functions | |
NxProfileLearner () | |
matrix of user profiles (n_users*profile_dim) | |
virtual int | outputsize () const |
Returns the size of this learner's output, (which typically may depend on its inputsize(), targetsize() and set options). | |
virtual void | forget () |
(Re-)initializes the PLearner in its fresh state (that state may depend on the 'seed' option) and sets 'stage' back to 0 (this is the stage of a fresh learner!). | |
virtual void | train () |
The role of the train method is to bring the learner up to stage==nstages, updating the train_stats collector with training costs measured on-line in the process. | |
virtual void | computeOutput (const Vec &input, Vec &output) const |
Computes the output from the input. | |
virtual void | computeCostsFromOutputs (const Vec &input, const Vec &output, const Vec &target, Vec &costs) const |
Computes the costs from already computed output. | |
virtual TVec< std::string > | getTestCostNames () const |
Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method). | |
virtual TVec< std::string > | getTrainCostNames () const |
Returns the names of the objective costs that the train method computes and for which it updates the VecStatsCollector train_stats. | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual NxProfileLearner * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Finish building the object; just call inherited::build followed by build_() | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
Static Public Member Functions | |
static string | _classname_ () |
static OptionList & | _getOptionList_ () |
static RemoteMethodMap & | _getRemoteMethodMap_ () |
static Object * | _new_instance_for_typemap_ () |
static bool | _isa_ (const Object *o) |
static void | _static_initialize_ () |
static const PPath & | declaringFile () |
Public Attributes | |
int | profile_dim |
### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //! | |
real | slr |
dimension of the profiles to be learnt | |
real | dc |
start learning rate | |
real | L1_penalty_factor |
learning rate decrease constant | |
real | L2_penalty_factor |
PP< NatGradEstimator > | ngest_films |
*Natural gradient* For now we use 2 estimators: 1 for films and one for users. | |
PP< NatGradEstimator > | ngest_users |
Natural gradient estimator for the user parameters. | |
int | n_films |
int | n_users |
Mat | f_profiles |
Mat | u_profiles |
matrix of film profiles (n_films*profile_dim) | |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares the class options. | |
Private Types | |
typedef PLearner | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
The first sentence should be a BRIEF DESCRIPTION of what the class does.
Place the rest of the class programmer documentation here. Doxygen supports Javadoc-style comments. See http://www.doxygen.org/manual.html
Definition at line 58 of file NxProfileLearner.h.
typedef PLearner PLearn::NxProfileLearner::inherited [private] |
Reimplemented from PLearn::PLearner.
Definition at line 60 of file NxProfileLearner.h.
PLearn::NxProfileLearner::NxProfileLearner | ( | ) |
matrix of user profiles (n_users*profile_dim)
Default constructor
Definition at line 50 of file NxProfileLearner.cc.
References PLearn::PLearner::random_gen.
: profile_dim(1), slr(0.0), dc(0.0), L1_penalty_factor(0.0), L2_penalty_factor(0.0), n_films(17770), n_users(480189) /* ### Initialize all fields to their default value here */ { // ... // ### You may (or not) want to call build_() to finish building the object // ### (doing so assumes the parent classes' build_() have been called too // ### in the parent classes' constructors, something that you must ensure) // ### If this learner needs to generate random numbers, uncomment the // ### line below to enable the use of the inherited PRandom object. // random_gen = new PRandom(); if( !random_gen) random_gen = new PRandom; }
string PLearn::NxProfileLearner::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 48 of file NxProfileLearner.cc.
OptionList & PLearn::NxProfileLearner::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 48 of file NxProfileLearner.cc.
RemoteMethodMap & PLearn::NxProfileLearner::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 48 of file NxProfileLearner.cc.
Reimplemented from PLearn::PLearner.
Definition at line 48 of file NxProfileLearner.cc.
Object * PLearn::NxProfileLearner::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 48 of file NxProfileLearner.cc.
StaticInitializer NxProfileLearner::_static_initializer_ & PLearn::NxProfileLearner::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 48 of file NxProfileLearner.cc.
void PLearn::NxProfileLearner::build | ( | ) | [virtual] |
Finish building the object; just call inherited::build followed by build_()
Reimplemented from PLearn::PLearner.
Definition at line 156 of file NxProfileLearner.cc.
References PLearn::PLearner::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::NxProfileLearner::build_ | ( | ) | [private] |
This does the actual building.
matrix of film profiles (n_films*profile_dim)
matrix of user profiles (n_users*profile_dim)
Reimplemented from PLearn::PLearner.
Definition at line 121 of file NxProfileLearner.cc.
References PLearn::endl(), f_profiles, forget(), L1_penalty_factor, L2_penalty_factor, n_films, n_users, PLWARNING, profile_dim, PLearn::TMat< T >::resize(), slr, PLearn::PLearner::train_set, and u_profiles.
Referenced by build().
{ // ### This method should do the real building of the object, // ### according to set 'options', in *any* situation. // ### Typical situations include: // ### - Initial building of an object from a few user-specified options // ### - Building of a "reloaded" object: i.e. from the complete set of // ### all serialised options. // ### - Updating or "re-building" of an object after a few "tuning" // ### options have been modified. // ### You should assume that the parent class' build_() has already been // ### called. if( !train_set ) return; cout << "build()" << endl; if( L1_penalty_factor < 0. ) PLWARNING("NxProfileLearner::build:\n" "L1_penalty_factor is negative!\n"); if( L2_penalty_factor < 0. ) PLWARNING("NxProfileLearner::build:\n" "L2_penalty_factor is negative!\n"); if( (slr*L2_penalty_factor) > 1. ) PLWARNING("NxProfileLearner::build:\n" "slr = %f is too large for L2_penalty_factor!\n", slr); f_profiles.resize(n_films, profile_dim); u_profiles.resize(n_users, profile_dim); forget(); }
string PLearn::NxProfileLearner::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 48 of file NxProfileLearner.cc.
Referenced by train().
void PLearn::NxProfileLearner::computeCostsFromOutputs | ( | const Vec & | input, |
const Vec & | output, | ||
const Vec & | target, | ||
Vec & | costs | ||
) | const [virtual] |
Computes the costs from already computed output.
Implements PLearn::PLearner.
Definition at line 356 of file NxProfileLearner.cc.
{ real error = target[0] - output[0]; // the 16 is to put the error on the 1-5 rating basis costs[0] = 16.0 * error * error; //cout << " error " << error << " cost[0] " << costs[0] << endl; }
Computes the output from the input.
Reimplemented from PLearn::PLearner.
Definition at line 341 of file NxProfileLearner.cc.
References PLearn::dot(), f_profiles, n_films, n_users, outputsize(), PLASSERT, PLearn::TVec< T >::resize(), and u_profiles.
{ int nout = outputsize(); output.resize(nout); PLASSERT( (input[0]>=0) && (input[0]<n_films) && (input[1]>=0) && (input[1]<n_users) ); output[0] = dot( f_profiles((int)input[0]), u_profiles((int)input[1]) ); /* cout << " f " << filmProfileID << " " << f_profiles(filmProfileID) << endl; cout << " u " << userProfileID << " " << u_profiles(userProfileID) << endl; cout << "output[0] " << output[0];*/ }
void PLearn::NxProfileLearner::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares the class options.
Reimplemented from PLearn::PLearner.
Definition at line 72 of file NxProfileLearner.cc.
References PLearn::OptionBase::buildoption, dc, PLearn::declareOption(), PLearn::PLearner::declareOptions(), L1_penalty_factor, L2_penalty_factor, ngest_films, ngest_users, profile_dim, and slr.
{ // ### Declare all of this object's options here. // ### For the "flags" of each option, you should typically specify // ### one of OptionBase::buildoption, OptionBase::learntoption or // ### OptionBase::tuningoption. If you don't provide one of these three, // ### this option will be ignored when loading values from a script. // ### You can also combine flags, for example with OptionBase::nosave: // ### (OptionBase::buildoption | OptionBase::nosave) declareOption(ol, "profile_dim", &NxProfileLearner::profile_dim, OptionBase::buildoption, "Dimension of the profiles to learn."); declareOption(ol, "slr", &NxProfileLearner::slr, OptionBase::buildoption, "Starting learning rate."); declareOption(ol, "dc", &NxProfileLearner::dc, OptionBase::buildoption, "Learning rate decrease constant."); declareOption(ol, "L1_penalty_factor", &NxProfileLearner::L1_penalty_factor, OptionBase::buildoption, "Optional (default=0) factor of L1 regularization term, i.e.\n" "minimize L1_penalty_factor * sum_{ij} |weights(i,j)| during training.\n" "Gets multiplied by the learning rate."); declareOption(ol, "L2_penalty_factor", &NxProfileLearner::L2_penalty_factor, OptionBase::buildoption, "Optional (default=0) factor of L2 regularization term, i.e.\n" "minimize 0.5 * L2_penalty_factor * sum_{ij} weights(i,j)^2 during training.\n" "Gets multiplied by the learning rate."); declareOption(ol, "ngest_films", &NxProfileLearner::ngest_films, OptionBase::buildoption, "Optional NatGradEstimator object for the gradients on the parameters OF ALL USERS!\n" "NOT A TEMPLATE!"); declareOption(ol, "ngest_users", &NxProfileLearner::ngest_users, OptionBase::buildoption, "Optional NatGradEstimator object for the gradients on the parameters OF ALL FILMS!\n" "NOT A TEMPLATE!"); // Now call the parent class' declareOptions inherited::declareOptions(ol); }
static const PPath& PLearn::NxProfileLearner::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::PLearner.
Definition at line 171 of file NxProfileLearner.h.
:
//##### Protected Options ###############################################
NxProfileLearner * PLearn::NxProfileLearner::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::PLearner.
Definition at line 48 of file NxProfileLearner.cc.
void PLearn::NxProfileLearner::forget | ( | ) | [virtual] |
(Re-)initializes the PLearner in its fresh state (that state may depend on the 'seed' option) and sets 'stage' back to 0 (this is the stage of a fresh learner!).
(Re-)initialize the PLearner in its fresh state (that state may depend on the 'seed' option) and sets 'stage' back to 0 (this is the stage of a fresh learner!)
A typical forget() method should do the following:
Reimplemented from PLearn::PLearner.
Definition at line 184 of file NxProfileLearner.cc.
References PLearn::endl(), f_profiles, PLearn::PLearner::forget(), profile_dim, PLearn::PLearner::random_gen, PLearn::sqrt(), PLearn::PLearner::stage, and u_profiles.
Referenced by build_().
{ inherited::forget(); cout << "forget" << endl; //real delta = 1/sqrt(real(layer_sizes[i])); real delta = 1.0/sqrt(real(profile_dim));; random_gen->fill_random_uniform(u_profiles,-delta,delta); random_gen->fill_random_uniform(f_profiles,-delta,delta); stage = 0; }
OptionList & PLearn::NxProfileLearner::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 48 of file NxProfileLearner.cc.
OptionMap & PLearn::NxProfileLearner::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 48 of file NxProfileLearner.cc.
RemoteMethodMap & PLearn::NxProfileLearner::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 48 of file NxProfileLearner.cc.
TVec< string > PLearn::NxProfileLearner::getTestCostNames | ( | ) | const [virtual] |
Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).
Implements PLearn::PLearner.
Definition at line 365 of file NxProfileLearner.cc.
References getTrainCostNames().
{ // Return the names of the costs computed by computeCostsFromOutputs // (these may or may not be exactly the same as what's returned by // getTrainCostNames). return getTrainCostNames(); }
TVec< string > PLearn::NxProfileLearner::getTrainCostNames | ( | ) | const [virtual] |
Returns the names of the objective costs that the train method computes and for which it updates the VecStatsCollector train_stats.
Implements PLearn::PLearner.
Definition at line 373 of file NxProfileLearner.cc.
References PLearn::TVec< T >::resize().
Referenced by getTestCostNames().
{ // Return the names of the objective costs that the train method computes // and for which it updates the VecStatsCollector train_stats // (these may or may not be exactly the same as what's returned by // getTestCostNames). TVec<string> costs; costs.resize(1); costs[0]="MSE"; return costs; }
void PLearn::NxProfileLearner::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::PLearner.
Definition at line 163 of file NxProfileLearner.cc.
References PLearn::deepCopyField(), f_profiles, PLearn::PLearner::makeDeepCopyFromShallowCopy(), ngest_films, ngest_users, and u_profiles.
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(ngest_films, copies); deepCopyField(ngest_users, copies); deepCopyField(f_profiles, copies); deepCopyField(u_profiles, copies); // ### Remove this line when you have fully implemented this method. //PLERROR("NxProfileLearner::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); }
int PLearn::NxProfileLearner::outputsize | ( | ) | const [virtual] |
Returns the size of this learner's output, (which typically may depend on its inputsize(), targetsize() and set options).
Implements PLearn::PLearner.
Definition at line 177 of file NxProfileLearner.cc.
Referenced by computeOutput().
{ // Compute and return the size of this learner's output (which typically // may depend on its inputsize(), targetsize() and set options). return 1; }
void PLearn::NxProfileLearner::train | ( | ) | [virtual] |
The role of the train method is to bring the learner up to stage==nstages, updating the train_stats collector with training costs measured on-line in the process.
Implements PLearn::PLearner.
Definition at line 208 of file NxProfileLearner.cc.
References classname(), d, dc, PLearn::dot(), f_profiles, PLearn::VMat::getExample(), i, PLearn::PLearner::initTrain(), PLearn::PLearner::inputsize(), L1_penalty_factor, L2_penalty_factor, PLearn::VMat::length(), n_films, n_users, PLearn::PLearner::nstages, PLASSERT, profile_dim, PLearn::PLearner::report_progress, PLearn::TVec< T >::resize(), slr, PLearn::PLearner::stage, PLearn::PLearner::targetsize(), PLearn::PLearner::train_set, PLearn::PLearner::train_stats, and u_profiles.
{ // The role of the train method is to bring the learner up to // stage==nstages, updating train_stats with training costs measured // on-line in the process. static Vec input; // static so we don't reallocate memory each time... static Vec target; // (but be careful that static means shared!) input.resize(inputsize()); // the train_set's inputsize() target.resize(targetsize()); // the train_set's targetsize() real weight, error, lr; static Vec f_grad, f_natgrad; static Vec u_grad, u_natgrad; f_grad.resize(profile_dim); f_natgrad.resize(profile_dim); u_grad.resize(profile_dim); u_natgrad.resize(profile_dim); // indexes for natural gradient estimator calls // Since calls must have continuous 't's, and we exceed the int limit // we need to do some hacking static int ngf_idx=0; static int ngu_idx=0; // This generic PLearner method does a number of standard stuff useful for // (almost) any learner, and return 'false' if no training should take // place. See PLearner.h for more details. if (!initTrain()) return; int nsamples = train_set->length(); // clear statistics of previous epoch train_stats->forget(); while(stage<nstages) { PP<ProgressBar> pb; if( report_progress ) pb = new ProgressBar( "Training "+classname(), nsamples); // TODO In case minibarches are used, remember to modify in the case of // natural gradient updates. lr = slr/(1.0 + stage*dc); real L1_delta = lr * L1_penalty_factor; real L2_scaling = 1. - lr * L2_penalty_factor; for(int i=0; i<nsamples; i++) { train_set->getExample(i, input, target, weight); PLASSERT( (input[0]>=0) && (input[0]<n_films) && (input[1]>=0) && (input[1]<n_users) ); // Save a function call by not using the functions (computeOutput, // etc.). Also, we're using squared error cost, but dropping the 2 and taking // the negative already. error = target[0] - dot( f_profiles((int)input[0]), u_profiles((int)input[1]) ); // WHAT FOLLOWS SHOULD PROBABLY BE MADE MORE EFFICIENT // examples: // - for normal gradient multiply lr and error (2 scalars) // before multiplying the vector // - Consider operating on the vector's elements //- Use the updated film parameters in the user's update // the gradients f_grad = error * u_profiles((int)input[1]); u_grad = error * f_profiles((int)input[0]); /* // Update the parameters if( !ngest_films ) { f_profiles((int)input[0]) += lr * f_grad; } else { (*ngest_films)( ngf_idx, f_grad, f_natgrad ); // do index shananigans ngf_idx++; ngf_idx = ngf_idx%ngest_films->cov_minibatch_size + ngest_films->cov_minibatch_size; ngest_films->previous_t = ngf_idx-1; // perform parameter update f_profiles((int)input[0]) += lr * f_natgrad; } if( !ngest_users ) { u_profiles((int)input[1]) += lr * u_grad; } else { (*ngest_users)( ngu_idx, u_grad, u_natgrad ); // do index shananigans ngu_idx++; ngu_idx = ngu_idx%ngest_users->cov_minibatch_size + ngest_users->cov_minibatch_size; ngest_users->previous_t = ngu_idx-1; // perform parameter update u_profiles((int)input[1]) += lr * u_natgrad; } */ // L1 regularization if( L1_penalty_factor != 0. ) { for( int d=0; d<profile_dim; d++ ) { // films if( f_profiles((int)input[0], d) > L1_delta ) f_profiles((int)input[0], d) -= L1_delta; else if( f_profiles((int)input[0], d) < -L1_delta ) f_profiles((int)input[0], d) += L1_delta; else f_profiles((int)input[0], d) = 0.; // users if( u_profiles((int)input[1], d) > L1_delta ) u_profiles((int)input[1], d) -= L1_delta; else if( u_profiles((int)input[1], d) < -L1_delta ) u_profiles((int)input[1], d) += L1_delta; else u_profiles((int)input[1], d) = 0.; } } // L2 regularization if( L2_penalty_factor != 0. ) { f_profiles((int)input[0]) *= L2_scaling; u_profiles((int)input[1]) *= L2_scaling; } //train_stats->update(train_costs) if( pb ) pb->update(i); } ++stage; train_stats->finalize(); // finalize statistics for this epoch } }
Reimplemented from PLearn::PLearner.
Definition at line 171 of file NxProfileLearner.h.
start learning rate
Definition at line 70 of file NxProfileLearner.h.
Referenced by declareOptions(), and train().
Definition at line 98 of file NxProfileLearner.h.
Referenced by build_(), computeOutput(), forget(), makeDeepCopyFromShallowCopy(), and train().
learning rate decrease constant
Definition at line 72 of file NxProfileLearner.h.
Referenced by build_(), declareOptions(), and train().
Definition at line 73 of file NxProfileLearner.h.
Referenced by build_(), declareOptions(), and train().
Definition at line 90 of file NxProfileLearner.h.
Referenced by build_(), computeOutput(), and train().
Definition at line 91 of file NxProfileLearner.h.
Referenced by build_(), computeOutput(), and train().
*Natural gradient* For now we use 2 estimators: 1 for films and one for users.
We also treat each user/film gradient as if it were on the same parameters, ie, feed all user gradients to the same estimator. Natural gradient estimator for the film parameters. (if 0 then do not use natural gradient)
Definition at line 82 of file NxProfileLearner.h.
Referenced by declareOptions(), and makeDeepCopyFromShallowCopy().
Natural gradient estimator for the user parameters.
(if 0 then do not use natural gradient)
Definition at line 86 of file NxProfileLearner.h.
Referenced by declareOptions(), and makeDeepCopyFromShallowCopy().
### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //!
Definition at line 68 of file NxProfileLearner.h.
Referenced by build_(), declareOptions(), forget(), and train().
dimension of the profiles to be learnt
Definition at line 69 of file NxProfileLearner.h.
Referenced by build_(), declareOptions(), and train().
matrix of film profiles (n_films*profile_dim)
Definition at line 99 of file NxProfileLearner.h.
Referenced by build_(), computeOutput(), forget(), makeDeepCopyFromShallowCopy(), and train().