Search code examples
c++deep-learningtorch

How can I resolve ReLU is not a member of torch::nn error while building?


I am trying to build a simple network using torch. But when I try to build the project it returns some error messages like error: ‘ReLU’ is not a member of ‘torch::nn’

My network definitions is as follows:

#pragma once

#include <torch/torch.h>

class ConvNetImpl : public torch::nn::Module {
 public:
    explicit ConvNetImpl(int64_t num_classes = 10);
    torch::Tensor forward(torch::Tensor x);

 private:
    torch::nn::Sequential layer1{
        torch::nn::Conv2d(torch::nn::Conv2dOptions(1, 16, 5).stride(1).padding(2)),
        torch::nn::BatchNorm2d(16),
        torch::nn::ReLU(),
        torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2))
    };

    torch::nn::Sequential layer2{
        torch::nn::Conv2d(torch::nn::Conv2dOptions(16, 32, 5).stride(1).padding(2)),
        torch::nn::BatchNorm2d(32),
        torch::nn::ReLU(),
        torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2))
    };

    torch::nn::Linear fc;
};

TORCH_MODULE(ConvNet);

When I try to build with: cmake --build . --config Release

It returns me this message:

[ 33%] Building CXX object CMakeFiles/freespace_torch.dir/src/convnet.cpp.o
In file included from /home/fugurcal/freespace_torch/src/convnet.cpp:2:0:
/home/fugurcal/freespace_torch/include/convnet.h:13:20: error: ‘BatchNorm2d’ is not a member of ‘torch::nn’
         torch::nn::BatchNorm2d(16),
                    ^~~~~~~~~~~
/home/fugurcal/freespace_torch/include/convnet.h:13:20: note: suggested alternative: ‘BatchNorm’
         torch::nn::BatchNorm2d(16),
                    ^~~~~~~~~~~
                    BatchNorm
/home/fugurcal/freespace_torch/include/convnet.h:14:20: error: ‘ReLU’ is not a member of ‘torch::nn’
         torch::nn::ReLU(),
                    ^~~~
/home/fugurcal/freespace_torch/include/convnet.h:15:20: error: ‘MaxPool2d’ is not a member of ‘torch::nn’
         torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2))
                    ^~~~~~~~~
/home/fugurcal/freespace_torch/include/convnet.h:15:41: error: ‘MaxPool2dOptions’ is not a member of ‘torch::nn’
         torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2))
                                         ^~~~~~~~~~~~~~~~
/home/fugurcal/freespace_torch/include/convnet.h:15:41: note: suggested alternative: ‘Conv2dOptions’
         torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2))
                                         ^~~~~~~~~~~~~~~~
                                         Conv2dOptions
/home/fugurcal/freespace_torch/include/convnet.h:16:5: error: could not convert ‘{torch::nn::Conv2d((* &(& torch::nn::ConvOptions<2>(1, 16, torch::ExpandingArray<2, long int>(5)).torch::nn::ConvOptions<2>::stride(torch::ExpandingArray<2, long int>(1)))->torch::nn::ConvOptions<2>::padding(torch::ExpandingArray<2, long int>(2)))), <expression error>, <expression error>, <expression error>}’ from ‘<brace-enclosed initializer list>’ to ‘torch::nn::Sequential’
     };
     ^
/home/fugurcal/freespace_torch/include/convnet.h:20:20: error: ‘BatchNorm2d’ is not a member of ‘torch::nn’
         torch::nn::BatchNorm2d(32),
                    ^~~~~~~~~~~
/home/fugurcal/freespace_torch/include/convnet.h:20:20: note: suggested alternative: ‘BatchNorm’
         torch::nn::BatchNorm2d(32),
                    ^~~~~~~~~~~
                    BatchNorm
/home/fugurcal/freespace_torch/include/convnet.h:21:20: error: ‘ReLU’ is not a member of ‘torch::nn’
         torch::nn::ReLU(),
                    ^~~~
/home/fugurcal/freespace_torch/include/convnet.h:22:20: error: ‘MaxPool2d’ is not a member of ‘torch::nn’
         torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2))
                    ^~~~~~~~~
/home/fugurcal/freespace_torch/include/convnet.h:22:41: error: ‘MaxPool2dOptions’ is not a member of ‘torch::nn’
         torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2))
                                         ^~~~~~~~~~~~~~~~
/home/fugurcal/freespace_torch/include/convnet.h:22:41: note: suggested alternative: ‘Conv2dOptions’
         torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2))
                                         ^~~~~~~~~~~~~~~~
                                         Conv2dOptions
/home/fugurcal/freespace_torch/include/convnet.h:23:5: error: could not convert ‘{torch::nn::Conv2d((* &(& torch::nn::ConvOptions<2>(16, 32, torch::ExpandingArray<2, long int>(5)).torch::nn::ConvOptions<2>::stride(torch::ExpandingArray<2, long int>(1)))->torch::nn::ConvOptions<2>::padding(torch::ExpandingArray<2, long int>(2)))), <expression error>, <expression error>, <expression error>}’ from ‘<brace-enclosed initializer list>’ to ‘torch::nn::Sequential’
     };
     ^
CMakeFiles/freespace_torch.dir/build.make:62: recipe for target 'CMakeFiles/freespace_torch.dir/src/convnet.cpp.o' failed
make[2]: *** [CMakeFiles/freespace_torch.dir/src/convnet.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/freespace_torch.dir/all' failed
make[1]: *** [CMakeFiles/freespace_torch.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

I know mentioned methods are member of torch::nn. How can I resolve this issue?


Solution

  • Issue solved. While making "cmake -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch .." my libtorch directory was "/home/user/folder/libtorch". After changing the libtorch directory to "/home/user/libtorch" issue solved.