构建时如何解决 ReLU is not member of torch::nn 错误?

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

我正在尝试使用 torch 构建一个简单的网络。但是当我尝试构建项目时,它 returns 一些错误消息,例如 error: ‘ReLU’ is not member of ‘torch::nn’

我的网络定义如下:

#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);

当我尝试构建时:cmake --build 。 --config 版本

它 returns 我这条消息:

[ 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

我知道提到的方法是 torch::nn 的成员。我该如何解决这个问题?

问题已解决。在制作 "cmake -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch .." 时,我的 libtorch 目录是“/home/user/folder/libtorch”。将 libtorch 目录更改为“/home/user/libtorch”后问题解决了。