Help me... How to resolve error due to tensor size ? Plz

username: 111347
cooked: <p>import gym<br>
import collections<br>
import random</p>
<p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
import torch.optim as optim</p>
<p>learning_rate = 0.0005<br>
gamma         = 0.98<br>
buffer_limit  = 50000<br>
batch_size    = 32</p>
<p>class ReplayBuffer():<br>
def <strong>init</strong>(self):<br>
self.buffer = collections.deque(maxlen=buffer_limit)<br>
def put(self, data):<br>
self.buffer.append(data)<br>
def sample(self, n):<br>
mini_batch = random.sample(self.buffer, n)<br>
s_lst, a_lst, r_lst, s_prime_lst, done_mask_lst = [], [], [], [], []<br>
for transition in mini_batch:<br>
s, a, r, s_prime, done_mask = transition<br>
s_lst.append(s)<br>
a_lst.append([a])<br>
r_lst.append([r])<br>
s_prime_lst.append(s_prime)<br>
done_mask_lst.append([done_mask])</p>
<pre><code>    return torch.tensor(s_lst, dtype=torch.float), torch.tensor(a_lst), \
           torch.tensor(r_lst), torch.tensor(s_prime_lst, dtype=torch.float), \
           torch.tensor(done_mask_lst)

def size(self): 
    return len(self.buffer)
</code></pre>
<p>class Qnet(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Qnet, self).<strong>init</strong>()<br>
self.fc1 = nn.Linear(2, 128)<br>
self.fc2 = nn.Linear(128, 128)<br>
self.fc3 = nn.Linear(128, 1)</p>
<pre><code>def forward(self, x):
    x = F.relu(self.fc1(x)) 
    x = F.relu(self.fc2(x)) 
    x = self.fc3(x)

    return x
def sample_action(self, obs, epsilon):
    out = self.forward(obs)
    coin = random.random()
    if coin &lt; epsilon:
        return random.randint(0,1)
    else : 
        return out.argmax().item()
</code></pre>
<p>def train(q, q_target, memory, optimizer):<br>
for i in range(10):=<br>
s,a,r,s_prime,done_mask = memory.sample(batch_size)<br>
q_out = q(s)<br>
q_a = q_out.gather(1,a)<br>
max_q_prime = q_target(s_prime).max(1)[0].unsqueeze(1)<br>
target = r + gamma * max_q_prime * done_mask<br>
loss = F.smooth_l1_loss(q_a, target)<br>
optimizer.zero_grad()<br>
loss.backward()<br>
optimizer.step()</p>
<p>def main():<br>
env = gym.make(‘Pendulum-v0’)<br>
q = Qnet()<br>
q_target = Qnet() <span class="hashtag">#fix</span><br>
q_target.load_state_dict(q.state_dict())</p>
<pre><code>memory = ReplayBuffer()
print_interval = 20
score = 0.0  
optimizer = optim.Adam(q.parameters(), lr=learning_rate)
render = False

for n_epi in range(10000):
    epsilon = max(0.01, 0.08 - 0.01*(n_epi/200)) 
    s = env.reset()
    done = False

    while not done: 
        a = q.sample_action(torch.from_numpy(s).float(), epsilon)
        s_prime, r, done, info = env.step(a)
        done_mask = 0.0 if done else 1.0
        memory.put((s,a,r/100.0,s_prime, done_mask))
    
        s = s_prime

        score += r
        
        if render:
            env.render()
            
        if done:
            break
            
     
        
    if score/print_interval &gt; 300: 
        render = True

    if memory.size()&gt;2000: 
        train(q, q_target, memory, optimizer)

    if n_epi%print_interval==0 and n_epi!=0:

        q_target.load_state_dict(q.state_dict())
        print("n_episode :{}, score : {:.1f}, n_buffer : {}, eps : {:.1f}%".format(
                                                        n_epi, score/print_interval, memory.size(), epsilon*100))
        
        score = 0.0
env.close()
</code></pre>
<p>if <strong>name</strong> == ‘<strong>main</strong>’:<br>
main()</p>
<p>RuntimeError: size mismatch, m1: [1 x 3], m2: [2 x 128] at C:\w\b\windows\pytorch\aten\src\TH/generic/THTensorMath.cpp:41</p>

username: chetan_patil
cooked: <p>Hi, your <code>self.fc1</code> connects from 2 units to 128 units.<br>
But your input seems to 3 be units.<br>
Either change input to 2, or change <code>self.fc1</code> to<br>
<code>nn.Linear(3,128)</code></p>

username: 111347
cooked: <p>Thank you</p>
<p>So, I changed it to 3. It looks like this.</p>
<p>“IndexError: invalid index to scalar variable.”</p>
<p>What’s the problem?.. Help me…</p>

username: chetan_patil
cooked: <p>Can you share the error completely ?<br>
What did you chage to 3 ? <code>nn.Linear(3,128)</code> ?<br>
If so, verify if your input is of shape <code>(N, 3)</code> .</p>