00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
#include "SemiSupervisedProbClassCostVariable.h"
00044
#include "Var_utils.h"
00045
00046
namespace PLearn {
00047
using namespace std;
00048
00049
00052
PLEARN_IMPLEMENT_OBJECT(SemiSupervisedProbClassCostVariable,
00053
"ONE LINE DESCR",
00054
"NO HELP");
00055
00056 SemiSupervisedProbClassCostVariable::SemiSupervisedProbClassCostVariable(
Var prob_,
Var target_,
Var prior_,
real ff)
00057 :
inherited(prob_ & target_ & (
VarArray)prior_,1,1), flatten_factor(ff)
00058 {
00059
build_();
00060 }
00061
00062
void
00063 SemiSupervisedProbClassCostVariable::build()
00064 {
00065 inherited::build();
00066
build_();
00067 }
00068
00069
void
00070 SemiSupervisedProbClassCostVariable::build_()
00071 {
00072
if (varray.
size() >= 3 && varray[0] && varray[1] && varray[2]) {
00073
00074
if (varray[2]->length()>0 && varray[0]->length() != varray[2]->length())
00075
PLERROR(
"In SemiSupervisedProbClassCostVariable: If prior.length()>0 then prior and prob must have the same size");
00076
if (!varray[1]->isScalar())
00077
PLERROR(
"In SemiSupervisedProbClassCostVariable: target must be a scalar");
00078
raised_prob.
resize(varray[0]->
length());
00079 }
00080
if (
flatten_factor <= 0)
00081
PLERROR(
"In SemiSupervisedProbClassCostVariable: flatten_factor must be positive, and even > 1 for normal use.");
00082 }
00083
00084
void
00085 SemiSupervisedProbClassCostVariable::declareOptions(
OptionList &ol)
00086 {
00087
declareOption(ol,
"flatten_factor", &SemiSupervisedProbClassCostVariable::flatten_factor, OptionBase::buildoption,
"");
00088 inherited::declareOptions(ol);
00089 }
00090
00091 void SemiSupervisedProbClassCostVariable::recomputeSize(
int& l,
int& w)
const
00092
{ l=1; w=1; }
00093
00094
00095 void SemiSupervisedProbClassCostVariable::fprop()
00096 {
00101
real target_value =
target()->valuedata[0];
00102
int n=
prob()->size();
00103
real* p=
prob()->valuedata;
00104
if (finite(target_value))
00105 {
00106
int t =
int(target_value);
00107
if (t<0 || t>=n)
00108
PLERROR(
"In SemiSupervisedProbClassCostVariable: target must be either missing or between 0 and %d incl.\n",
prob()->
size()-1);
00109 valuedata[0] = -
safeflog(p[t]);
00110 }
00111
else
00112 {
00113
sum_raised_prob=0;
00114
real* priorv =
prior()->valuedata;
00115
for (
int i=0;i<n;i++)
00116 {
00117
raised_prob[i] =
pow(priorv[i]*p[i],
flatten_factor);
00118
sum_raised_prob +=
raised_prob[i];
00119 }
00120 valuedata[0] = -
safeflog(
sum_raised_prob)/
flatten_factor;
00121 }
00122 }
00123
00124 void SemiSupervisedProbClassCostVariable::bprop()
00125 {
00126
real target_value =
target()->valuedata[0];
00127
int n=
prob()->size();
00128
real* dprob=
prob()->gradientdata;
00129
real* p=
prob()->valuedata;
00130
if (finite(target_value))
00131 {
00132
int t =
int(target_value);
00133
for (
int i=0;i<n;i++)
00134
if (i==t && p[t]>0)
00135 dprob[i] += -gradientdata[0]/p[t];
00136 }
00137
else
00138 {
00139
for (
int i=0;i<n;i++)
00140
if (p[i]>0)
00141 {
00142
real grad = - gradientdata[0]*
raised_prob[i]/(p[i]*
sum_raised_prob);
00143
if (finite(grad))
00144 dprob[i] += grad;
00145 }
00146 }
00147 }
00148
00149
00150 void SemiSupervisedProbClassCostVariable::symbolicBprop()
00151 {
00152
PLERROR(
"SemiSupervisedProbClassCostVariable::symbolicBprop() not implemented");
00153 }
00154
00155
00156 void SemiSupervisedProbClassCostVariable::rfprop()
00157 {
00158
PLERROR(
"SemiSupervisedProbClassCostVariable::rfprop() not implemented");
00159 }
00160
00161
00162
00163 }
00164
00165