Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - nuaafk

Pages: [1]
1
C++ / a puzzle in studing C++ API
« on: April 27, 2010, 16:01:56  »
hello!
Recently,I met a puzzle in studing C++ API. I really need a hand.
After my practice on some examples given in the forum and the help files . Now I want to realize a function with C++ programme. When I have a BBN net and the data file(the BBN net file named apple.net and the data file named apple.dat),how should I finish the EM learning with these two files (apple.net and apple.dat)and get every node's CPTs.
Last time ,I had asked this problem,and do some research on one example in test file of HUGIN, but I could hardly go through with that example , because I can not find where should the .net file and .dat file read. That is to say ,when I run this programme , I can't receive any useful result. If the followed code could not satisfy my Requirement ,would you please give me a sample about my function? ;)
 there followed the code : (waiting for your help!thank you!)
*****************************************************************
# include "hugin"

# include <vector>
# include <string>
# include <cstdio>
# include <iostream>
# include <exception>

using namespace HAPI;
using namespace std;

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 (int argc, char *argv[])
{
  if (argc != 2) {
    cerr << "Usage: " << argv[0] << " <net_file>\n";
    return -1;
  }

  new EM (string (argv[1]));

  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);
  printLearningParameters (&d);

  loadCases (&d);
  printCases (&d);

  d.learnTables ();

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

  printNodeMarginals (&d);

  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::printLearningParameters (Domain *d)
{
  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) {
      cout << dcNode->getLabel () << " (" << dcNode->getName () << "):\n";

      cout << "   ";
      if (dcNode->hasExperienceTable ()) {
   Table *table = dcNode->getExperienceTable ();
   NumberList data = table->getData ();
   size_t tblSize = table->getSize ();

   for (size_t i = 0; i < tblSize; i++)
     cout << data << " ";

   cout << endl;
      }
      else
   cout << "No experience table\n";

      cout << "   ";
      if (dcNode->hasFadingTable ()) {
   Table *table = dcNode->getFadingTable ();
   NumberList data = table->getData ();
   size_t tblSize = table->getSize ();

   for (size_t i = 0; i < tblSize; i++)
     cout << data << " ";

   cout << endl;
      }
      else
   cout << "No fading table\n";
    }
  }

  cout << "Log likelihood tolerance: " << d->getLogLikelihoodTolerance ()
       << endl;
  cout << "Max EM iterations: " << d->getMaxNumberOfEMIterations () << endl;
}


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);
}


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

  size_t nCases = d->getNumberOfCases ();

  cout << "Number of cases: " << nCases << endl;

  for (size_t i = 0; i < nCases; i++) {
    cout << "case " << i << " " << d->getCaseCount (i) << " ";

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

      if (dcNode != 0) {
   cout << " (" + dcNode->getName () + ",";
   if (dcNode->caseIsSet (i))
     cout << dcNode->getCaseState (i) << ") ";
   else
     cout << "N/A) ";
      }
    }
  }
  cout << endl;
}


void EM::printNodeMarginals (Domain *d)
{
  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) {
      size_t nStates = dcNode->getNumberOfStates ();

      cout << dcNode->getLabel () + " (" + dcNode->getName () + ")\n";

      for (size_t i = 0; i < nStates; i++)
   cout << " - " << dcNode->getStateLabel (i)
        << ": " << dcNode->getBelief (i) << endl;
    }
  }
}

2
C++ / Re: EM Learning -throuth API
« on: April 27, 2010, 07:02:50  »
thank you for your reply ,i will go ahead with your suggestion!

3
C++ / EM Learning -throuth API
« on: April 26, 2010, 13:54:39  »
Hi,
After learning about the structural learning through API, i have got interested in the C++ API .But ,i am not sure about how to use it in EM learning. Continued that topic "Structural Learning-through API", if the BBN's structure has been determined ,we want to get the conditional probability distribution with the help of a data file  next.And how to make it come true throuth C++ API?
    would you mind reply with a sample code? ;D
    regards,
    a new student waiting for your answer

4
hi, i am new here,
there is problem that has troubled me for  a long time.
When i use the EM learning part and load datas from the file. There comes a error:The limits of this software have been exceeded (too many states or too many cases).
i just do one example in the Tutorials , in fact ,10000 teams of datas is very big, but, sometimes we need lots of datas indeed.
So ,how to deel with it. Is there any set to change the limits? thank you for your help ;D

Pages: [1]