Indexing of torch::from_blob() returns wrong data in C++ compare to Python

Viewed 14

I have a tensor which has shape of (22,20,8), i want to index it using ellipsis in C++ (like the style in python).

Original Tensor Data:

(1,.,.) = (Column 1 to 6)
  1.7799e-22  1.0000e+00  9.1812e-01  3.7039e-01 -1.7281e-01 -2.2303e-01
  5.6862e-21  1.0000e+00  1.2175e+00 -7.5004e-02 -1.7592e-01  2.2146e-01
  2.1190e-10  1.0000e+00  8.3094e-01  3.5965e-02 -6.6968e-02 -1.0352e-01
  3.3032e-04  9.9967e-01  7.8312e-01  9.4667e-02 -3.4348e-02 -2.3513e-01
  1.2638e-04  9.9987e-01  7.8169e-01  6.7750e-02 -5.0201e-02 -2.3084e-01
  1.3742e-03  9.9863e-01  6.2953e-01  4.1200e-02  2.3423e-02 -1.7077e-01
  ...

Result from indexing with PyTorch:

>>> result = tensor[..., 0]
>>> result

[[1.77992756e-22 5.68621886e-21 2.11894446e-10 3.30319250e-04
1.26377534e-04 1.37416169e-03 1.23896878e-02 9.82156545e-02
3.32255810e-02 1.83563633e-03 1.20873723e-04 1.82283948e-05
1.14620752e-05 5.54892858e-06 2.80703119e-07 1.27647101e-10
2.37733531e-11 2.45479266e-12 6.97299098e-13 8.68344241e-09]...

>>> tensor.shape
(22,20)

Result from indexing with LibTorch (C++):

auto output_buffer = std::shared_ptr<float>{new float[22*20*8]};
std::vector<float> outBuf(output_buffer.get(), output_buffer.get() + output_size);
torch::Tensor tensor = torch::from_blob(outBuf.data(), {22,20,8});
torch::Tensor result = tensor.index({"...", 0});

std::cout<<result;

 (Columns 1 to 10) 0.0000  0.0000  0.0000  0.0003  0.0001  0.0014  0.0124  0.0982  0.0332  0.0018
 0.0000  0.0000  0.0000  0.0000  0.0001  0.0031  0.0040  0.0027  0.0004  0.0000
 0.0000  0.0000  0.0000  0.0000  0.0000  0.0001  0.0000  0.0000  0.0000  0.0000
 0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000
 0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000
 0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000
 0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000
 0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000  0.0000

 ...
 [CPUFloatType{22,20}]

The correct one is Python. Am i missing something or there're things to do with 'view' data of from_blob function or there'is mistake with pointer usage ?

0 Answers
Related