如何解决 ReLU 不是构建时火炬::nn 错误的成员?
How can I resolve ReLU is not a member of torch::nn error while building?
我正在尝试使用火炬构建一个简单的网络。但是当我尝试构建项目时,它会返回一些错误消息,例如错误:"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"后,问题解决了。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决 ReLU 不是构建时火炬::nn 错误的成员?