Updating the code for newer Pytorch and getting RuntimeError: Tensors must have same number of dimensions: got 2 and 1

Kosh: <p>I am studying AI and now I am trying to understand and update a code which was made for older version of Pytorch. I have tried it for versions 1.0 &amp; 1.5 with similar results. Variable has been deprecated and when creating a tensor</p>
<blockquote>
<p>requires_grad=True</p>
</blockquote>
<p>should be used. Also I was advised that instead of</p>
<blockquote>
<p>torch.Tensor</p>
</blockquote>
<p>, which is an alias of</p>
<blockquote>
<p>torch.FloatTensor</p>
</blockquote>
<p>, I should use</p>
<blockquote>
<p>torch.tensor</p>
</blockquote>
<p>, which should automatically determine the data type.</p>
<p>But there are errors which I am not able to handle:</p>
<blockquote>
<p>return map(lambda x: Variable(torch.cat(x, 0)), samples) <span class="hashtag-raw">#putting</span> the samples in to pytorch variable</p>
</blockquote>
<p>as well as when I remove  <strong>Variable</strong></p>
<blockquote>
<p>return map(lambda x: torch.cat(x, 0), samples)</p>
</blockquote>
<p>gets the same error:</p>
<p><strong>RuntimeError: Tensors must have same number of dimensions: got 2 and 1</strong></p>
<p>Code is:</p>
<blockquote>
<p>import numpy as np<br>
import random<br>
import os<br>
import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
import torch.optim as optim<br>
import torch.autograd as autograd<br>
from torch.autograd import Variable</p>
<p>class Network(nn.Module):  # Creating the architecture of the Neural Network</p>
<pre><code>def __init__(self, input_size, nb_action):
    super(Network, self).__init__()  #to use all tools from nn.Module
    self.input_size = input_size
    self.nb_action = nb_action
    self.fc1 = nn.Linear(input_size, 30) #creating the full connection between input &amp; hidden layer
    self.fc2 = nn.Linear(30, nb_action)

def forward(self, state):   #forward propagation
    x = F.relu(self.fc1(state))
    q_values = self.fc2(x)
    return q_values
</code></pre>
<p>class ReplayMemory(object): # Implementing Experience Replay</p>
<pre><code>def __init__(self, capacity):
    self.capacity = capacity  #maximum number of events in memory
    self.memory = []

def push(self, event):  #append new event in to the memory up to a maximum memory size
    self.memory.append(event)
    if len(self.memory) &gt; self.capacity:
        del self.memory[0]

def sample(self, batch_size):   #take random samples form the memory
    # if list=((1,2,3),(4,5,6)) then zip(*list)=((1,4),(2,5),(3,6))
    # events = (state,action,reward) we need (state1,state2), (action1,action2), (reward1,reward2)
    samples = zip(*random.sample(self.memory, batch_size))
    # torch.cat aligns everything as (state, action, reward)
    # OLD-&gt; return map(lambda x: Variable(torch.cat(x, 0)), samples) #putting samples in a pytorch variable
    return map(lambda x: torch.cat(x, 0), samples) #&lt;-NEW    #putting samples in a pytorch variable
</code></pre>
<p>class Dqn():  # Implementing Deep Q Learning</p>
<pre><code>def __init__(self, input_size, nb_action, gamma):
    self.gamma = gamma
    self.reward_window = []
    self.model = Network(input_size, nb_action)
    self.memory = ReplayMemory(100000)   #memory capacity
    self.optimizer = optim.Adam(self.model.parameters(), lr = 0.001)
</code></pre>
<p>#OLD-&gt; self.last_state = torch.Tensor(input_size).unsqueeze(0)<br>
self.last_state = torch.tensor(input_size, requires_grad=True).unsqueeze(0) #&lt;-NEW<br>
self.last_action = 0<br>
self.last_reward = 0</p>
<pre><code>def select_action(self, state):
    with torch.no_grad():
        probs = F.softmax(self.model(state), dim=1)*100 # T=100
        action = probs.multinomial(num_samples=1)
        return action.data[0,0]

# obsolete
#    def select_action(self, state):
#        probs = F.softmax(self.model(Variable(state, volatile = True))*100) # T=100
#        action = probs.multinomial()
#        return action.data[0,0]        




def learn(self, batch_state, batch_next_state, batch_reward, batch_action):
    outputs = self.model(batch_state).gather(1, batch_action.unsqueeze(1)).squeeze(1)
    next_outputs = self.model(batch_next_state).detach().max(1)[0]
    target = self.gamma*next_outputs + batch_reward
    td_loss = F.smooth_l1_loss(outputs, target)
    self.optimizer.zero_grad()
    td_loss.backward(retain_graph = True)
    self.optimizer.step()

def update(self, reward, new_signal):
    #OLD-&gt; new_state = torch.Tensor(new_signal).float().unsqueeze(0)
    new_state = torch.tensor(new_signal, requires_grad=True, dtype=torch.float).float().unsqueeze(0)   #&lt;-NEW

    self.memory.push((self.last_state, new_state, torch.LongTensor([int(self.last_action)]), torch.Tensor([self.last_reward])))
    action = self.select_action(new_state)
    if len(self.memory.memory) &gt; 100:
        batch_state, batch_next_state, batch_action, batch_reward = self.memory.sample(100)
        self.learn(batch_state, batch_next_state, batch_reward, batch_action)
    self.last_action = action
    self.last_state = new_state
    self.last_reward = reward
    self.reward_window.append(reward)
    if len(self.reward_window) &gt; 1000:
        del self.reward_window[0]
    return action

def score(self):
    return sum(self.reward_window)/(len(self.reward_window)+1.)

def save(self):
    torch.save({'state_dict': self.model.state_dict(),
                'optimizer' : self.optimizer.state_dict(),
               }, 'last_brain.pth')

def load(self):
    if os.path.isfile('last_brain.pth'):
        print("=&gt; loading checkpoint... ")
        checkpoint = torch.load('last_brain.pth')
        self.model.load_state_dict(checkpoint['state_dict'])
        self.optimizer.load_state_dict(checkpoint['optimizer'])
        print("done !")
    else:
        print("no checkpoint found...")
</code></pre>
</blockquote>

albanD: <p>Hi,</p>
<p>Not sure what is the goal of this line?<br>
The error happens because the elements in <code>x</code> don’t have the same number of dimensions: some are 1D and others 2D which cat cannot handle.</p>

Renan_Fagundes: <p>This code was write to run with Pytorch=0.3.1.<br>
I’m studyng this code too.</p>