-3

Recently I've been trying to write a neural network program. I have all a neurons connections stored in a vector in the neuron. However whenever I push back a connection into the vector it doesn't seem to store (I can tell via debug mode), and when I try to add up the activation values of the vectors in a for loop, I get an out_of_range error. Here's my code.

Main.cpp

#include <iostream>
#include "neuron.h"

void displayboard(bool board [8][8]);

using namespace std;

int main()
{
    int id = 2;
    int inputids [] = {3};
    int outputids [] = {4};
    int inputweights [] = {5};
    bool new_neuron = true;
    neuron test (inputids, outputids, inputweights, new_neuron, id);
    test.connections.at(0).value = 6;
    // here is where the error is returned
    test.activation();
    cout << test.activationnumber;
    return 0;
}

And here's Neuron.cpp:

#include "neuron.h"
#include <vector>
#include <random>
#include <ctime>

using namespace std;

neuron::neuron(int inputids [], int outputids [], int inputweights [], 
    bool new_neuron, int id)
{
    this->id = id;
    if (new_neuron==true) {
        srand (time(0));
        connection tempconnection;
        for (int i = 0; i <=(sizeof (inputids)/sizeof (inputids [0])); i++)            
        {
            tempconnection.type=false;
            tempconnection.tofrom = inputids [i];
            tempconnection.weight = rand ();
            this->connections.push_back (tempconnection);
        }
    // this continues on for other options
}

void neuron::activation (){
    for (int i=0; i<=this->connections.size (); i++) {
        this->activationnumber += ((this->connections.at(i).value)
            *(this->connections.at (i).weight));
    }
}
Tetrabyte
  • 1
  • 1

1 Answers1

3

UPDATE: Reading this will help you understand why your "sizeof/sizeof" approach is not good in C++.


Original answer

The behavior of sizeof(array)/sizeof(array[0]) might not be what you expected. The following code outputs 2 but you seem to expect 4. Use array for objects in the stack or vector for objects in the heap.

#include <iostream>
using namespace std;

void foo( int array[] )
{
    wcout << sizeof( array ) / sizeof( array[ 0 ] );
}

int main()
{
    int test[ 4 ];
    foo( test );
    return 0;
}

Change

int inputids [] = {3};
int outputids [] = {4};

to

vector< int > {3};
vector< int > {4};

Also change

neuron(int inputids [],int outputids [] …
{
    …
    for (int i = 0; i <= …; i++)            
        …
        tempconnection.tofrom = inputids [i];

to

neuron( vector< int > & inputids, vector< int > & outputids …
{
    …
    for( auto id : inputids )
        …
        tempconnection.tofrom = id;
Hector
  • 2,464
  • 3
  • 19
  • 34
  • Ok, thanks for the advice. How should I find the number of elements in the array? – Tetrabyte Aug 03 '15 at 18:30
  • 2
    ***How should I find the number of elements in the array?*** Pass this in as a parameter or use std::vector from the start. – drescherjm Aug 03 '15 at 18:48
  • @Tetrabyte Figure out the size using whatever method you thought `sizeof` used to tell. :) – David Schwartz Aug 03 '15 at 19:01
  • Umm.. So I did some testing, I implemented the changes you suggested and got the same error. I also tested the code you gave me to find the size of an array, and it returned the right answer. – Tetrabyte Aug 05 '15 at 02:22