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 "UnaryHardSlopeVariable.h"
00044
#include "Var_operators.h"
00045
00046
namespace PLearn {
00047
using namespace std;
00048
00049
00052
PLEARN_IMPLEMENT_OBJECT(UnaryHardSlopeVariable,
00053
"Hard slope function whose Var input is only the argument of the function.",
00054
"Maps x (elementwise) to 0 if x<left, 1 if x>right, and linear in between otherwise.");
00055
00056 UnaryHardSlopeVariable::UnaryHardSlopeVariable(
Variable* input,
real l,
real r)
00057 :
inherited(input, input->length(), input->width()) ,
00058
left(l),
right(r), inv_slope(1.0/(r-l))
00059 {}
00060
00061
void
00062 UnaryHardSlopeVariable::build()
00063 {
00064 inherited::build();
00065
build_();
00066 }
00067
00068
void
00069 UnaryHardSlopeVariable::build_()
00070 {
00071
inv_slope = (1.0 / (
right - 1));
00072 }
00073
00074
void
00075 UnaryHardSlopeVariable::declareOptions(
OptionList &ol)
00076 {
00077
declareOption(ol,
"left", &UnaryHardSlopeVariable::left, OptionBase::buildoption,
"");
00078
declareOption(ol,
"right", &UnaryHardSlopeVariable::right, OptionBase::buildoption,
"");
00079 inherited::declareOptions(ol);
00080 }
00081
00082 void UnaryHardSlopeVariable::recomputeSize(
int& l,
int& w)
const
00083
{
00084
if (input) {
00085 l = input->
length();
00086 w = input->
width();
00087 }
else
00088 l = w = 0;
00089 }
00090
00091
00092 void UnaryHardSlopeVariable::fprop()
00093 {
00094
int l =
nelems();
00095
real* inputptr = input->valuedata;
00096
real* ptr = valuedata;
00097
for(
int i=0; i<l; i++)
00098 *ptr++ =
hard_slope(*inputptr++,
left,
right);
00099 }
00100
00101
00102 void UnaryHardSlopeVariable::bprop()
00103 {
00104
int l =
nelems();
00105
real* inputgradientptr = input->gradientdata;
00106
real* gradientptr = gradientdata;
00107
real* valueptr = valuedata;
00108
for(
int i=0; i<l; i++)
00109 {
00110
real x = *valueptr++;
00111
if (
x>
left)
00112
if (
x<
right)
00113 *inputgradientptr++ += *gradientptr++ *
inv_slope;
00114 }
00115 }
00116
00117
00118 }
00119
00120