vendredi 29 juin 2018

C++ vector not storing class

I am writing a simple feed-forward neural network in c++, however when I try to store my neuron class in my layer structure, it crashes and gives this output:

terminate called after throwing an instance of 'std::bad_array_new_length'

what(): std::bad_array_new_length

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

Here is my program:

#include <iostream>
#include <random>
#include <vector>
using namespace std;
random_device e;

int randomG(int min, int max){
  return (e()%(max-min))+min;
}

int f(int v){
  return v+5;
}

class neuron{
public:
  neuron(int _insN, int _funtype){
    insN=_insN;funtype=_funtype;
  }
  float out;
  void genWeights(){
    for (int i = 0; i < insN; i++){
      weights[i]=float(randomG(1,1000))/100;
    }
  }
  float parceOut(float* ins){
    float preOut=0;
    for (int i = 0; i < insN; ++i){
      preOut+=(weights[i]*ins[i]);
    }
    out=activation(preOut, funtype);
  }
private:
  float ReLU(float f){
    if (f<=0){return f*0.01;}
    else {return f;}
  }
  float Softmax(float f){
    return f;
  }
  float activation(float f, int function){
    switch(function){
    case(1): return ReLU(f); break;
    case(2): return f; break;
    case(3): return Softmax(f); break;
    }
  }
  int insN;
  int funtype;
  float* weights = new float[insN];
};

struct layer{
  int insN=1, neuronN=1;
  float* outs=new float[neuronN];
  vector<neuron> nS;
  void generateNeurons(){
    for(int i=0;i<1;i++){
      nS.push_back(neuron(insN,1));
    }
  }
};

int main(int argc, char *argv[])
{
  layer input;
  input.insN=1;
  input.neuronN=5;
  input.generateNeurons();
  cin.get();
  return 0;
}

I don't think that it is to hard to understand, but if it is I am trying to make a vector with my neuron class in my layer structure, but even when I put just one neuron in the vector it says that there is not enough memory allocated to the program. I have tried converting the neuron class into a structure, but that did not help.

Aucun commentaire:

Enregistrer un commentaire