How to set up torch::nn::SequentialImpl with 'torch::narrow'?

Hello, How do I use torch::narrow in a torch::nn::SequentialImpl? I am new to the c++ api. My first attempt was to create a Functional module, which I understand wraps a free function, but this doesn’t compile:

#include <torch/torch.h>

class NarrowingImpl : public torch::nn::SequentialImpl {
    NarrowingImpl() {
        using namespace torch::nn;

        // This was my first attempt, but it doesn't work
        push_back(Functional(torch::narrow, 0, 1, 1));

        // This works, but with slightly different semantics (end vs length)
        // push_back(Functional(functional::_narrow_with_range, 0, 1, 2));

int main() {
  Narrowing nrw;
  torch::Tensor x = torch::rand({2, 3, 4});

  std::cout << x << std::endl << std::endl;

  std::cout << nrw->forward(x) << std::endl;

As a side note, I was able to get it to work with functional::_narrow_with_range, but I’m not sure if that is the correct way to go about this.

Any help would be much appreciated. Thank you!

You can use functional::_narrow_with_range. The only potential issue is that this is an internal function, might change without public notification.

The reason you can not use torch::narrow directly is because there are 2 versions of torch::narrow,
Tensor narrow(const Tensor & self, int64_t dim, int64_t start, int64_t length);
Tensor narrow(const Tensor & self, int64_t dim, const Tensor & start, int64_t length);
We can not deduce the function type with parameter bindings at the moment, if you want to use torch::narrow in this case, you can wrap it with another name, for eg.
torch::Tensor aaa(const torch::Tensor& t, int a, int b, int c){ return torch::narrow(t, a, b, c);}
Then you can do Functional(aaa, 0, 1, 1);

Thank you for the explanation and alternative method for using torch::narrow. Thanks!!