Tensor of undefined size

username: afshin67
cooked: <p>In <code>C++</code> we can declare a vector and then add undefined number of elements to that by <code>push_back</code> function, e.g.,:</p>
<pre><code class="lang-auto">std::vector&lt;float&gt; v;
for (int i=0; i&lt; n; i++)
  v.push_back(i);
</code></pre>
<p>in which <code>n</code> can be any number. In other words, in the declaration time, I do not need to instantiate <code>std::vector</code> with a known number of elements.</p>
<p>In <code>torch</code>, we I create a tensor, I could not find any <code>torch::tensor</code> that does not need to define the number of elements, and also could not find functions like <code>push_back</code>. Is there such a functionality in torch?</p>

username: LeviViana
cooked: <p>You can always concatenate a tensor with another for as long as you wish, I’ve seen this a lot as an equivalent of <code>push_back</code> in many PyTorch implementations.</p>

username: afshin67
cooked: <p>Thanks, it is a good idea.</p>

username: afshin67
cooked: <p>I implemented this idea, and it turned out that it is very slow.<br>
When I know the size of the tensor and create it with <code>torch::zeros()</code> and fill it in a loop, it is way faster than using <code>torch::cat</code>.<br>
Is there any other way to handle this?</p>

username: LeviViana
cooked: <p>It depends on your application. But say that you have the data you want to pass to your tensor in a array, say <code>int x[4] = {1, 2, 3, 4}</code>, then you can create a tensor by putting <code>x</code> as argument.</p>
<pre><code class="lang-cpp">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

int main(){
	int x[4] = {1, 2, 3, 4};
	torch::Tensor tensor = torch::tensor(x);
	std::cout&lt;&lt; tensor &lt;&lt; std::endl;
	return 0;
}
</code></pre>
<p>It works for <code>std::vector</code> too:</p>
<pre><code class="lang-cpp">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

int main(){
	std::vector&lt;int&gt; x = {1, 2, 3, 4};
	torch::Tensor tensor = torch::tensor(x);
	std::cout&lt;&lt; tensor &lt;&lt; std::endl;
	return 0;
}
</code></pre>
<p>And finally, it works for pointers too:</p>
<pre><code class="lang-cpp">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

int main(){
	int *x = (int *) malloc(4 * sizeof(int));
	for(int i = 0; i &lt; 4; i++){
		x[i] = i;
	}
	torch::Tensor tensor = torch::zeros({4}).to(torch::kInt);
	std::memcpy(tensor.data&lt;int&gt;(), x, 4 * sizeof(int));
	std::cout&lt;&lt; tensor &lt;&lt; std::endl;
	free(x);
	return 0;
}
</code></pre>

username: afshin67
cooked: <p>Thanks for the quick response.<br>
You are right, when the input dimension is an array or 1D vector, there is a overloaded version of <code>torch::tensor</code> that easily creates a tensor from them. But, the problem is that there is not any version of <code>torch::tensor</code> for getting a 2D, 3D, or 4D vector as the input. If you try something like:</p>
<pre><code>std::vector&lt;std::vector&lt;int&gt;&gt; input_2d;
for (int i = 0 ; i &lt;5; i++)
    input_2d.push_back(input_1d);

torch::Tensor state_from_vector_2d = torch::tensor(input_2d);
</code></pre>
<p>you get <code>No matching function for call to 'tensor'</code> error.<br>
Any suggestion?</p>

username: LeviViana
cooked: <p>I don’t know any super optimized way of doing this. But this could be useful:</p>
<pre><code class="lang-cpp">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

int main(){
	std::vector&lt;std::vector&lt;int&gt;&gt; x = {{1, 2}, {3, 4}};
	torch::Tensor tensor = torch::zeros({2, 2}).to(torch::kInt);
	int n = x.size();
	for(int i = 0; i &lt; n; i++){
		std::memcpy(tensor.data&lt;int&gt;() + i * n, &amp;x[i][0], 2 * sizeof(int));
	}
	std::cout&lt;&lt; tensor &lt;&lt; std::endl;
	return 0;
}
</code></pre>
<p>There, you can kind of save a nested for loop, since <code>std::memcpy</code> is very fast.</p>

username: afshin67
cooked: <aside class="quote no-group" data-username="LeviViana" data-post="7" data-topic="47061">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/leviviana/48/9548_2.png" class="avatar"> LeviViana:</div>
<blockquote>
<p>torch::Tensor tensor = torch::zeros({2, 2}).to(torch::kInt);</p>
</blockquote>
</aside>
<p>Yes, this approach works, but you have to know the number of your observations (rows), as you have defined it by <code>torch::Tensor tensor = torch::zeros({2, 2}).to(torch::kInt);</code>.<br>
The initial question was looking for a solution when we do not know the number of rows.</p>

username: LeviViana
cooked: <p>If you don’t have any information on how your tensor will look like at compile time, I don’t know how you can highly optimize this operation. If it becomes a bottleneck, I guess it could be useful to factorize some parts of your code, so that you could potentially have some prior information on the inputs.</p>