-1

recently I have been trying to make a Neural Network with an arduino library and I came across a library, that was quite literally, called Neural Network by George Chousos. I stumbled apon a couple of errors that I managed to fix quite simply, but then I got caught on the error of

sketch_sep22b:24:43: error: incompatible types in assignment of 'float*' to 'float [4]' outputs = NN.FeedForward(inputs[i]);

This is all of my code:

#include <math.h> // Include Math Library
#include <NeuralNetwork.h> // Include Neural Network Framework

const unsigned int layers[] = {4, 9, 4}; // First Value (Inputs), Second Value (Neurons / Hidden Layers), Third Value (Outputs)
float outputs[4] = {}; //Outputs Float

const float* /* Pointer */ inputs[1] = {1};

const float expectedOutputs[1] = {1}; //Training Values

void setup()
{
  Serial.begin(9600);
  NeuralNetwork NN(layers, NumberOf(layers));

  for (int i = 0; i < 3000; i++)
  {
    for (int j = 0; j < NumberOf(inputs); j++)
    {

      for (int i = 0; i < NumberOf(inputs) - 1; i++)
      {
        outputs = NN.FeedForward(inputs[i]);
        Serial.println(outputs[0], 7);
      }

      NN.print();
    }
  }
}

Edit:

The declaration for FeedForward is:

float *FeedForward(const float *inputs); // Moves Calculated outputs as inputs to next layer.
andand
  • 17,134
  • 11
  • 53
  • 79
  • 3
    Can you post the declaration for NeuralNetwork::FeedForward? – andand Sep 25 '20 at 21:31
  • You can't just copy pointers to pointers with `=`, you need to use something like `memcpy` or, even better, take advantage of the C++ Standard Library and use `std::vector` if you can. – tadman Sep 25 '20 at 21:33
  • You can't assign to an array, ever. They are too stupid. You must assign to elements. – user4581301 Sep 25 '20 at 21:33
  • "const float expectedOutputs[1] = {1};" What is the point of an array with only one element? Why make it an array? – Delta_G Sep 25 '20 at 22:26
  • @Delta_G It's just temporary, I am collecting data for the neural network, I just don't have it yet. – James B Sep 26 '20 at 07:02
  • @andand This is the source code for the library it includes the definition and declaration for the whole Neural Network Class https://github.com/GiorgosXou/NeuralNetworks – James B Sep 26 '20 at 08:18
  • Added the declaration to the question and updated my answer below. – andand Sep 27 '20 at 15:15

1 Answers1

0

Okay, I looked at the link and the declaration. The first problem, and the one that's leading to the compiler error is the return type. The declaration is expecting output to be a pointer, not an array. So, changing the declaration of output to:

float* outputs; //Outputs Float

should fix the compiler error. I haven't tried it, but that looks to be the issue the compiler is catching. This will likely return a pointer to an array of 4 floats which you will need to deallocate later (using either free or delete[] depending on how the library allocated the memory) once you're done with it or it will create a memory leak.

As others have noted, your current declaration of input is attempting to access a fixed memory location (1) which leads to undefined behavior, so, you still need to address this as well. Since the library appears to be expecting an input with 4 float values, you should either give it an array with 4 float values declared at compile time, or you could dynamically allocate an array with 4 values at run time.

The first option looks like this:

const float inputs[4] = {1.0, 2.0, 3.0, 4.0};

The second option looks like:

float* input;

...

input = new float[4];
input[0] = 1.0;
input[1] = 2.0;
input[2] = 3.0;
input[3] = 4.0;

...

output = NN.FeedForward(input);

...

delete[] input;
andand
  • 17,134
  • 11
  • 53
  • 79
  • That's an important problem but not the one being asked about. – user4581301 Sep 25 '20 at 21:56
  • @user4581301 Why is my question down-voted, I followed everything that stack overflow recommends. – James B Sep 26 '20 at 08:15
  • @JamesB hard to say for sure. Could be the lack of a [mre]. The example provided should be able to compile except for the one error you are asking about, and you can reproduce the problem in about three lines of code. When someone can make an answer like this, one that's 100% correct but solves a different problem, the question is probably not tight enough. – user4581301 Sep 26 '20 at 19:20
  • The downvote may be because you didn’t link to your library. Put the link you have in the comments up into the question and I’ll upvote to get you back to zero. – Delta_G Sep 26 '20 at 23:03