Hi,
I have a net composed of layers in a nn::ModuleList. I didn’t succeed to apply the forward method on the modules in the list. How can I use them?
Here is the code to reproduce the issue:
#include <torch/torch.h>
using namespace torch;
struct LinearImpl : nn::Module
{
LinearImpl(int in_features, int out_features, bool bias):
linear_layer(nn::LinearOptions(in_features, out_features).bias(bias))
{
register_module("linear_layer", linear_layer);
}
torch::Tensor forward(torch::Tensor x)
{
x = linear_layer(x);
return x;
}
nn::Linear linear_layer;
};
TORCH_MODULE(Linear);
struct NetImpl : nn::Module
{
NetImpl(int in_features, int out_features, bool bias):
layers(Linear(in_features, out_features, bias),
Linear(out_features, out_features, bias))
{
register_module("layers", layers);
}
torch::Tensor forward(torch::Tensor x)
{
for (const auto &module : *layers)
{
x = module->forward(x);
}
return x;
}
nn::ModuleList layers;
};
TORCH_MODULE(Net);
int main()
{
Net net(32, 64, false);
torch::Tensor inputs = torch::randn(32);
torch::Tensor outputs = net(inputs);
std::cout << outputs << std::endl;
return 0;
}
And the error I have during compilation:
main.cpp: In member function ‘at::Tensor NetImpl::forward(at::Tensor)’:
main.cpp:36:25: error: ‘using element_type = class torch::nn::Module {aka class torch::nn::Module}’ has no member named ‘forward’
x = module->forward(x);
^~~~~~~
I am using libtorch version 1.6.0.dev20200415+cpu on linux.