PLearn 0.1
|
#include <RandomVar.h>
Public Member Functions | |
MultinomialRandomVariable (const RandomVar &log_probabilities) | |
the parameters are unnormalized log-probabilities associated to each of the possible values of the observation | |
const RandomVar & | log_probabilities () |
convenience inline | |
bool | learn_the_probabilities () |
virtual char * | classname () |
Var | logP (const Var &obs, const RVInstanceArray &RHS, RVInstanceArray *parameters_to_learn) |
void | setValueFromParentsValue () |
sampling algorithm | |
void | EMUpdate () |
void | EMBprop (const Vec obs, real posterior) |
void | EMEpochInitialize () |
Initialization of an individual EMEpoch. | |
bool | isDiscrete () |
most common default | |
Protected Attributes | |
Vec | sum_posteriors |
temporaries used for EM: |
Y is discrete and takes values 0 to N-1. This class represents P(Y=i) = p_i where p=softmax(log_probabilities) and log_probabilities is the unique parent (which can be learned with "EM" in one iteration).
Definition at line 1472 of file RandomVar.h.
PLearn::MultinomialRandomVariable::MultinomialRandomVariable | ( | const RandomVar & | log_probabilities | ) |
the parameters are unnormalized log-probabilities associated to each of the possible values of the observation
Definition at line 2235 of file RandomVar.cc.
:StochasticRandomVariable(log_probabilities,1), sum_posteriors(log_probabilities->length()) { }
virtual char* PLearn::MultinomialRandomVariable::classname | ( | ) | [inline, virtual] |
Implements PLearn::RandomVariable.
Definition at line 1483 of file RandomVar.h.
{ return "MultinomialRandomVariable"; }
************ EM STUFF ********** propagate posterior information to parents in order to perform an EMupdate at the end of an EMEpoch. In the case of mixture-like RVs and their components, the posterior is the probability of the component "this" given the observation "obs".
Implements PLearn::RandomVariable.
Definition at line 2261 of file RandomVar.cc.
References PLearn::TVec< T >::data(), learn_the_probabilities(), and sum_posteriors.
{ if (learn_the_probabilities()) { real *p = sum_posteriors.data(); p[(int)obs[0]] += posterior; } }
void PLearn::MultinomialRandomVariable::EMEpochInitialize | ( | ) | [virtual] |
Initialization of an individual EMEpoch.
the default just propagates to the unmarked parents
Reimplemented from PLearn::RandomVariable.
Definition at line 2253 of file RandomVar.cc.
References PLearn::TVec< T >::clear(), PLearn::RandomVariable::EMmark, learn_the_probabilities(), and sum_posteriors.
{ if (EMmark) return; RandomVariable::EMEpochInitialize(); if (learn_the_probabilities()) sum_posteriors.clear(); }
void PLearn::MultinomialRandomVariable::EMUpdate | ( | ) | [virtual] |
update the fixed (non-random) parameters using internal learning mechanism, at end of an EMEpoch. the default just propagates to the unmarked parents.
Reimplemented from PLearn::RandomVariable.
Definition at line 2270 of file RandomVar.cc.
References PLearn::apply(), PLearn::RandomVariable::EMmark, learn_the_probabilities(), log_probabilities(), PLearn::multiply(), PLearn::safeflog(), PLearn::sum(), sum_posteriors, and PLearn::RandomVariable::value.
{ if (EMmark) return; EMmark=true; if (learn_the_probabilities()) { real denom = sum(sum_posteriors); if (denom>0) // update probabilities { multiply(sum_posteriors,real(1.0/denom),sum_posteriors); apply(sum_posteriors,log_probabilities()->value->value,safeflog); } // maybe should WARN the user if denom==0 here } }
bool PLearn::MultinomialRandomVariable::isDiscrete | ( | ) | [virtual] |
most common default
Reimplemented from PLearn::StochasticRandomVariable.
Definition at line 2287 of file RandomVar.cc.
{ return true; }
bool PLearn::MultinomialRandomVariable::learn_the_probabilities | ( | ) | [inline] |
Definition at line 1481 of file RandomVar.h.
Referenced by EMBprop(), EMEpochInitialize(), and EMUpdate().
{ return learn_the_parameters[0]; }
const RandomVar& PLearn::MultinomialRandomVariable::log_probabilities | ( | ) | [inline] |
convenience inline
Definition at line 1480 of file RandomVar.h.
Referenced by EMUpdate(), logP(), and setValueFromParentsValue().
{ return parents[0]; }
Var PLearn::MultinomialRandomVariable::logP | ( | const Var & | obs, |
const RVInstanceArray & | RHS, | ||
RVInstanceArray * | parameters_to_learn | ||
) | [virtual] |
Construct a Var that computes logP(This = obs | RHS ). This function SHOULD NOT be used directly, but is called by the global function logP (same argument), which does proper massaging of the network before and after this call.
Implements PLearn::RandomVariable.
Definition at line 2241 of file RandomVar.cc.
References PLearn::RandomVariable::isMarked(), PLearn::log(), log_probabilities(), PLearn::marginalize(), PLearn::softmax(), and PLearn::RandomVariable::value.
{ if (log_probabilities()->isMarked()) return log(softmax(log_probabilities()->value))[obs]; // else // probably not feasible..., but try in case we know a trick return PLearn::logP(ConditionalExpression (RVInstance(marginalize(this,log_probabilities()),obs), RHS),true,parameters_to_learn); }
void PLearn::MultinomialRandomVariable::setValueFromParentsValue | ( | ) | [virtual] |
sampling algorithm
Implements PLearn::RandomVariable.
Definition at line 2292 of file RandomVar.cc.
References log_probabilities(), PLearn::softmax(), and PLearn::RandomVariable::value.
{ value = Var(new MultinomialSampleVariable(softmax(log_probabilities()->value))); }
Vec PLearn::MultinomialRandomVariable::sum_posteriors [protected] |
temporaries used for EM:
sum of posteriors over training data
Definition at line 1495 of file RandomVar.h.
Referenced by EMBprop(), EMEpochInitialize(), and EMUpdate().