How to use the Tensor::accessor

markl: <p>Reading the documentation <a href="https://pytorch.org/cppdocs/notes/tensor_basics.html" rel="nofollow noopener">https://pytorch.org/cppdocs/notes/tensor_basics.html</a> it looks like accessor() takes a template value which specifies the dimensionality. Is this value allowed to be different than the source tensor? For example if I want to access a tensor as a logical 1-d array, but the source tensor is a possibly noncontiguous (coming from a slice operation) n-d tensor, am I allowed to do <code>my_tensor.accessor&lt;float, 1&gt;()</code>?</p>

markl: <p>I think I found the answer from the generated source code <code>Tensor.h</code></p>
<pre><code class="lang-auto">
  // Return a `TensorAccessor` for CPU `Tensor`s. You have to specify scalar type and
  // dimension.
  template&lt;typename T, size_t N&gt;
  TensorAccessor&lt;T,N&gt; accessor() const&amp; {
    static_assert(N &gt; 0, "accessor is used for indexing tensor, for scalars use *data&lt;T&gt;()");
    TORCH_CHECK(dim() == N, "expected ", N, " dims but tensor has ", dim());
    return TensorAccessor&lt;T,N&gt;(data&lt;T&gt;(),sizes().data(),strides().data());
  }
</code></pre>

MrCrHaM: <p>I’m a bit comfused. I apologize if this is kinda noob. But if <code>N</code> is meant to be the same as <code>dim()</code>, what’s the purpose of making <code>N</code> as a parameter in the first place? Why not simply do <code>TensorAccessor&lt;T, dim()&gt;</code> or somehing like that?</p>

leigaoxiang1: <p>auto srcdata = score.accessor&lt;float,3&gt;();</p>
<pre><code>cout &lt;&lt; srcdata[0][0][0] &lt;&lt; endl;
for (int i = 0; i &lt; srcdata.size(0); i++) {
	int max_index = 0;
	float max_value = -1000;
	for (int j = 0; j &lt; srcdata.size(1); j++) {
		
		for (int k =0;k&lt; srcdata.size(2);k++)
		{
			cout &lt;&lt; "maxvalue:" &lt;&lt; srcdata[i][j][k] &lt;&lt; endl;
			if (srcdata[i][j][k] &gt; max_value) {
				max_value = srcdata[i][j][k];
				max_index = k;
			}

		}
</code></pre>
<p>i use accessor as above:but i can not  get the value by cout &lt;&lt;srcdata[i][j][k</p>

Andrei_Pokrovsky: <p>.dim() is a runtime value, you cannot pass it into a template expression which expects a constexpr (constant known at compile time)</p>

Jonas_De_Schouwer: <p>A related question: can one also assign a value using the Tensor::accessor?</p>
<p>E.g.</p>
<pre data-code-wrap="cpp"><code class="lang-cpp">myAccessor[i][j] = 3
</code></pre>
<p>EDIT:<br>
According to this StackOverflow post, it is possible.</p><aside class="onebox stackexchange" data-onebox-src="https://stackoverflow.com/questions/59676983/in-torch-c-api-how-to-write-to-the-internal-data-of-a-tensor-fastly">
  <header class="source">

      <a href="https://stackoverflow.com/questions/59676983/in-torch-c-api-how-to-write-to-the-internal-data-of-a-tensor-fastly" target="_blank" rel="noopener nofollow ugc">stackoverflow.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/9555853/liu-qingyuan" target="_blank" rel="noopener nofollow ugc">
    <img alt="LIU Qingyuan" src="https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=256" class="thumbnail onebox-avatar" width="256" height="256">
  </a>

<h4>
  <a href="https://stackoverflow.com/questions/59676983/in-torch-c-api-how-to-write-to-the-internal-data-of-a-tensor-fastly" target="_blank" rel="noopener nofollow ugc">In Torch C++ API, How to write to the internal data of a tensor fastly?</a>
</h4>

<div class="tags">
  <strong>c++, pytorch, libtorch</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/9555853/liu-qingyuan" target="_blank" rel="noopener nofollow ugc">
    LIU Qingyuan
  </a>
  on <a href="https://stackoverflow.com/questions/59676983/in-torch-c-api-how-to-write-to-the-internal-data-of-a-tensor-fastly" target="_blank" rel="noopener nofollow ugc">07:13AM - 10 Jan 20 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>