weird problem with Pytorch's mse_loss function
Traceback (most recent call last):
  File "c:/Users/levin/Desktop/programming/nn.py", line 208, in &lt;module&gt;
    agent.train(BATCHSIZE)
  File "c:/Users/levin/Desktop/programming/nn.py", line 147, in train
    output = F.mse_loss(prediction, target)
  File "C:\Users\levin\Anaconda3\lib\site-packages\torch\nn\functional.py", line 2203, in mse_loss
    if not (target.size() == input.size()):
AttributeError: 'NoneType' object has no attribute 'size'


This above is the Error that I'm constantly getting and I really don't know how to fix it.

This some code that might be important

    def train(self, BATCHSIZE):
        trainsample = random.sample(self.memory, BATCHSIZE)

        for state, action, reward, new_state, gameovertemp in trainsample:
            if gameovertemp:
                target = torch.tensor(reward).grad_fn
            else:
                target = reward + self.gamma * torch.max(self.dqn.forward(new_state))

            self.dqn.zero_grad()
            prediction = torch.max(self.dqn.forward(state))
            #print(prediction, "prediction")
            #print(target, "target")
            output = F.mse_loss(prediction, target)
            output.backward()
            self.optimizer.step()


As stated in a comment the error due to either target of input to be None and is not related to the size() attribute.

The problem is probably at this line:

target = torch.tensor(reward).grad_fn


Here you convert reward to a new Tensor. However, a Tensor created by the user always has a grad_fn equal to None (as explained in Pytorch Autograd).

To have a grad_fn a Tensor must be the result of some computation, not a static value.



The thing is that mse_loss does not expect target to be differentiable, as the name suggest it is just the value to be compared.

Try to remove the grad_fn from this line the raw Tensor should be sufficient.
