如何解决 ReLU 不是构建时火炬::nn 错误的成员?

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

本文关键字:nn 错误 成员 构建 何解决 解决 ReLU      更新时间:2023-10-16

我正在尝试使用火炬构建一个简单的网络。但是当我尝试构建项目时,它会返回一些错误消息,例如错误:"ReLU"不是"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 Release

它向我返回此消息:

[ 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

我知道提到的方法是火炬的成员::nn。如何解决此问题?

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