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 "ExtendedVariable.h"
00044
#include "SubMatVariable.h"
00045
00046
00047
namespace PLearn {
00048
using namespace std;
00049
00050
00053
PLEARN_IMPLEMENT_OBJECT(SubMatVariable,
00054
"ONE LINE DESCR",
00055
"NO HELP");
00056
00057 SubMatVariable::SubMatVariable(
Variable* v,
int i,
int j,
int the_length,
int the_width)
00058 :
inherited(v, the_length, the_width),
00059 startk(i*v->width()+j),
00060 length_(the_length),
00061 width_(the_width),
00062 i_(i),
00063 j_(j)
00064 {
00065
build_();
00066 }
00067
00068 void SubMatVariable::build()
00069 {
00070 inherited::build();
00071
build_();
00072 }
00073
00074 void SubMatVariable::build_()
00075 {
00076
if (input) {
00077
00078
if(i_ < 0 || i_ + length() > input->
length() || j_ < 0 || j_ + width() > input->
width())
00079
PLERROR(
"In SubMatVariable: requested sub-matrix is out of matrix bounds");
00080
startk =
i_ * input->
width() +
j_;
00081 }
00082 }
00083
00084
void
00085 SubMatVariable::declareOptions(
OptionList &ol)
00086 {
00087
declareOption(ol,
"length_", &SubMatVariable::length_, OptionBase::buildoption,
"");
00088
declareOption(ol,
"width_", &SubMatVariable::width_, OptionBase::buildoption,
"");
00089
declareOption(ol,
"startk", &SubMatVariable::startk, OptionBase::buildoption,
"");
00090
declareOption(ol,
"i_", &SubMatVariable::i_, OptionBase::buildoption,
"");
00091
declareOption(ol,
"j_", &SubMatVariable::j_, OptionBase::buildoption,
"");
00092 inherited::declareOptions(ol);
00093 }
00094
00095 void SubMatVariable::recomputeSize(
int& l,
int& w)
const
00096
{ l=
length_; w=
width_; }
00097
00098 void SubMatVariable::fprop()
00099 {
00100
if(
width()==input->
width())
00101 {
00102
real* inputdata = input->valuedata+
startk;
00103
for(
int k=0;
k<
nelems();
k++)
00104 valuedata[
k] = inputdata[
k];
00105 }
00106
else
00107 {
00108
real* inputrowdata = input->valuedata+
startk;
00109
int thisk=0;
00110
for(
int i=0; i<
length(); i++)
00111 {
00112
for(
int j=0; j<
width(); j++)
00113 valuedata[thisk++] = inputrowdata[j];
00114 inputrowdata += input->
width();
00115 }
00116 }
00117 }
00118
00119
00120 void SubMatVariable::bprop()
00121 {
00122
if(
width()==input->
width())
00123 {
00124
real* inputgradient = input->gradientdata+
startk;
00125
for(
int k=0;
k<
nelems();
k++)
00126 inputgradient[
k] += gradientdata[
k];
00127 }
00128
else
00129 {
00130
real* inputrowgradient = input->gradientdata+
startk;
00131
int thisk=0;
00132
for(
int i=0; i<
length(); i++)
00133 {
00134
for(
int j=0; j<
width(); j++)
00135 inputrowgradient[j] += gradientdata[thisk++];
00136 inputrowgradient += input->
width();
00137 }
00138 }
00139 }
00140
00141
00142 void SubMatVariable::bbprop()
00143 {
00144
if (input->diaghessian.
length()==0)
00145 input->resizeDiagHessian();
00146
if(
width()==input->
width())
00147 {
00148
real* inputdiaghessian = input->diaghessian.data()+
startk;
00149
for(
int k=0;
k<
nelems();
k++)
00150 inputdiaghessian[
k] += diaghessiandata[
k];
00151 }
00152
else
00153 {
00154
real* inputrowdiaghessian = input->diaghessiandata+
startk;
00155
int thisk=0;
00156
for(
int i=0; i<
length(); i++)
00157 {
00158
for(
int j=0; j<
width(); j++)
00159 inputrowdiaghessian[j] += diaghessiandata[thisk++];
00160 inputrowdiaghessian += input->
width();
00161 }
00162 }
00163 }
00164
00165
00166 void SubMatVariable::symbolicBprop()
00167 {
00168
int i =
startk/input->
width();
00169
int j =
startk%input->
width();
00170
int topextent = i;
00171
int bottomextent = input->
length()-(i+
length());
00172
int leftextent = j;
00173
int rightextent = input->
width()-(j+
width());
00174 input->accg(
extend(g,topextent,bottomextent,leftextent,rightextent));
00175 }
00176
00177
00178 void SubMatVariable::rfprop()
00179 {
00180
if (rValue.
length()==0)
resizeRValue();
00181
if(
width()==input->
width())
00182 {
00183
real* inputrdata = input->rvaluedata+
startk;
00184
for(
int k=0;
k<
nelems();
k++)
00185 rvaluedata[
k] = inputrdata[
k];
00186 }
00187
else
00188 {
00189
real* inputrowrdata = input->rvaluedata+
startk;
00190
int thisk=0;
00191
for(
int i=0; i<
length(); i++)
00192 {
00193
for(
int j=0; j<
width(); j++)
00194 rvaluedata[thisk++] = inputrowrdata[j];
00195 inputrowrdata += input->
width();
00196 }
00197 }
00198 }
00199
00200 }
00201