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 "OneHotSquaredLoss.h"
00044
#include "RowAtPositionVariable.h"
00045
#include "Var_operators.h"
00046
00047
00048
namespace PLearn {
00049
using namespace std;
00050
00051
00054
PLEARN_IMPLEMENT_OBJECT(OneHotSquaredLoss,
00055
"Computes sum(square_i(netout[i]-(i==classnum ?hotval :coldval))",
00056
"NO HELP");
00057
00058 OneHotSquaredLoss::OneHotSquaredLoss(
Variable* netout,
Variable* classnum,
real coldval,
real hotval)
00059 :
inherited(netout,classnum,1,1), coldval_(coldval), hotval_(hotval)
00060 {
00061
build_();
00062 }
00063
00064
void
00065 OneHotSquaredLoss::build()
00066 {
00067 inherited::build();
00068
build_();
00069 }
00070
00071
void
00072 OneHotSquaredLoss::build_()
00073 {
00074
00075
if(input2 && !input2->isScalar())
00076
PLERROR(
"In OneHotSquaredLoss: classnum must be a scalar variable representing an index of netout (typically a classnum)");
00077 }
00078
00079
void
00080 OneHotSquaredLoss::declareOptions(
OptionList &ol)
00081 {
00082
declareOption(ol,
"coldval_", &OneHotSquaredLoss::coldval_, OptionBase::buildoption,
"");
00083
declareOption(ol,
"hotval_", &OneHotSquaredLoss::hotval_, OptionBase::buildoption,
"");
00084 inherited::declareOptions(ol);
00085 }
00086
00087 void OneHotSquaredLoss::recomputeSize(
int& l,
int& w)
const
00088
{ l=1, w=1; }
00089
00090 void OneHotSquaredLoss::fprop()
00091 {
00092
real* netout = input1->valuedata;
00093
int n = input1->value.size();
00094
int classnum = (
int) input2->valuedata[0];
00095
real res = 0.;
00096
for(
int i=0; i<n; i++)
00097 res +=
square(*netout++ - (i==classnum ?
hotval_ :
coldval_));
00098 *valuedata = res;
00099 }
00100
00101
00102 void OneHotSquaredLoss::bprop()
00103 {
00104
real gr = *gradientdata;
00105
real* netout = input1->valuedata;
00106
int n = input1->value.size();
00107
int classnum = (
int) input2->valuedata[0];
00108
real* input1grptr = input1->gradientdata;
00109
if(gr!=1.)
00110 {
00111 gr = gr+gr;
00112
for(
int i=0; i<n; i++)
00113 *input1grptr++ += gr*(*netout++ - (i==classnum ?
hotval_ :
coldval_));
00114 }
00115
else
00116 {
00117
for(
int i=0; i<n; i++)
00118 input1->gradientdata[i] +=
two(netout[i] - (i==classnum ?
hotval_ :
coldval_));
00119 }
00120 }
00121
00122
00123 void OneHotSquaredLoss::symbolicBprop()
00124 {
00125
Var gi = g*(input1 -
coldval_);
00126
Var gindex =
new RowAtPositionVariable(g*(
coldval_-
hotval_), input2, input1->
length());
00127
Var ginput = gi + gindex;
00128 input1->accg(ginput+ginput);
00129 }
00130
00131
00132 void OneHotSquaredLoss::rfprop()
00133 {
00134
int n = input1->value.size();
00135
int classnum = (
int) input2->valuedata[0];
00136
real sum = 0;
00137
for (
int i=0; i<n; i++)
00138
sum += 2 * input1->rvaluedata[i] * (input1->valuedata[i] - (i==classnum ?
hotval_ :
coldval_));
00139 rvaluedata[0] =
sum;
00140 }
00141
00142
00143
00144 }
00145
00146