Why does he use Variable here?

L_R: <p>Sorry I’m very new to deep learning and PyTorch. I’m looking into some codes about LSTM with pytorch.</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://colab.research.google.com/github/dlmacedo/starter-academic/blob/master/content/courses/deeplearning/notebooks/pytorch/Time_Series_Prediction_with_LSTM_Using_PyTorch.ipynb#scrollTo=a2vvHeFgVtCp">
  <header class="source">
      <img src="https://colab.research.google.com/img/favicon.ico?vrz=colab-20220616-060045-RC00_455344893" class="site-icon" width="128" height="128">

      <a href="https://colab.research.google.com/github/dlmacedo/starter-academic/blob/master/content/courses/deeplearning/notebooks/pytorch/Time_Series_Prediction_with_LSTM_Using_PyTorch.ipynb#scrollTo=a2vvHeFgVtCp" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>

  <article class="onebox-body">
    <img src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar" width="260" height="260">

<h3><a href="https://colab.research.google.com/github/dlmacedo/starter-academic/blob/master/content/courses/deeplearning/notebooks/pytorch/Time_Series_Prediction_with_LSTM_Using_PyTorch.ipynb#scrollTo=a2vvHeFgVtCp" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>

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

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

<p>Why does he use Variable in</p>
<pre><code class="lang-auto">dataX = Variable(torch.Tensor(np.array(x)))
dataY = Variable(torch.Tensor(np.array(y)))

trainX = Variable(torch.Tensor(np.array(x[0:train_size])))
trainY = Variable(torch.Tensor(np.array(y[0:train_size])))

testX = Variable(torch.Tensor(np.array(x[train_size:len(x)])))
testY = Variable(torch.Tensor(np.array(y[train_size:len(y)])))
</code></pre>
<p>and</p>
<pre><code class="lang-auto">def forward(self, x):
        h_0 = Variable(torch.zeros(
            self.num_layers, x.size(0), self.hidden_size))
        
        c_0 = Variable(torch.zeros(
            self.num_layers, x.size(0), self.hidden_size))
</code></pre>
<p>According to this question, <a href="https://stackoverflow.com/questions/44429199/how-to-load-a-list-of-numpy-arrays-to-pytorch-dataset-loader" class="inline-onebox" rel="noopener nofollow ugc">python - How to load a list of numpy arrays to pytorch dataset loader? - Stack Overflow</a>, you should use <code>TensorDataset</code> to convert a list of 2d arrays into pytorch inputs, why does he use Variable here?</p>
<p>Besides, why does he use Variable in forward function? Is torch.zeros not enough?</p>
<p>The second question is that, when I try to run this code, I got this error, ‘RuntimeError: Input and parameter tensors are not at the same device, found input tensor at cuda:0 and parameter tensor at cpu’. How should I fix this? Which lines should I add <code>.to(device)</code> command?</p>
<p>I added .cuda() as follows:</p>
<pre><code class="lang-auto">dataX = Variable(torch.Tensor(np.array(x))).cuda()
dataY = Variable(torch.Tensor(np.array(y))).cuda()

trainX = Variable(torch.Tensor(np.array(x[0:train_size]))).cuda()
trainY = Variable(torch.Tensor(np.array(y[0:train_size]))).cuda()

testX = Variable(torch.Tensor(np.array(x[train_size:len(x)]))).cuda()
testY = Variable(torch.Tensor(np.array(y[train_size:len(y)]))).cuda()
</code></pre>
<p>and</p>
<pre><code class="lang-auto">def forward(self, x):
        h_0 = Variable(torch.zeros(
            self.num_layers, x.size(0), self.hidden_size)).cuda()
        
        c_0 = Variable(torch.zeros(
            self.num_layers, x.size(0), self.hidden_size)).cuda()
</code></pre>
<p>but still got this error.</p>

ptrblck: <p>Don’t use the <code>Variable</code> class anymore, as it’s deprecated since PyTorch <code>0.4</code> (we are about to release <code>1.12.0</code> soon so <code>0.4</code> is “quite old” <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"> ).</p>
<aside class="quote no-group" data-username="L_R" data-post="1" data-topic="154438">
<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/l_r/48/30837_2.png" class="avatar"> L_R:</div>
<blockquote>
<p>Besides, why does he use Variable in forward function? Is torch.zeros not enough?</p>
</blockquote>
</aside>
<p><code>torch.zeros</code> would work in newer PyTorch releases and you should just remove the usage of <code>Variable</code> entirely from the script.</p>
<aside class="quote no-group" data-username="L_R" data-post="1" data-topic="154438">
<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/l_r/48/30837_2.png" class="avatar"> L_R:</div>
<blockquote>
<p>How should I fix this? Which lines should I add <code>.to(device)</code> command?</p>
</blockquote>
</aside>
<p>Make sure all (input) tensors as well as the model parameters are on the same device.</p>

L_R: <p>Thank you very much! I fixed all the forementioned problems.</p>
<p>The training and testing data are 3d tensors with the first dimension being sample size. Does that mean I can directly put in a 3d tensor with the first dimension being sample size without using <code>DataLoader</code> as inputs?</p>
<p>Because I’m more familiar with keras, where you can directly put in 3d arrays as inputs with first dimension being sample size.</p>

ptrblck: <p>Yes, you can directly execute the forward pass if you pass the input tensor in the expected shape to the model. By default the batch dimension would be <code>dim0</code>, but be careful about RNNs as they use <code>dim1</code> as the default dimension for the batch size (you could use <code>batch_first=True</code> to change this behavior, but check the docs for these layers for more information).</p>
<p>The <code>DataLoader</code> will batch the samples for you and is a convenient method to use a <code>Dataset</code> which loads a single sample only without worrying about batching, shuffling etc.</p>