Dear all,
I have implemented a CNN composed by several layers using libtorch. I have first defined a generic layer as:
struct ConvBlock : torch::nn::Module {
torch::nn::Conv1d conv{nullptr};
torch::nn::ReLU relu{nullptr};
ConvBlock(int in_channels, int out_channels, int kernel_size, int padding):
conv(torch::nn::Conv1d( torch::nn::Conv1dOptions(in_channels, out_channels, kernel_size).stride(1).padding(padding) ) ),
relu(torch::nn::ReLU()){
// Register the two modules of the convolutional block
conv = register_module("conv", conv);
relu = register_module("relu", relu);
}
torch::Tensor forward(torch::Tensor x){
x = conv->forward(x);
return relu->forward(x);
}
};
Then I have defined the Network model as:
class CNNNet : public torch::nn::Module {
public:
CNNNet(){
c1 = register_module("c1", c1);
c2 = register_module("c2", c2);
c3 = register_module("c3", c3);
p1 = register_module("p1", p1);
c4 = register_module("c4", c4);
c5 = register_module("c5", c5);
c6 = register_module("c6", c6);
}
std::tuple<torch::Tensor, torch::Tensor> forward(torch::Tensor x){
torch::Tensor indices;
x = c1.forward(x);
x = c2.forward(x);
x = c3.forward(x);
x = p1->forward(x);
x = c4.forward(x);
x = c5.forward(x);
x = c6.forward(x);
return x;
}
private:
ConvBlock c1 = ConvBlock(1, 16, 7, 0);
ConvBlock c2 = ConvBlock(16, 16, 5, 0);
ConvBlock c3 = ConvBlock(16, 16, 3, 0);
torch::nn::MaxPool1d p1 = torch::nn::MaxPool1d(torch::nn::MaxPool1dOptions(2));
ConvBlock c4 = ConvBlock(16, 32, 3, 0);
ConvBlock c5 = ConvBlock(32, 32, 3, 0);
ConvBlock c6 = ConvBlock(32, 32, 3, 0);
};
However, when I compile I get the following error:
error: no matching function for call to ‘ CNNNet::register_module(const char [3], ConvBlock&)’
59 | c2 = register_module(“c2”, c2);
How can I manage this issue?