13 #include <xercesc/dom/DOM.hpp>
27 XERCES_CPP_NAMESPACE_USE
29 using namespace PhysicsTools;
41 virtual void configure(DOMElement *
elem)
override;
44 virtual void trainBegin()
override;
45 virtual void trainData(
const std::vector<double> *
values,
47 virtual void trainEnd()
override;
49 virtual bool load()
override;
50 virtual void cleanup()
override;
64 unsigned int count, row;
66 std::auto_ptr<MLP> mlp;
67 std::vector<double> vars;
93 void ProcMLP::configure(DOMElement *
elem)
95 DOMNode *
node = elem->getFirstChild();
96 while(node && node->getNodeType() != DOMNode::ELEMENT_NODE)
97 node = node->getNextSibling();
101 <<
"Expected MLP config in config section."
104 if (std::strcmp(
XMLSimpleStr(node->getNodeName()),
"config") != 0)
106 <<
"Expected config tag in config section."
109 elem =
static_cast<DOMElement*
>(
node);
111 boost = XMLDocument::readAttribute<int>(
elem,
"boost", -1);
112 limiter = XMLDocument::readAttribute<double>(
elem,
"limiter", 0);
113 steps = XMLDocument::readAttribute<unsigned int>(
elem,
"steps");
117 node = node->getNextSibling();
118 while(node && node->getNodeType() != DOMNode::ELEMENT_NODE)
119 node = node->getNextSibling();
123 <<
"Superfluous tags in config section."
126 vars.resize(getInputs().
size() - (boost >= 0 ? 1 : 0));
134 std::ifstream
in(trainer->trainFileName(
this,
"txt").c_str());
151 std::auto_ptr<MLP> mlp(
new MLP(getInputs().
size() - (boost >= 0 ? 1 : 0),
152 getOutputs().
size(), layout));
153 mlp->load(trainer->trainFileName(
this,
"txt"));
156 std::ifstream
in(fileName.c_str(), std::ios::binary |
std::ios::in);
159 <<
"Weights file " << fileName
160 <<
"cannot be opened for reading." << std::endl;
165 in.getline(linebuf, 127);
166 while(linebuf[0] ==
'#');
168 int layers = mlp->getLayers();
169 const int *neurons = mlp->getLayout();
171 for(
int layer = 1; layer <
layers; layer++) {
174 for(
int i = 0;
i < neurons[layer];
i++) {
177 for(
int j = 0;
j <= neurons[layer - 1];
j++) {
178 in.getline(linebuf, 127);
179 std::istringstream
ss(linebuf);
186 neuron.second.push_back(weight);
188 layerConf.first.push_back(neuron);
190 layerConf.second = layer < layers - 1;
192 calib->
layers.push_back(layerConf);
200 void ProcMLP::trainBegin()
210 mlp = std::auto_ptr<MLP>(
211 new MLP(getInputs().
size() - (boost >= 0 ? 1 : 0),
212 getOutputs().
size(), layout));
225 void ProcMLP::trainData(
const std::vector<double> *
values,
226 bool target,
double weight)
229 double x = values[boost][0];
231 weight *= 1.0 + 0.02 *
std::exp(5.0 * (1.0 - x));
233 weight *= 1.0 + 0.1 *
std::exp(5.0 * x);
236 if (weight < limiter) {
249 for(
unsigned int i = 0;
i < vars.size();
i++, values++) {
252 vars[
i] = values->front();
255 for(
unsigned int i = 0;
i <
targets.size();
i++)
261 void ProcMLP::runMLPTrainer()
263 for(
unsigned int i = 0;
i <
steps;
i++) {
264 double error = mlp->train();
266 std::cout <<
"Training MLP epoch " << mlp->getEpoch()
267 <<
", rel chi^2: " << (error / weightSum)
272 void ProcMLP::trainEnd()
279 "(weighted " << weightSum <<
")" << std::endl;
283 mlp->save(trainer->trainFileName(
this,
"txt"));
300 std::remove(trainer->trainFileName(
this,
"txt").c_str());
std::vector< LayerSetAndLayers > layers(const SeedingLayerSetsHits &sets)
static void cleanup(const Factory::MakerMap::value_type &v)
MVATrainerComputer * calib
volatile std::atomic< bool > shutdown_flag false
tuple size
Write out results.
#define MVA_TRAINER_DEFINE_PLUGIN(T)