Libann

Libann: Using Libann in your Programs


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2. Using Libann in your Programs

There are no special features that you need to know about when using Libann, that don't also apply to other C++ libraries. However, the effects of ignoring them might be more severe than with simpler programs. This chapter is largely a memorandum for persons not thoroughly familiar with C++ programming. If you're an expert with C++, then you can skip this chapter, or just give it a cursory glance.

2.1 Namespaces  Avoiding identifier clashes.
2.2 Public Header Files  Declarations you need to know about.
2.3 Exceptions  Exceptional situations.
2.4 Randomness  
2.5 Compiling and Linking with Libann  How to produce an executable.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.1 Namespaces

Everything provided by Libann is in the ann namespace. Therefore, your code should:
  • Prepend every typename and classname with ann::; or
  • Put the statement using namespace ann; at the start of every file.
The latter is not recommended in header files.

If you don't do one of these, then your compiler won't recognise any identifiers from the library. This is a feature designed to prevent clashes with symbols from other libraries or from other parts of your own program.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.2 Public Header Files

There is a public header file for each type of Neural Network provided by the library. For example if you want to use a Multi-Layer Perceptron network, then you must include the `ann/mlp.h' file. All #include statements must have the ann/ prefix, and on most compilers should be enclosed by angle brackets. For example:
 
#include <ann/mlp.h>

As well as a header file for each network type, there is a common file, `ann/ann.h' which contains declarations for classes which you'll almost definitely need if you're going to do anything useful with Libann. More about this header file later.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.3 Exceptions

The library throws exceptions! Most often, an exception means that you have provided invalid data to a Neural Network. For example, if you pass a vector to a network which has the wrong input size, then you'll definitely get an exception.

This is not to say that all exceptions are errors. Exceptions should be thought of as exactly what their name suggests, viz: things which happen only in exceptional circumstances. For example you might know that most of your vectors are the appropriate size for a network, but that there are the occasional few which are not. In that case, you could catch the exception and deal with the few cases as they arise.

The potential to throw exceptions however, means that you must protect your main function with a try-catch block:

 
#include <stdexcept> 
#include <iostream>

int
main(int argc, char **argv)
{
 try {

  // Program will go here

  return 0;
 }
 catch (const std::exception &e) { 
  std::cerr << "Unexpected exception " << e.what() <<
  std::endl;
 }
 catch (...) {
  std::cerr << "Unhandled exception\n";
 }
 return 1;
}

There is nothing new here. Any C++ program should have this (or something similar) in its main. You might not think you need to be worried about exceptions, but you do (use of the new operator has the potential to throw an exception). If you don't catch exceptions like this, then your program may unexpectedly terminate and you'll have no idea why.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.4 Randomness

Many types of neural network depend upon random initialisation. This is important to ensure that solutions do not fall into local minima or become biased towards a particular solution. The library uses the standard `libc' call rand() to generate its random numbers.

Therefore, before using Libann, your program should make a call to srand() to initialise the random generator seed. Whilst developing and debugging, it's useful to use the same seed each time. Simply calling srand(0) is the easiest way to do this. When your program is complete however, you should ensure that the generator is truly random. One way to do this, is to initialise the seed from the real time clock:
 
#include <stdlib.h>
#include <time.h>

int
main()
{
  srand(time(0));

  .
  .
  .

  return 0;
}


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.5 Compiling and Linking with Libann

There's nothing special about compiling and linking a program with Libann. Assuming that you have installed Libann in a directory called `/usr/local/libann', on most systems, you would use a command line similar to
 
g++ -I /usr/local/libann/include -lann -L /usr/local/libann prog.cc
where prog.cc is the name of the source file for your program. For all but the most trivial programs of course, you will probably want to split your source files and to use make or some other build tool to assist you.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by John Darrington on May, 15 2003 using texi2html

[Home] [Frequently Asked Questions] [News] [Development] [Links] [Using and Installing] [Download] [Licence]