Results 1 to 7 of 7

Thread: neural network & objects

  1. #1
    Aragorn
    Guest

    neural network & objects

    I want to simulate a neural network with c++. The problem is that i never used objects seriously
    For the moment i'll stick to feed forward networks.
    I need to create a Cell object with all its properties (kind of cell, connections to other cells, activation state & some flags etc...), but where should i place all the cells?
    1) in an array of Cells?
    can I create arrays of objects? I guess the answer is yes, but could anyone give me some hints?
    2) should i place the array of cells in a new object called Network?

    This is more tricky:
    Cells will have a property with all the connections to oter cells; pointers I suppose, but I need to create two more objects, input_Cell & output_Cell. These i will place at the beginning and the end of the network, so that i can set the input and read the output. But how can i make allthese three objects (Cell, input_Cell & output_Cell) compatible with the connections property?
    I guess that has something to do with inheritance: am i right?
    Shold I define a basic object (basic_Cell) that will have 3 different particular cases, and then set the connections among these primary basic_Cell?

    thank you

  2. #2
    Zaei
    Guest
    You can contain all of the cells in an STL vector class ("std::vector<CELL> myCells;"). For the connections, you can set it up like this:
    Code:
    class CELL
    {
        //constructor/destructor//stuff
        
        std::vector<CELL*> inputCell; //all of the input cells
        CELL* outputCell;
    };
    This stores all of the cells in a tree-like structure:
    Code:
    I = input; O= output;
    I     I     I            I      I     I     I // input cells. all entires in input vector are NULL
     \   / \   /              \    /  \   /  \   /
       O I  OI                 OI      OI      OI 
        \    /                 \      /    \     /
          O                        O       O  //Ouput cells. outputCell pointer = NULL
    See how it all ties together? This way, you can check for NULL pointers in input and output cells to see what kind of cell it is. That should get you started. Sorry for the bad ASCII art =).

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Using some virtual inheritance you could derive from an abstract class cell and add on functionality like input and output devices.

    i'd use an array of pointers to cells as ADT's and then access/process the functionality of each cell by a simple call to a virtual function. something like

    cell** cellf;// first
    cell** celll;// last
    cell** celli;//iterator
    for (celli=cellf;celli<celll{Loggresult((*celli++)->Process())};


    whereas loggresult would display the result of each process, process would be the virtually inherited function on celli iterator object that could be any type of a cell.
    Last edited by kedaman; Jun 26th, 2001 at 12:06 AM.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I was in a bit hurry last time, so i'll continue here..
    Code:
    //this is the ADT cell
    class cell {
    cell(){}
    virtual ~cell(){};
    virtual RESULT Process()=0;
    ;}
    
    //some derivatives of cell that virtually inherits Process
    //you can add other methods and vars if you need...
    class outputcell:public cell{
    Process() {/* do something here */};
    };
    
    
    class inputcell:public cell{
    Process() {/* do something here */};
    };
    
    //then to populate the array you could do as you wish 
    //but to instantiate the cells you need to new them dynamically, heres a sample with 3 cells two output ones and a input one.
    
    cellf=new cell*[3];
    celli=cellf;
    *celli++=new outputcell;
    *celli++=new inputcell;
    *celli=new outputcell;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Aragorn
    Guest
    thank's guys, you have given me a lot of new info.

    Zaei: The idea of using templates doesn't sound bad at all, but I really don't know how to do it. I would love it to learn it, but i think it's still a bit far ahead. Actually i'm a real newbie for most things about OOP.
    However i will take it into account, and i already rose some questions:

    the following sentence

    std::vector<CELL*> inputCell;

    creates a pointer (named inputCell) to some sort of "array" or vector of cells, isn't it? I think i should study what this STL can do for me...

    Besides, the tree-like structure is ok for some kind of feed forward networks, but is not general enough, though i do not doubt that you can make it more flexible in quite the same fashion.

    If i would want multiple outputs, i.e. the same value forwarded to many cells should i use:
    class cell {
    //constructor/destructor stuff
    std::vector<CELL*> inputCell;
    std::vector<CELL*> outputCell;
    };
    I imagine this would give me a cell with multiple inputs and multiple outputs (correct me if i'm wrong!)

    Kedaman: Your suggestion is more close to what i already had in mind though i think i can merge it with Zaei's method, so i'm sure that your piece of code will be very helpful!
    Already i have some questions: the code

    class outputcell: public cell{
    Process() {/* do something here */};
    };

    i suppose is the constructor of the cell (outputcell) although the name of it is "cell". Is that right? (same for inputcell, of couse)

    Thank's guys. You have been really helpful. I will come up with more questions, but it will take me some time (for i do this for hobby and i don't have much spare time!)

    bye!

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    the STL vector can be used as an array wrapper, it's not hard to understand templates but i'd just suggest using an array of pointers instead.

    the idea i was suggesting is called polymorphism, which in this case means objects can be derive from the same class but also act differently.

    class outputcell: public cell{
    Process() {/* do something here */};
    };

    this is the declaration of the outputcell class, it derives cell, which means it is a cell and inherit it's properties and methods, Process() function in cell is a "pure" virtual function, which means you have to derive from it to use it, this also makes cell an ADT (abstract data type) which you cannot instantiate. The idea is that you can make different types of cells but they are all cells, so when you call Process() on them, it calls the Process() function in the aproperiate class. This makes things much easier for you when you can process all cells without even knowing what they are and do.

    a constructor is a function with the same name as the class, and is called when you instantiate the class, you can use it to initialize variables or pass parameters to it on initialisation.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Aragorn
    Guest

    Thumbs up cool thing!

    thank's kedaman. I've been studying it, and it doesn't seem as difficult as i had first expected.
    I have already defined the class std_cell and input_cell from an ADT cell, and i set the connections among cell so that they all look the same, though i still have to add functionality to the whole network.

    I wonder if i could define some kind of container for cells, so that i can create cells in it and then define methods such as a function output(input) from the whole network, connection actualization, etc...
    My idea is the following

    class network {
    typedef std::vector<float> pattern;

    std::vector<cell*> elements; // Zaei:

    some_type add_cells(//stuff);
    pattern output(pattern input) {
    //stuff here
    }
    // etc...
    };

    but i don't really know the limits of this new ting (for me!) called OOP, so if you think i'm too arcaic just let me know

    thanks in advance!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width