Search code examples
c++relu

Implementation of ReLU function on C++


I'm actually working on a CNN, I'm using a Sigmoid for the activation function, but i would like to use the ReLU.

I have implemented a code for ReLU using Eigen, but it doesn't seems to work, can you help me please ?

Here's my code:

Matrix ReLu(const Matrix & x){

    Matrix A;
    for( int i = 0; i< x.rows(); ++i )
    for (int j=0; i< x.cols(); j++) {
        if (x(i,j) <= 0){
            A(i,j)=(0.0);
        }
        else A(i,j)=(x(i,j));
    }
    return std::move(A.matrix());
}

Matrix ReLu_deriv (const Matrix& y) {
    Matrix B;
    for( int i = 0; i < y.rows(); ++i )
    for (int j=0; i < y.cols() ; j++)
    {
        {
        if (y(i,j) <= 0.0){
            B(i,j)=(0.0);
        }
        else B(i,j)=(1.0);
    }
    return std::move(B.matrix());
}

and the error is :

> /usr/include/eigen3/Eigen/src/Core/DenseCoeffsBase.h :365 : Eigen::DenseCoeffsBase<Derived,
> 1>::Scalar& Eigen::DenseCoeffsBase<Derived,
> 1>::operator()(Eigen::Index, Eigen::Index) [with Derived =
> Eigen::Matrix<double, -1, -1>; Eigen::DenseCoeffsBase<Derived,
> 1>::Scalar = double; Eigen::Index = long int]:  l'assertion « row >= 0
> && row < rows() && col >= 0 && col < cols() » a échoué.

Solution

  • You will need to initialize the your temporary matrix first properly:

    Matrix ReLu(const Matrix & x){
    
        Matrix A(x.rows(), x.cols());
        for( int i = 0; i< x.rows(); ++i )
        for (int j=0; j< x.cols(); ++j) {
            if (x(i,j) <= 0){
                A(i,j)=(0.0);
            }
            else A(i,j)=(x(i,j));
        }
        return std::move(A.matrix());
    }
    
    Matrix ReLu_deriv (const Matrix& y) {
        Matrix B(y.rows(), y.cols());
        for( int i = 0; i < y.rows(); ++i )
        for (int j=0; j < y.cols() ; ++j)
        {
            if (y(i,j) <= 0.0){
                B(i,j)=(0.0);
            }
            else B(i,j)=(1.0);
        }
        return std::move(B.matrix());
    }