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 "HardSlopeVariable.h"
00044
#include "Var_utils.h"
00045
00046
namespace PLearn {
00047
using namespace std;
00048
00049
00052
PLEARN_IMPLEMENT_OBJECT(HardSlopeVariable,
00053
"This Var computes the hard_slope function",
00054
"The hard_slope function is linear by parts function:\n"
00055
"0 in [-infty,left], linear in [left,right], and 1 in [right,infty], and continuous.\n"
00056
"If the arguments are vectors than the operation is performed element by element on all of them.\n");
00057
00058 HardSlopeVariable:: HardSlopeVariable(
Variable* x,
Variable* left,
Variable* right)
00059 :
inherited(
VarArray(
x,
left) &
Var(
right),
00060
x->length()<
left->length()?
left->length():
x->length(),
00061
x->width()<
left->width()?
left->width():
x->width())
00062 {}
00063
00064
00065 void HardSlopeVariable::recomputeSize(
int& l,
int& w)
const
00066
{
00067 l = w = 0;
00068
if (varray.
size() >= 3) {
00069
for (
int i = 0;i < 3; i++) {
00070
if (varray[i]->
length()>l)
00071 l = varray[i]->
length();
00072
if (varray[i]->width() > w)
00073 w = varray[i]->width();
00074 }
00075
for (
int i = 0;i < 3; i++) {
00076
if (varray[i]->
length() != l || varray[i]->width() != w) {
00077
if (varray[i]->
length() != 1 || varray[i]->width() != 1)
00078
PLERROR(
"Each argument of HardSlopeVariable should either have the same length/width as the others or length 1");
00079 }
00080 }
00081 }
00082 }
00083
00084
00085 void HardSlopeVariable::fprop()
00086 {
00087
int n=
nelems();
00088
int n1=varray[0]->
nelems();
00089
int n2=varray[1]->nelems();
00090
int n3=varray[2]->nelems();
00091
real*
x = varray[0]->valuedata;
00092
real*
left = varray[1]->valuedata;
00093
real*
right = varray[2]->valuedata;
00094
00095
if (n1==n && n2==n && n3==n)
00096
for(
int i=0; i<n; i++)
00097 valuedata[i] =
hard_slope(
x[i],
left[i],
right[i]);
00098
else if (n1==1 && n2==n && n3==n)
00099
for(
int i=0; i<n; i++)
00100 valuedata[i] =
hard_slope(*
x,
left[i],
right[i]);
00101
else
00102 {
00103
int m1= n1==1?0:1;
00104
int m2= n2==1?0:1;
00105
int m3= n3==1?0:1;
00106
for(
int i=0; i<n; i++,
x+=m1,
left+=m2,
right+=m3)
00107 valuedata[i] =
hard_slope(*
x, *
left, *
right);
00108 }
00109 }
00110
00111
00112 void HardSlopeVariable::bprop()
00113 {
00114
int n=
nelems();
00115
int n1=varray[0]->
nelems();
00116
int n2=varray[1]->nelems();
00117
int n3=varray[2]->nelems();
00118
int m1= n1==1?0:1;
00119
int m2= n2==1?0:1;
00120
int m3= n3==1?0:1;
00121
real*
x = varray[0]->valuedata;
00122
real*
left = varray[1]->valuedata;
00123
real*
right = varray[2]->valuedata;
00124
real* dx = varray[0]->gradientdata;
00125
real* dleft = varray[1]->gradientdata;
00126
real* dright = varray[2]->gradientdata;
00127
for(
int i=0; i<n; i++,
x+=m1,
left+=m2,
right+=m3,dx+=m1,dleft+=m2,dright+=m3)
00128 {
00129
real tleft = *
x - *
left;
00130
real tright = *
x - *
right;
00131
if (tright<=0 && tleft>=0)
00132 {
00133
real inv_delta=1.0/(*
right - *
left);
00134
real dll = tright*inv_delta;
00135
real drr = -tleft*inv_delta;
00136
real dxx = inv_delta;
00137 *dx += gradientdata[i] * dxx;
00138 *dleft += gradientdata[i] * dll;
00139 *dright += gradientdata[i] * drr;
00140 }
00141 }
00142 }
00143
00144 void HardSlopeVariable::symbolicBprop()
00145 {
00146
PLERROR(
"HardSlopeVariable::symbolicBprop() not implemented");
00147 }
00148
00149
00150
00151 }
00152
00153