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 "MatrixSumOfVariable.h"
00044
00045
namespace PLearn {
00046
using namespace std;
00047
00048
00051
PLEARN_IMPLEMENT_OBJECT(MatrixSumOfVariable,
00052
"ONE LINE DESCR",
00053
"NO HELP");
00054
00055 MatrixSumOfVariable::MatrixSumOfVariable(
VMat the_distr,
Func the_f,
int the_nsamples,
int the_input_size)
00056 :
inherited(
nonInputParentsOfPath(the_f->inputs,the_f->outputs), the_f->outputs[0]->length(), the_f->outputs[0]->width()),
00057 distr(the_distr), f(the_f), nsamples(the_nsamples), input_size(the_input_size)
00058
00059 {
00060
build_();
00061 }
00062
00063
void
00064 MatrixSumOfVariable::build()
00065 {
00066 inherited::build();
00067
build_();
00068 }
00069
00070
void
00071 MatrixSumOfVariable::build_()
00072 {
00073
curpos = 0;
00074
if (
f &&
distr) {
00075
if(
f->outputs.size()!=1)
00076
PLERROR(
"In MatrixSumOfVariable: function must have a single variable output (maybe you can vconcat the vars into a single one prior to calling sumOf, if this is really what you want)");
00077
00078
input_value.
resize(
distr->
width() *
nsamples);
00079
input_gradient.
resize(
distr->
width() *
nsamples);
00080
output_value.
resize(
f->outputs[0]->size());
00081
00082
if(
nsamples == -1)
00083
nsamples =
distr->
length();
00084
f->inputs.setDontBpropHere(
true);
00085 }
00086 }
00087
00088
void
00089 MatrixSumOfVariable::declareOptions(
OptionList &ol)
00090 {
00091
declareOption(ol,
"distr", &MatrixSumOfVariable::distr, OptionBase::buildoption,
"");
00092
declareOption(ol,
"f", &MatrixSumOfVariable::f, OptionBase::buildoption,
"");
00093
declareOption(ol,
"nsamples", &MatrixSumOfVariable::nsamples, OptionBase::buildoption,
"");
00094
declareOption(ol,
"input_size", &MatrixSumOfVariable::input_size, OptionBase::buildoption,
"");
00095 inherited::declareOptions(ol);
00096 }
00097
00098 void MatrixSumOfVariable::recomputeSize(
int& l,
int& w)
const
00099
{
00100
if (
f) {
00101 l =
f->outputs[0]->length();
00102 w =
f->outputs[0]->width();
00103 }
else
00104 l = w = 0;
00105 }
00106
00107
00108 void MatrixSumOfVariable::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies)
00109 {
00110 NaryVariable::makeDeepCopyFromShallowCopy(copies);
00111
deepCopyField(
distr, copies);
00112
deepCopyField(
f, copies);
00113 }
00114
00115
00116 void MatrixSumOfVariable::fprop()
00117 {
00118
Vec oneInput_value(
distr->
width());
00119
f->recomputeParents();
00120
00121
int inputpos=0;
00122
int targetpos=
nsamples*
input_size;
00123
for (
int i=0; i<
nsamples; i++)
00124 {
00125
distr->getRow(
curpos, oneInput_value);
00126
for (
int j=0; j<input_size; j++,inputpos++)
00127
input_value[inputpos]=oneInput_value[j];
00128
for (
int j=input_size; j<
distr.
width(); j++,targetpos++)
00129
input_value[targetpos] = oneInput_value[j];
00130
if(++
curpos==
distr.
length())
00131
curpos=0;
00132 }
00133
f->fprop(
input_value, value);
00134 }
00135
00136
00137 void MatrixSumOfVariable::bprop()
00138 {
fbprop(); }
00139
00140
00141 void MatrixSumOfVariable::fbprop()
00142 {
00143
Vec oneInput_value(
distr->
width());
00144
f->recomputeParents();
00145
00146
int inputpos=0;
00147
int targetpos=
nsamples*
input_size;
00148
for (
int i=0; i<
nsamples; i++)
00149 {
00150
distr->getRow(
curpos, oneInput_value);
00151
for (
int j=0; j<input_size; j++,inputpos++)
00152
input_value[inputpos]=oneInput_value[j];
00153
for (
int j=input_size; j<
distr.
width(); j++,targetpos++)
00154
input_value[targetpos] = oneInput_value[j];
00155
if(++
curpos==
distr.
length())
00156
curpos=0;
00157 }
00158
f->fbprop(
input_value, value,
input_gradient, gradient);
00159 }
00160
00161
00162 void MatrixSumOfVariable::symbolicBprop()
00163 {
00164
PLERROR(
"MatrixSumOfVariable::symbolicBprop not implemented.");
00165 }
00166
00167
00168 void MatrixSumOfVariable::rfprop()
00169 {
00170
PLERROR(
"MatrixSumOfVariable::rfprop not implemented.");
00171 }
00172
00173
00174 void MatrixSumOfVariable::printInfo(
bool print_gradient)
00175 {
00176
PLERROR(
"MatrixSumOfVariable::printInfo not implemented.");
00177
Vec input_value(
distr->
width());
00178
Vec input_gradient(
distr->
width());
00179
Vec output_value(
nelems());
00180
00181
f->recomputeParents();
00182 value.
clear();
00183
00184
for(
int i=0; i<
nsamples; i++)
00185 {
00186
distr->getRow(
curpos++,input_value);
00187
if (print_gradient)
00188
f->fbprop(input_value, output_value, input_gradient, gradient);
00189
else
00190
f->fprop(input_value, output_value);
00191 value += output_value;
00192
if(++
curpos>=
distr->
length())
00193
curpos = 0;
00194
f->fproppath.printInfo(print_gradient);
00195 }
00196 cout <<
info() <<
" : " <<
getName() <<
" = " << value;
00197
if (print_gradient) cout <<
" gradient=" << gradient;
00198 cout <<
endl;
00199 }
00200
00201
00202
00203 }
00204
00205