Unable to make predictions due to incompatible matrix shape/size
I am trying to make a model to predict insurance cost based on the individual. And this is the code for it.
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, TensorDataset

import numpy as np
import pandas as pd

from LSR import ListSearchReplace as LSR

csv = pd.read_csv(&quot;main.csv&quot;)
partialInputs = csv[[&quot;age&quot;, &quot;bmi&quot;, &quot;children&quot;]]
smoker, sex = list(csv[&quot;smoker&quot;]), list(csv[&quot;sex&quot;])

L1 = LSR(smoker)
L1.replace(&quot;yes&quot;, 1, True)
L1.replace(&quot;no&quot;, 0, True)

L2 = LSR(sex)
L2.replace(&quot;female&quot;, 1, True)
L2.replace(&quot;male&quot;, 0, True)

pdReadySmoker = pd.DataFrame({&quot;smoker&quot;: smoker})
pdReadySex = pd.DataFrame({&quot;sex&quot;: sex})

SmokerAndSex = pd.merge(pdReadySmoker, pdReadySex, how=&quot;outer&quot;, left_index=True, right_index=True)

INPUTS = pd.merge(partialInputs, SmokerAndSex, how=&quot;outer&quot;, left_index=True, right_index=True)
TARGETS = csv[&quot;charges&quot;]

INPUTS = torch.from_numpy(np.array(INPUTS, dtype='float32'))
TARGETS = torch.from_numpy(np.array(TARGETS, dtype='float32'))

print(INPUTS.shape, TARGETS.shape)

loss_fn = F.mse_loss
model = nn.Linear(5, 3) # &lt;-- changing this, changes the error message.
opt = torch.optim.SGD(model.parameters(), lr=1e-5)

trainDataset = TensorDataset(INPUTS, TARGETS)
BATCH_SIZE = 5
trainDataloader = DataLoader(trainDataset, BATCH_SIZE, shuffle=True)

def fit(numEpochs, model, loss_fn, opt, trainDataloader):
    for epochs in range(numEpochs):
        for inputBatch, targetBatch in trainDataloader:
            preds = model(inputBatch)
            loss = loss_fn(preds, targetBatch)
            loss.backward()
            
            opt.step()
            
            opt.zero_grad()
        
        e = epoch + 1
        if e % 10 == 0:
            print(f&quot;Epoch: {e/numEpochs}, loss: {loss.item():.4f}&quot;)

fit(100, model, loss_fn, opt, trainDataloader) &lt;-- error

Error produced:
&lt;ipython-input-7-b7028a3d94fd&gt;:5: UserWarning: Using a target size (torch.Size([5])) that is different to the input size (torch.Size([5, 3])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
  loss = loss_fn(preds, targetBatch)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-20-d8f5bcdc847d&gt; in &lt;module&gt;
----&gt; 1 fit(100, model, loss_fn, opt, trainDataloader)

&lt;ipython-input-7-b7028a3d94fd&gt; in fit(numEpochs, model, loss_fn, opt, trainDataloader)
      3         for inputBatch, targetBatch in trainDataloader:
      4             preds = model(inputBatch)
----&gt; 5             loss = loss_fn(preds, targetBatch)
      6             loss.backward()
      7 

D:\coding\machine-learning\env-ml\lib\site-packages\torch\nn\functional.py in mse_loss(input, target, size_average, reduce, reduction)
   2657         reduction = _Reduction.legacy_get_string(size_average, reduce)
   2658 
-&gt; 2659     expanded_input, expanded_target = torch.broadcast_tensors(input, target)
   2660     return torch._C._nn.mse_loss(expanded_input, expanded_target, _Reduction.get_enum(reduction))
   2661 

D:\coding\machine-learning\env-ml\lib\site-packages\torch\functional.py in broadcast_tensors(*tensors)
     69         if any(type(t) is not Tensor for t in tensors) and has_torch_function(tensors):
     70             return handle_torch_function(broadcast_tensors, tensors, *tensors)
---&gt; 71     return _VF.broadcast_tensors(tensors)  # type: ignore
     72 
     73 

RuntimeError: The size of tensor a (3) must match the size of tensor b (5) at non-singleton dimension 1

I've tried changing the dimensions of the of model, and these are a few of the changes made and the associated errors:
model = nn.Linear(5, 1338)

Error:
RuntimeError: The size of tensor a (1338) must match the size of tensor b (5) at non-singleton dimension 1

model = nn.Linear(1338, 1338)

Error:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (5x5 and 1338x1338)

Sometimes this error, will make me change the matrix to the correct shape, but that results in the previous error regarding non-singleton dimension 

This should be quite straight-forward, you only have a single layer. This is a matter of sorting the shapes right.
You are feeding a nn.Linear layer an input with shape input_shape. This type of layer takes two arguments: in_features the number of features in the input vector, and out_features the number of features in the resulting vector. Since you are using the F.mse_loss, your target vector needs to have the same shape as your prediction.
Bear in mind the first dimension is the batch dimension. In summary, your input tensor has shape (batch, input_size), your dense layer is defined as nn.Linear(input_size, out_size) and your target tensor has shape (batch, output_size).

Coming back to your case, your TARGETS tensor is of shape (1338) so you either mean to:

have a single prediction with 1338 components which would match a nn.Linear(?, 1338) and it would actually correspond to (1, 1338) (a single element in the batch). This can be fixed with TARGETS = TARGETS.unsqueeeze(0).

or, there are actually 1338 predictions one element, which would match a nn.Linear(?, 1) and the appropriate target shape would be (1338, 1). This can be fixed with TARGETS = TARGETS.unsqueeeze(-1) (adds an additional axis after the last dimension).


