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

问题描述 投票:0回答:1

我正试图使用 torch 构建一个简单的网络,但当我尝试构建项目时,它返回一些错误信息,如 ReLU 不是 torch::nn 的成员。但当我尝试构建项目时,它返回一些错误信息,如 错误:'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

我知道提到的方法是 torch::nn 的成员。我怎样才能解决这个问题?

c++ deep-learning torch
1个回答
0
投票

问题解决了。当进行 "cmake -DCMAKE_PREFIX_PATH=absolutepathtolibtorch ... "时,我的libtorch目录是 "homeuserfolderlibtorch"。将libtorch目录改为 "homeuserlibtorch "后,问题解决了。

© www.soinside.com 2019 - 2024. All rights reserved.