00001
00040
00041
00042 #include <SharkDefs.h>
00043 #include <ReClaM/MSEFFNet.h>
00044 #include <sstream>
00045
00046
00047 using namespace std;
00048
00049
00067 void MSEFFNet::modelDerivative(const Array<double> &input, Array<double> &output, Array<double>& derivative)
00068 {
00069 throw SHARKEXCEPTION("modelDerivative(..) not implemented in MSEFFNet");
00070 }
00071
00072
00073
00090 void MSEFFNet::modelDerivative(const Array<double> &input, Array<double>& derivative)
00091 {
00092 throw SHARKEXCEPTION("modelDerivative(...) not implemented in MSEFFNet");
00093 }
00094
00095
00096
00130 double MSEFFNet::error(Model& model, const Array<double> &input, const Array<double> &target)
00131 {
00132 double se = 0;
00133 if (input.ndim() == 1)
00134 {
00135 Array<double> output(target.dim(0));
00136 model.model(input, output);
00137 for (unsigned c = 0; c < target.dim(0); c++)
00138 {
00139 se += (target(c) - output(c)) * (target(c) - output(c));
00140 }
00141 }
00142 else
00143 {
00144 Array<double> output(target.dim(1));
00145 for (unsigned pattern = 0; pattern < input.dim(0); ++pattern)
00146 {
00147 model.model(input[pattern], output);
00148 for (unsigned c = 0; c < target.dim(1); c++)
00149 {
00150 se += (target(pattern, c) - output(c)) * (target(pattern, c) - output(c));
00151 }
00152 }
00153 }
00154
00155 se /= (double) target.nelem();
00156 return se;
00157 }
00158
00159
00189 double MSEFFNet::errorDerivative(Model& model, const Array<double> &input, const Array<double> &target, Array<double>& derivative)
00190 {
00191 unsigned i, j;
00192 unsigned c;
00193 unsigned pos;
00194 double sum;
00195 double se = 0.;
00196
00197 derivative = 0;
00198 derivative.resize(model.getParameterDimension(), false);
00199
00200 readParameters();
00201
00202 if (input.ndim() == 1)
00203 {
00204
00205 activate(input);
00206
00207 for (i = firstOutputNeuron, c = 0 ; i < numberOfNeurons; i++, c++)
00208 {
00209 d(i) = 2 * (target(c) - z[i]) * dgOutput(z[i]);
00210 se += (target(c) - z[i]) * (target(c) - z[i]);
00211 }
00212
00213
00214 for (j = firstOutputNeuron - 1; j >= inputDimension; j--)
00215 {
00216 sum = 0;
00217 for (i = j + 1; i < numberOfNeurons; i++)
00218 {
00219 if (connection(i, j)) sum += weight(i, j) * d(i);
00220 }
00221 d(j) = dg(z[j]) * sum;
00222 }
00223
00224
00225 pos = 0;
00226 for (i = inputDimension; i < numberOfNeurons; i++)
00227 {
00228 for (j = 0; j < i; j++)
00229 {
00230 if (connection(i, j))
00231 {
00232 derivative(pos) -= d(i) * z[j];
00233 pos++;
00234 }
00235 }
00236 if (connection(i, bias))
00237 {
00238 derivative(pos) -= d(i);
00239 pos++;
00240 }
00241 }
00242 }
00243 else
00244 {
00245 for (unsigned pattern = 0; pattern < input.dim(0); ++pattern)
00246 {
00247
00248 activate(input[pattern]);
00249
00250 for (i = firstOutputNeuron, c = 0 ; i < numberOfNeurons; i++, c++)
00251 {
00252 d(i) = 2 * (target(pattern, c) - z[i]) * dgOutput(z[i]);
00253 }
00254
00255
00256 for (j = firstOutputNeuron - 1; j >= inputDimension; j--)
00257 {
00258 sum = 0;
00259 for (i = j + 1; i < numberOfNeurons; i++)
00260 {
00261 if (connection(i, j)) sum += weight(i, j) * d(i);
00262 }
00263 d(j) = dg(z[j]) * sum;
00264 }
00265
00266
00267 pos = 0;
00268 for (i = inputDimension; i < numberOfNeurons; i++)
00269 {
00270 for (j = 0; j < i; j++)
00271 {
00272 if (connection(i, j))
00273 {
00274 derivative(pos) -= d(i) * z[j];
00275 pos++;
00276 }
00277 }
00278 if (connection(i, bias))
00279 {
00280 derivative(pos) -= d(i);
00281 pos++;
00282 }
00283 }
00284 }
00285 }
00286 derivative /= (double) target.nelem();
00287
00288 se /= (double) target.nelem();
00289
00290 return se;
00291 }
00292
00293
00294
00318 MSEFFNet::MSEFFNet(const unsigned in, const unsigned out) : FFNet(in, out)
00319 {}
00320
00321
00322
00351 MSEFFNet::MSEFFNet(const unsigned in, unsigned out,
00352 const Array<int>& cmat) : FFNet(in, out, cmat)
00353 {
00354 d.resize(numberOfNeurons);
00355 }
00356
00357
00385 MSEFFNet::MSEFFNet(const unsigned in, unsigned out,
00386 const Array<int>& cmat, const Array<double>& wmat) :
00387 FFNet(in, out, cmat, wmat)
00388 {
00389 d.resize(numberOfNeurons, false);
00390 }
00391
00392
00393
00446 MSEFFNet::MSEFFNet(const string &filename) : FFNet(filename)
00447 {
00448 d.resize(numberOfNeurons, false);
00449 }
00450