0

I'm bulding my own Neural Network with my own Matrix class.

I'm trying to use the swishMatrix() function on a Matrix2D class object, before adding it to a vector<Matrix2D> variable. But I get this error and I have no idea why. -> no matching function for call to 'std::vector<Matrix2D>::push_back(int)'|

When I use the swishMatrix() on a normal Matrix2D object it works fine.

Here's the Matrix2D class

class Matrix2D{
    public:
        int rows;
        int columns;
        vector<vector<float> > matrix;
        Matrix2D() = default;
        Matrix2D(int x, int y){
            rows = x;
            columns = y;
            for (int i = 0; i < rows; i++) {
                vector<float> v1;
                for (int j = 0; j < columns; j++) {
                    v1.push_back(0);
                }
                matrix.push_back(v1);
            }
        }
        swishMatrix(){            
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    matrix[i][j] = matrix[i][j] * sigmoid(matrix[i][j]);
                }
            }            
        }        
        //Here there's a lot of static functions for matrix operations
};

Here's the Neural Network class

class NeuralNewtork{
    public:
       
        //A lot more declaration here but not important
        Matrix2D first_hidden_weights;

        Matrix2D input_nodes;

        vector<Matrix2D> hidden_weights;      

        vector<Matrix2D> hidden_biases; 

        vector<Matrix2D> activated_hidden_nodes;       

        NeuralNewtork(int input_nodes, int hidden_layers, int hidden_nodes, int action_nodes){
            first_hidden_weights = Matrix2D(numberof_hidden_nodes, numberof_input_nodes);
            first_hidden_weights.randomizeMatrix();            

            hidden_weights.reserve(numberof_hidden_layers-1);
            for (int i=0; i<numberof_hidden_layers-1; i++){
                hidden_weights.push_back(Matrix2D(numberof_hidden_nodes, numberof_hidden_nodes));
                hidden_weights.back().randomizeMatrix();
            }           

            hidden_biases.reserve(numberof_hidden_layers);
            for (int i=0; i<numberof_hidden_layers; i++){
                hidden_biases.push_back(Matrix2D(numberof_hidden_nodes, 1));
                hidden_biases.back().randomizeMatrix();
            }     
            //There are more declerations here but they aren't important for this problem   
        }

        feedForward(Matrix2D input){
            input_nodes = input;

        
            for(int i = 0; i < numberof_hidden_layers+1; i++){
                if(i==0){                    
                    activated_hidden_nodes.push_back(Matrix2D::matrixAddition(Matrix2D::matrixMultiplication(first_hidden_weights, input_nodes), hidden_biases[0]).swishMatrix());
                    //This is the line where I get the error
                    //no matching function for call to 'std::vector<Matrix2D>::push_back(int)'|
                    
                }
                if(i!=0 && i!=numberof_hidden_layers){
                    activated_hidden_nodes.push_back(Matrix2D::matrixAddition(Matrix2D::matrixMultiplication(hidden_weights[i-1], activated_hidden_nodes[i-1]), hidden_biases[i]).swishMatrix());  
                    //This is also a line where I get the error
                    //no matching function for call to 'std::vector<Matrix2D>::push_back(int)'|
                }
                if(i==numberof_hidden_layers){
                    //Not important
                }
            }
        }

I might have missed some part of the code, it was hard to keep short, but all the needed variables are correctly assigned.

  • What's your implementation of `Matrix2D::matrixAddition()` and `Matrix2D::matrixMultiplication()`? – Louis Go Nov 17 '21 at 08:17
  • Also what does `swishMatrix()` return? If it's default `int`, then that's the problem. The error msg infers that the code `Matrix2D::matrixAddition(Matrix2D::matrixMultiplication(hidden_weights[i-1], activated_hidden_nodes[i-1]), hidden_biases[i]).swishMatrix()` returns `int`... I guess. – Louis Go Nov 17 '21 at 08:17
  • Please *always* declare return types for your functions, even if it's just `void`. This isn't JavaScript. – Botje Nov 17 '21 at 08:21
  • `swishMatrix` doesn't return anything, it just changes the current objects properties. Atleast that's what I'm trying it to do. – SnowballAvocado Nov 17 '21 at 08:21
  • except you're calling `foo.push_back(bar.swishMatrix())`. The return type of `swishMatrix` is the argument type for `push_back`. C++ needs to know! – Botje Nov 17 '21 at 08:23
  • 1
    This is not legal C++ code. `swishMatrix()` has no type. Every function and every object need to be declared with a type. Did you get any compiler warnings? Never ignore those. – n. m. could be an AI Nov 17 '21 at 08:23
  • @SnowballAvocado if your function doesn't return anything, make it return `void` – justANewb stands with Ukraine Nov 17 '21 at 08:23
  • So I cannot have a function that just changes the current object's properties? All the fucntions need to be static and create a new object? – SnowballAvocado Nov 17 '21 at 08:25
  • 1
    I think you may have skipped ahead a bit far in your [nice C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Nov 17 '21 at 08:26
  • You have an inconsistent API, some of your functions return a new matrix (like matrixAddition) and other modify the matrix in-place (like swishMatrix). It is not necessarily wrong as far as correctness goes, but it is potentially confusing. – n. m. could be an AI Nov 17 '21 at 08:36
  • On an unrelated note, `vector>` is not a good representation for a 2D matrix. It is more efficient to use a flat `vector` and do the index calculations manually, i.e. instead of `matrix[i][j]` use `matrix[i*columns+j]`. – n. m. could be an AI Nov 17 '21 at 08:38

1 Answers1

0

The trivial fix is to make swishMatrix return the matrix object again:

Matrix2D& swishMatrix(){            
    for (int i = 0; i < rows; i++) {
         for (int j = 0; j < columns; j++) {
              matrix[i][j] = matrix[i][j] * sigmoid(matrix[i][j]);
         }
    }     
    return *this;       
}  

Never ever write code again without explicit return types on your methods.

Botje
  • 26,269
  • 3
  • 31
  • 41