Is there a convenience function for forecasting `n` timesteps ahead?

krishnab: <p>I was just wondering if there is an existing convenience function in Pytorch for forecasting <code>n</code> timesteps ahead, given a model.</p>
<p>For example in the <code>R</code> forecast package, you can fit a model and then indicate <code>forecast(h=10)</code> and it will forecast the next 10 timesteps given the model.</p>
<p>The algorithm itself is very simple–here is a toy example. Say you have a model based on 7 days inputs and you predict the next 3 days as output. Further, say you want to predict the next year, so 365 days. The you would use the last 7 input values from your dataset and predict the next 3 days–meaning you now have the first 3 days of the 365. Then you would append these predictions to the end of your input dataset and predict the subsequent 3 days. So now you have 6 days out of your 365 day prediction. So you would do this predict-and-append loop for 365//3 = 122 timesteps (or perhaps just 121 steps) and obtain your predictions for the full 365.</p>
<p>Now writing the loop is not too difficult, given the algorithm above. But instead of reinventing the wheel, I figured I would check if there is already some sort of convenience function for this in Pytorch. Also, I imagine that I could probably run this prediction loop on the CPU instead of having to push to the GPU each time–that might save some of the data transfer and improve the speed, depending on data volume and number of prediction steps, etc?</p>
<p>Thanks.</p>

albanD: <p>Hi,</p>
<p>I am not very familiar with the forecast package but I don’t think we have anything similar in the core lib no.<br>
The main reason being that this kind of forecasting can be done quite differently depending on the model you have.</p>
<blockquote>
<p>Also, I imagine that I could probably run this prediction loop on the CPU instead of having to push to the GPU</p>
</blockquote>
<p>Not sure what you mean here? Do you have a code sample?<br>
Can’t you push things to the GPU once at the beginning? Or keep everything on the CPU and run it there?</p>

krishnab: <p><a class="mention" href="/u/alband">@albanD</a> Thanks for the info. Yeah, that makes sense. You are totally right that this kind of forecasting can be handled differently depending on the scenario, but I figured I would check anyway. Like I said, it is not that hard to implement. I guess my only question would be if there is an easy way to shift the elements in a tensor instead of having to create a copy of the original tensor with shifted elements each time. Like could I use the <code>torch.roll()</code> function, and then replace say the last 3 elements. Or does <code>roll()</code> also create copies.</p>
<p>I can think about the GPU thing. I am still new to Pytorch, so still just trying to figure out the balance between CPU and GPU. But you are right, it is probably best to just run the prediction routine on the GPU and then pull back the final results.</p>
<p>Thanks again for your input and very prompt response :).</p>

albanD: <aside class="quote no-group" data-username="krishnab" data-post="3" data-topic="93244">
<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/k/b487fb/48.png" class="avatar"> krishnab:</div>
<blockquote>
<p>Or does <code>roll()</code> also create copies.</p>
</blockquote>
</aside>
<p>It does create a copy IIRC.<br>
But if you need to add new elements at the end anyways, you will have to use something like <code>torch.cat()</code> that does a copy. So you can simply do something like `base = torch.cat([base[3:], new_el], dim=0) to just do a single copy.</p>