Type conversion in Libtorch for C++

arslansadiq: <p>Hi all,<br>
I am trying to do inference for my model in C++ using Libtorch.<br>
Following is the code which I am using.</p>
<pre><code class="lang-auto">        torch::Tensor inputs = torch::zeros({in_1.size()*2, 1, 2}, torch::dtype(torch::kFloat32));
        int i = 0;
        const cellList &amp;cells = mesh.cells();
        forAll(cells, celli)
        {
            inputs[i][0][0] = in_1[celli];
            inputs[i][0][1] = in_2[celli];
	    i = i + 1;
        }

	    at::Tensor output_real = torch::zeros({in_1.size()*2,1,7}, torch::dtype(torch::kFloat32));
	    std::vector&lt;torch::jit::IValue&gt; input;

	    //* * * * * * * Inference * * * * * * * *//

	    for(int j = 0; j&lt;i; j++)
		{
			input.push_back(inputs[j]);
	        output_real[j] = module-&gt;forward(input).toTensor();
	        input.clear();
		}
	    std::cout&lt;&lt;"Inference has been completed"&lt;&lt;'\n';
	
	    int j = 0;
	
	    forAll(cells, celli)
		{
			if(j != i){
				out_1[celli] = output_real[j][0][celli * 7];
				out_2[celli] = output_real[j][0][celli * 7 + 1];
				out_3[celli] = output_real[j][0][celli * 7 + 2];
				out_4[celli] = output_real[j][0][celli * 7 + 3];
				out_5[celli] = output_real[j][0][celli * 7 + 4];
				out_6[celli] = output_real[j][0][celli * 7 + 5];
				out_7[celli] = output_real[j][0][celli * 7 + 6];
			}
			j = j+1;
        }
</code></pre>
<p>in this code neural network’s input is <code>1x2</code> dim and output is <code>1x7</code>.<br>
Now the problem is in the last loop it gives error of type conversion which is as follows:</p>
<pre><code class="lang-auto">inferFoam.C: In function ‘int main(int, char**)’:
inferFoam.C:95:46: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
    out_1[celli] = output_real[j][0][celli * 7];
                                              ^
inferFoam.C:96:59: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
             out_2[celli] = output_real[j][0][celli * 7 + 1];
                                                           ^
inferFoam.C:97:59: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
             out_3[celli] = output_real[j][0][celli * 7 + 2];
                                                           ^
inferFoam.C:98:59: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
             out_4[celli] = output_real[j][0][celli * 7 + 3];
                                                           ^
inferFoam.C:99:50: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
    out_5[celli] = output_real[j][0][celli * 7 + 4];
                                                  ^
inferFoam.C:100:56: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
          out_6[celli] = output_real[j][0][celli * 7 + 5];
                                                        ^
inferFoam.C:101:56: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
          out_7[celli] = output_real[j][0][celli * 7 + 6];
                                                        ^
</code></pre>
<p>I tried explicit conversion like <code>(double)output_real[j][0][celli * 7 + 5]</code> but it doesn’t seem to work and says:</p>
<pre><code class="lang-auto">error: invalid cast from type ‘at::Tensor’ to type ‘double’
      std::cout &lt;&lt; (double)output[i][0][1] &lt;&lt; '\n';
                                         ^
</code></pre>
<p>Can anybody please tell me what’s the solution to this problem is?</p>
<p>Secondly, I am confused that, earlier in the first loop when I am reading the input values from <code>in_1[celli]</code> and <code>in_2[celli]</code> into <code>inputs</code> it doesn’t give this kind of (type conversion) error there even though there also I am doing the same thing; exchanging two different datatypes.</p>
<p>Can anybody tell me please what’s seems to the problem, what is my mistake and also please suggest a solution.</p>
<p>Regards.</p>

CoderHam: <p>torch::Double can’t be casted to <strong>double</strong> directly, you should use <code>output_tensor[i][j][k].item().to&lt;double&gt;()</code>. It first converts a Tensor with a single value to a <strong>ScalarType</strong> then to a <strong>double</strong>.</p>

arslansadiq: <p>Thank you so much, it works <img src="https://discuss.pytorch.org/images/emoji/apple/blush.png?v=9" title=":blush:" class="emoji" alt=":blush:">.</p>

Edwardmark: <p>Hi，<br>
Say we have two tensor priors, and loc_cat,<br>
I want to set the type of priors same as loc_cat,<br>
in python, we can use the following code:</p>
<p>priors.type(loc_cat.data.type)</p>
<p>So how to do that in cpp libtorch?<br>
Thanks.</p>

MAsad: <p>I was looking for exactly the same. For anyone looking at this in future, here is how you can cast one tensor with dtype of another in C++:</p>
<pre><code class="lang-auto">priors.type_as(loc_cat)
</code></pre>