Convert c10::Scalar to int

Albert_Christianto: <p>Hi, I have an output tensor come from my network.</p>
<pre><code class="lang-auto">void LPRNet&lt;SpTDatum&gt;::PostProcess(std::vector&lt;SpTDatum&gt;&amp; the_datas, torch::Tensor &amp; model_outputs)
	{
		model_outputs = model_outputs.to(torch::kCPU);//make the variable is in CPU

		for (int i = 0; i &lt; the_datas.size(); ++i)
		{ //each batch
			for (int j = 0; j &lt; the_datas[i]-&gt;results-&gt;size(); ++j)
			{ //each detected LP
				if (the_datas[i]-&gt;results-&gt;at(j).HaveLp)
				{
					//process the tensor data
					std::string LpLabel = "";
					int last_char = -1;
					int current_char = -1;
					for (int k = 0; k &lt; model_outputs[the_datas[i]-&gt;results-&gt;at(j).QueueNumber].size(0); ++k)
					{
						//each character
						current_char = model_outputs[the_datas[i]-&gt;results-&gt;at(j).QueueNumber][k].item&lt;int&gt;();
						if ((last_char == current_char) || (mBlankIdx == current_char)) {
							last_char = current_char;
							continue;
						}
						LpLabel += mDisplayChar[current_char];//convert the index value to character value
						last_char = current_char;
					}
					the_datas[i]-&gt;results-&gt;at(j).LpLabel = LpLabel;
				}
			}
		}
		//program end here!
	}
</code></pre>
<p>This program works fine and is compiled in the MSVC. However when I use GCC 7.5 to build, i got this error message</p>
<pre><code class="lang-auto">[  1%] Building CXX object LPDR/CMakeFiles/lpdr.dir/src/LPDR/lprNet.cpp.o
/media/ion/ioNdrive/lpdr_beta/LPDR/src/LPDR/lprNet.cpp: In member function ‘void LPDR::LPRNet&lt;SpTDatum&gt;::PostProcess(std::vector&lt;_Tp&gt;&amp;, at::Tensor&amp;)’:
/media/ion/ioNdrive/lpdr_beta/LPDR/src/LPDR/lprNet.cpp:183:86: error: expected primary-expression before ‘int’
       current_char = model_outputs[the_datas[i]-&gt;results-&gt;at(j).QueueNumber][k].item&lt;int&gt;();
                                                                                      ^~~
/media/ion/ioNdrive/lpdr_beta/LPDR/src/LPDR/lprNet.cpp:183:86: error: expected ‘;’ before ‘int’
LPDR/CMakeFiles/lpdr.dir/build.make:285: recipe for target 'LPDR/CMakeFiles/lpdr.dir/src/LPDR/lprNet.cpp.o' failed
make[2]: *** [LPDR/CMakeFiles/lpdr.dir/src/LPDR/lprNet.cpp.o] Error 1
CMakeFiles/Makefile2:205: recipe for target 'LPDR/CMakeFiles/lpdr.dir/all' failed
make[1]: *** [LPDR/CMakeFiles/lpdr.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2
</code></pre>
<p>What is the other options to convert c10::Scalar to int(or other data type such as double, float, etc)<br>
best regards,<br>
albert</p>

Albert_Christianto: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> would you help me, please?</p>

ptrblck: <p>Could you check what is returned by <code>model_outputs[the_datas[i]-&gt;results-&gt;at(j).QueueNumber][k]</code> as I’m currently unsure what’s raising:<br>
<code>error: expected primary-expression before ‘int’</code>.</p>

Albert_Christianto: <p>It is a tensor (<em>CPULongType</em>).<br>
Edit:<br>
A single tensor of an tensor output (*model_outputs *).<br>
<em>model_outputs</em> have dimensions [batch x num_detection x 15].<br>
I want to convert it into <em>int</em>.<br>
I sensed a GCC version problem.<br>
I have solved the problem. In GCC we can not do this</p>
<pre><code class="lang-auto">current_char = model_outputs[the_datas[i]-&gt;results-&gt;at(j).QueueNumber][k].item&lt;int&gt;();
</code></pre>
<p>We must do this</p>
<pre><code class="lang-auto">at::Tensor tmp = model_outputs[the_datas[i]-&gt;results-&gt;at(j).QueueNumber][k]
current_char =tmp.item&lt;int&gt;();
</code></pre>
<p><a class="mention" href="/u/ptrblck">@ptrblck</a>  Thank you very much for your time.</p>

leimao: <p>Thanks for identifying this bug.</p>