Strange issue returning torch::Tensors in libtorch/C++

Crophone: <p>I am trying to convert the version of  poseNet code form python to c++.</p>
<pre><code class="lang-auto">https://github.com/rwightman/posenet-pytorch
</code></pre>
<p>I created two torch::Tensor objects and return them in a function and get them in another function. But the return of  one  tensor  succeed while another failed（ [Tensor (undefined)] ) .</p>
<p>does it because the local area of C++? but I can’t make sure of that, because both tensor was constructed in called function, and even I  add “static” before where I construct “scores_vec”.</p>
<p>So Would anyone help me figure out what cause that?<br>
And is there a way to get both returned tensor successfully?<br>
Thanks!</p>
<p>Here is the C++ code and commented python code for easy compare?</p>
<pre><code class="lang-auto">torch::Tensor build_part_with_score_torch(float score_threshold, int local_max_radius, torch::Tensor &amp;scores)
{
   // lmd = 2 * local_max_radius + 1
    int   lmd = 2 * local_max_radius + 1;

   // max_vals = F.max_pool2d(scores, lmd, stride=1, padding=1)
   torch::Tensor   max_vals = torch::max_pool2d(scores, lmd, 1, 1,1,false);
  // max_loc = (scores == max_vals) &amp; (scores &gt;= score_threshold)
   torch::Tensor   max_loc = torch::__and__((scores == max_vals),  (scores &gt;= score_threshold));
 
 // max_loc_idx = max_loc.nonzero()
   torch::Tensor  max_loc_idx = max_loc.nonzero();

   // scores_vec = scores[max_loc];  
   torch::Tensor scores_vec =  torch::randn(max_loc_idx.sizes()[0]);
   for(int i=0; i&lt;max_loc_idx.sizes()[0];i++){
       auto dim1 = max_loc_idx[i][0];
       auto dim2 = max_loc_idx[i][1];
       auto dim3 = max_loc_idx[i][2];
       scores_vec[i] = scores[dim1][dim2][dim3];
    }      
   cout&lt;&lt;"scores_vec"&lt;&lt;endl&lt;&lt;scores_vec&lt;&lt;endl;
   //sort_idx = torch.argsort(scores_vec, descending=True)
   torch::Tensor  sort_idx = torch::argsort(scores_vec,-1, true);
 
 
 //return scores_vec[sort_idx], max_loc_idx[sort_idx]
   torch::Tensor sorted_scores_vec = torch::index_select(scores_vec,0,sort_idx);
   torch::Tensor sorted_max_loc_idx  = torch::index_select(max_loc_idx,0,sort_idx);
   cout&lt;&lt;"sorted_scores_vec"&lt;&lt;endl&lt;&lt;sorted_scores_vec&lt;&lt;endl;
   cout&lt;&lt;"sorted_max_loc_idx"&lt;&lt;endl&lt;&lt;sorted_max_loc_idx&lt;&lt;endl;
  return sorted_scores_vec,sorted_max_loc_idx;

}


torch::Tensor decode_multiple_poses(torch::Tensor&amp; scores,torch::Tensor&amp; offsets,torch::Tensor&amp; displacements_fwd,torch::Tensor&amp; displacements_bwd,int max_pose_detections=10,float score_threshold=0.5,int nms_radius=20, float min_pose_score=0.5)
{
  
   torch::Tensor  part_scores, part_idx = build_part_with_score_torch(score_threshold, LOCAL_MAXIMUM_RADIUS, scores); 
  cout&lt;&lt;"part_scores"&lt;&lt;endl&lt;&lt;part_scores&lt;&lt;endl&lt;&lt;"part_idx"&lt;&lt;endl&lt;&lt;part_idx&lt;&lt;endl;
}
</code></pre>
<p>here is the output of the code</p>
<pre><code class="lang-auto">scores_vec
 0.9944
 0.9816
 0.9857
 0.6776
 0.8208
 0.9903
 0.9785
 0.8932
 0.9345
 0.8307
 0.6399
 0.5537
[ Variable[CPUFloatType]{12} ]
sorted_scores_vec
 0.9944
 0.9903
 0.9857
 0.9816
 0.9785
 0.9345
 0.8932
 0.8307
 0.8208
 0.6776
 0.6399
 0.5537
[ Variable[CPUFloatType]{12} ]
sorted_max_loc_idx
  0   2   6
  5   5   8
  2   1   6
  1   2   7
  6   5   3
  8   8   2
  7  10   9
  9  10   8
  4   2   5
  3   3   7
 10  10   1
 12  10   3
[ Variable[CPULongType]{12,3} ]
part_scores
[ Tensor (undefined) ]
part_idx
  0   2   6
  5   5   8
  2   1   6
  1   2   7
  6   5   3
  8   8   2
  7  10   9
  9  10   8
  4   2   5
  3   3   7
 10  10   1
 12  10   3
[ Variable[CPULongType]{12,3} ]
ok
Segmentation fault (core dumped)
</code></pre>

yuyong: <p>Maybe you can use vector or tuple?<br>
Can you tell me your libtorch version?<br>
I have the error in my libtorch version.<br>
error: ‘class at::Tensor’ has no member named ‘argsort’</p>
<p>Thk you</p>

backprop436: <p>Your function is returning two tensors and not one. To return multiple tensors in C++ return tuple of tensors as follows</p>
<pre><code class="lang-cpp">// c++14
std::tuple&lt;torch::Tensor, torch::Tensor&gt; foo(const torch::Tensor&amp; a, const torch::Tensor&amp; b)
{
  torch::Tensor op1 = a + b / a - b;
  torch::Tensor op2 = torch::randn({1}) * op1;

  return std::make_tuple(op1, op2); 
}

// On the calling side
torch::Tensor tensor1, tensor2;
std::tie(tensor1, tensor2) = foo(torch::randn({3, 2, 2}), torch::randn({3, 2, 2}));

</code></pre>