Size Mismatch when passing a state batch to network

username: SUMMERSON
cooked: <p>Hi all. Since I’m a beginner in ML, this question or the design overall may sound silly, sorry about that. I’m open to any suggestions.</p>
<p>I have a simple network with three linear layers one of which is output layer.</p>
<pre><code>    self.fc1 = nn.Linear(in_features=2, out_features=12)
	self.fc2 = nn.Linear(in_features=12, out_features=16)
	self.out = nn.Linear(in_features=16, out_features=4)
</code></pre>
<p>My states are consisting of two values, coordinate x and why.  That’s why input layer has two features.</p>
<p>In main.py I’m sampling and extracting memories in ReplayMemory class and pass them to get_current function:</p>
<pre><code>        experiences = memory.sample(batch_size)
		states, actions, rewards, next_states = qvalues.extract_tensors(experiences)

		current_q_values = qvalues.QValues.get_current(policy_net, states, actions)
</code></pre>
<p>Since a single state is consisting of two values, length of the states tensor is batchsize x 2 while length of the actions is batchsize. (Maybe that’s the problem?)</p>
<p>When I pass “states” to my network in get_current function to obtain predicted q-values for the state, I get this error:</p>
<p><strong>size mismatch, m1: [1x16], m2: [2x12]</strong></p>
<p>It looks like it is trying to grab the states tensor as if it is a single state tensor. I don’t want that. In the tutorials that I follow, they pass the states tensor which is a stack of multiple states, and there is no problem. What am I doing wrong? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>This is how I store an experience:</p>
<pre><code class="lang-auto">memory.push(dqn.Experience(state, action, next_state, reward))
</code></pre>
<p>This is my extract tensors function:</p>
<pre><code class="lang-auto">def extract_tensors(experiences):
    # Convert batch of Experiences to Experience of batches
    batch = dqn.Experience(*zip(*experiences))

    state_batch = torch.cat(tuple(d[0] for d in experiences))
    action_batch = torch.cat(tuple(d[1] for d in experiences))
    reward_batch = torch.cat(tuple(d[2] for d in experiences))
    nextState_batch = torch.cat(tuple(d[3] for d in experiences))

    print(action_batch)

    return (state_batch,action_batch,reward_batch,nextState_batch)
</code></pre>
<p>Tutorial that I follow is this project’s tutorial.</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/nevenp/dqn_flappy_bird/blob/master/dqn.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/nevenp/dqn_flappy_bird/blob/master/dqn.py" target="_blank" rel="nofollow noopener">nevenp/dqn_flappy_bird/blob/master/dqn.py</a></h4>
<pre><code class="lang-py">import os
import random
import sys
import time

import cv2
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim

from game.flappy_bird import GameState


class NeuralNetwork(nn.Module):

    def __init__(self):
        super(NeuralNetwork, self).__init__()

        self.number_of_actions = 2
</code></pre>

  This file has been truncated. <a href="https://github.com/nevenp/dqn_flappy_bird/blob/master/dqn.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>Look between 148th and 169th lines. And especially 169th line where it passes the states batch to the network.</p>

username: SUMMERSON
cooked: <p>SOLVED. It turned out that I didn’t know how to properly create 2d tensor. 2D Tensor must be like this:</p>
<p>states = torch.tensor([[1, 1], [2,2]], dtype=torch.float)</p>