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 "OneHotVariable.h"
00044
00045
namespace PLearn {
00046
using namespace std;
00047
00048
00051
PLEARN_IMPLEMENT_OBJECT(OneHotVariable,
00052
"Represents a vector of a given lenth, that has value 1 at the index "
00053
"given by another variable and 0 everywhere else",
00054
"NO HELP");
00055
00056 OneHotVariable::OneHotVariable(
int thelength,
Variable* hotindex,
real the_coldvalue,
real the_hotvalue)
00057 :
inherited(hotindex,thelength,1), hotvalue(the_hotvalue), coldvalue(the_coldvalue), length_(thelength)
00058 {
00059
build_();
00060 }
00061
00062
void
00063 OneHotVariable::build()
00064 {
00065 inherited::build();
00066
build_();
00067 }
00068
00069
void
00070 OneHotVariable::build_()
00071 {
00072
00073
if (input && !input->isScalar())
00074
PLERROR(
"InterValuesVariable OneHotVariable(int thelength, Variable* hotindex, real the_coldvalue, real the_hotvalue) hotindex must be scalar as it is supposed to be an integer index");
00075 }
00076
00077
void
00078 OneHotVariable::declareOptions(
OptionList &ol)
00079 {
00080
declareOption(ol,
"hotvalue", &OneHotVariable::hotvalue, OptionBase::buildoption,
"");
00081
declareOption(ol,
"coldvalue", &OneHotVariable::coldvalue, OptionBase::buildoption,
"");
00082
declareOption(ol,
"length_", &OneHotVariable::length_, OptionBase::buildoption,
"");
00083 inherited::declareOptions(ol);
00084 }
00085
00086 void OneHotVariable::recomputeSize(
int& l,
int& w)
const
00087
{ l=
length_; w=1; }
00088
00089
00090 void OneHotVariable::fprop()
00091 {
00092
int index =
int(input->valuedata[0]);
00093
if (
nelems()==1)
00094 value[0] = index==0 ?
coldvalue :
hotvalue;
00095
else
00096 {
00097
for(
int i=0; i<
nelems(); i++)
00098 valuedata[i] =
coldvalue;
00099 value[index] =
hotvalue;
00100 }
00101 }
00102
00103
00104 void OneHotVariable::bprop() {}
00105
00106
00107 void OneHotVariable::symbolicBprop() {}
00108
00109
00110 void OneHotVariable::rfprop() {
00111
if (rValue.
length()==0)
resizeRValue();
00112 }
00113
00114
00115
00116 }
00117
00118