Search code examples
c++tensorlibtorch

How do you transpose a tensor in LibTorch?


I can't seem to figure out how to transpose a tensor in LibTorch, the (C++ version of PyTorch).

torch::Tensor one_T = torch::rand({6, 6});

int main() {
    std::cout << one_T.transpose << "\n";
}

My error...

/home/iii/tor/m_gym/multiv_normal.cpp:53:24: note:   mismatched types ‘const std::set<Types ...>’ and ‘at::Tensor (at::Tensor::*)(at::Dimname, at::Dimname) const’
   53 |     std::cout << one_T.transpose << "\n";
      |                        ^~~~~~~~~
/home/iii/tor/m_gym/multiv_normal.cpp:53:24: note:   mismatched types ‘const std::set<Types ...>’ and ‘at::Tensor (at::Tensor::*)(int64_t, int64_t) const’ {aka ‘at::Tensor (at::Tensor::*)(long int, long int) const’}
In file included from /home/iii/tor/m_gym/libtorch/include/c10/util/Logging.h:28,
                 from /home/iii/tor/m_gym/libtorch/include/c10/core/TensorImpl.h:17,
                 from /home/iii/tor/m_gym/libtorch/include/ATen/core/TensorBody.h:20,
                 from /home/iii/tor/m_gym/libtorch/include/ATen/core/Tensor.h:3,
                 from /home/iii/tor/m_gym/libtorch/include/ATen/Tensor.h:3,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/function_hook.h:3,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/cpp_hook.h:2,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/variable.h:6,

Solution

  • one_T.transpose(0, 1)

    The 0, 1 is the type of default transpose used in PyTorch and Numpy without being specified.