Author Topic: parameter learning files  (Read 13432 times)

Offline panyinghui

  • Newbie
  • *
  • Posts: 1
    • View Profile
parameter learning files
« on: July 13, 2011, 13:40:51 »
The error, "Unhandled exception at 0x75aa9617 in learning.exe: Microsoft C++ exception:

HAPI::ExceptionMemory at memory location 0x001cf9c4..", will happen, when I add the

parsecases ( ) based on EM class.

The program:
# include "hugin"

# include <vector>
# include <string>
# include <cstdio>
# include <iostream>
# include <exception>
class EM {
public:
  EM (const string &fileName);

private:
  void specifyLearningParameters (Domain *d);
  void printLearningParameters (Domain *d);
  void loadCases (Domain *d);
  void printCases (Domain *d);
  void printNodeMarginals (Domain *d);
};


int main()
{
   new EM ("C:\\learning");
   return 0;
   
}

EM::EM (const string &fileName)
{
  string netFileName = fileName + ".net";
  Domain d (netFileName, NULL);

  string logFileName = fileName + ".log";
  FILE *logFile = fopen (logFileName.c_str (), "w");
  d.setLogFile (logFile);

  d.compile ();

  specifyLearningParameters (&d);

  loadCases (&d);

  DefaultParseListener pl;
  d.parseCases("C:\\learningdata", &pl);

  d.learnTables ();

  cout << "Log likelihood: " << d.getLogLikelihood () << endl;


  d.saveAsNet ("q.net");


}
void EM::specifyLearningParameters (Domain *d)
{
  const NodeList nl = d->getNodes ();

  NumberList data;

  for (NodeList::const_iterator nlIter = nl.begin (), nlEnd = nl.end ();
       nlIter != nlEnd; ++nlIter) {
    DiscreteChanceNode *node = dynamic_cast<DiscreteChanceNode*> (*nlIter);

    if (node != 0) {
      Table *table = node->getExperienceTable ();

      data.clear ();
      data.insert (data.end (), table->getSize (), 1);

      table->setData (data);
    }
  }

  d->setLogLikelihoodTolerance (0.000001);
  d->setMaxNumberOfEMIterations (1000);
}
void EM::loadCases (Domain *d)
{
  d->setNumberOfCases (0);

  size_t iCase = d->newCase ();
  cout << "Case index: " << iCase << endl;

  d->setCaseCount (iCase, 2.5);

  const NodeList nl = d->getNodes ();

  for (NodeList::const_iterator nlIter = nl.begin (), nlEnd = nl.end ();
       nlIter != nlEnd; ++nlIter) {
    DiscreteChanceNode *dcNode = dynamic_cast<DiscreteChanceNode*> (*nlIter);

    if (dcNode != 0)
      dcNode->setCaseState (iCase, 0);
  }

  DiscreteChanceNode *dcNode = dynamic_cast<DiscreteChanceNode*> (nl[1]);
  if (dcNode != 0)
    dcNode->unsetCase (iCase);
}
 


 I don't know why the error happen.
Can you help me?

Thank you very much!

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2295
    • View Profile
Re: parameter learning files
« Reply #1 on: July 14, 2011, 09:10:05 »
It seems you are missing the suffix "txt" in the file name for the data file.
HUGIN EXPERT A/S