Create a 2D tensor with laggged/window values from a 1D tensor time series

username: maggette
cooked: <p>Hi,<br>
Sorry for the vague question.</p>
<p><strong>I have about a dozend implementations of that in numpy. So I am not looking for “one possible way to do it”.</strong></p>
<p>But I know that I found somewhere on the internet ( I think it was either here or on StackOverflow) a beautiful “one liner” solution in PyTorch and thought “awesome, will try that later”. And I can’t find it again even after an hour googling or going through my history.</p>
<p>Problem:<br>
given a1D tensor/array (time series) and a desired lag/window length (let’s say window length = 2)<br>
[x1, x2, x3, x4…]</p>
<p>I want to get to a 2D tensor, each row the current value and the n lags:<br>
[x1,x2,x3]<br>
[x2,x3,x4]<br>
[x3,x4,x5]<br>
…<br>
There are oviously many ways to do that. But I think I recall it was either a single function or maybe a clever one-line combination of reshape and other functions. But I think it was a single function call.</p>
<p>It drives me mad that I can’t find the particular post again. Anybody an idea?<br>
Thanks!!</p>

username: KFrank
cooked: <p>Hi Mag!</p>
<aside class="quote no-group" data-username="maggette" data-post="1" data-topic="147042" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/3ec8ea/48.png" class="avatar"> maggette:</div>
<blockquote>
<p>I want to get to a 2D tensor, each row the current value and the n lags:<br>
[x1,x2,x3]<br>
[x2,x3,x4]<br>
[x3,x4,x5]</p>
</blockquote>
</aside>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.10.2'
&gt;&gt;&gt; _ = torch.manual_seed (2022)
&gt;&gt;&gt; n = 3
&gt;&gt;&gt; t = torch.randint (100, (20,))
&gt;&gt;&gt; t
tensor([41, 40, 73, 76, 49, 37, 84, 75, 24,  6, 48, 92, 37, 69, 41, 47, 75, 79,
        42, 86])
&gt;&gt;&gt; t[torch.arange (n) + torch.arange (len (t) + 1 - n).unsqueeze (1)]
tensor([[41, 40, 73],
        [40, 73, 76],
        [73, 76, 49],
        [76, 49, 37],
        [49, 37, 84],
        [37, 84, 75],
        [84, 75, 24],
        [75, 24,  6],
        [24,  6, 48],
        [ 6, 48, 92],
        [48, 92, 37],
        [92, 37, 69],
        [37, 69, 41],
        [69, 41, 47],
        [41, 47, 75],
        [47, 75, 79],
        [75, 79, 42],
        [79, 42, 86]])
</code></pre>
<p>Best.</p>
<p>K. Frank</p>

username: maggette
cooked: <p>Hi Frank,<br>
thanks for that. That is pretty close to  my numpy/torch implementations. So you don’t know of any magic function that does torch.magic(tensor, windowlength) to produce that? That is what I am looking for,.</p>

username: ptrblck
cooked: <p>Maybe you are thinking about <code>unfold</code>:</p>
<pre><code class="lang-python">x = torch.arange(10)
y = x.unfold(dimension=0, size=3, step=1)
print(y)
# tensor([[0, 1, 2],
#         [1, 2, 3],
#         [2, 3, 4],
#         [3, 4, 5],
#         [4, 5, 6],
#         [5, 6, 7],
#         [6, 7, 8],
#         [7, 8, 9]])
</code></pre>
<p>or with <a class="mention" href="/u/kfrank">@KFrank</a>’s values:</p>
<pre><code class="lang-python">torch.manual_seed(2022)
t = torch.randint (100, (20,))
print(t.unfold(dimension=0, size=3, step=1))
# tensor([[41, 40, 73],
#         [40, 73, 76],
#         [73, 76, 49],
#         [76, 49, 37],
#         [49, 37, 84],
#         [37, 84, 75],
#         [84, 75, 24],
#         [75, 24,  6],
#         [24,  6, 48],
#         [ 6, 48, 92],
#         [48, 92, 37],
#         [92, 37, 69],
#         [37, 69, 41],
#         [69, 41, 47],
#         [41, 47, 75],
#         [47, 75, 79],
#         [75, 79, 42],
#         [79, 42, 86]])
</code></pre>

username: maggette
cooked: <p>Aahhhhhh.</p>
<p>You know the feeling if you hear an actors voice and can’t put a face to it, or you are humming a song and can’t really remember what song it was? And it is driving you crazy?</p>
<p>Thanks man. That was exactly what I was looking for, Don’t know how I missed while going through the tensor API documentation.</p>
<p>Thansk sooo much mate!<br>
Regards<br>
Phil</p>