Nn model forSpotifyDataset

class SpotifyModel(nn.Module):
def init(self):
super().init()
self.linear1 = nn.Linear(input_size, output_size1)
self.linear2 = nn.Linear(output_size1, output_size2)
self.linear3 = nn.Linear(output_size2, output_size)

def forward(self, xb):
out1 = self.linear1(xb)
out2 = self.linear2(out1)
out = self.linear3(out2)
print(out1.shape,out2.shape,out.shape)

def training_step(self, batch):
inputs, targets = batch
outputs = self(inputs)
loss = F.mse_loss(outputs, targets)
return loss

def validation_step(self, batch):
inputs, targets = batch
outputs = self(inputs)
loss = F.mse_loss(outputs, targets)
return {‘val_loss’ : loss.detach()}

def epoch_end(self, epoch, result, num_epochs):
# Print result every 20th epoch
if (epoch+1) % 20 == 0 or epoch == num_epochs-1:
print(“Epoch [{}], val_loss: {:.4f}”.format(epoch+1, result[‘val_loss’]))

def validation_epoch_end(self, outputs):
batch_losses = [x[‘val_loss’] for x in outputs]
epoch_loss = torch.stack(batch_losses).mean() # Combine losses
return {‘val_loss’: epoch_loss.item()}

THIS IS :point_up_2: CODE FOR MY MODEL CLASS

I am getting error in mse_loss funtion:

for batch in val_dl:
inputs, targets = batch
outputs = model(inputs)
print(targets.shape)
loss = F.mse_loss(outputs, targets)
print(loss)
break

THIS IS THE OUTPUT I AM GETTING:

torch.Size([128, 256]) torch.Size([128, 16]) torch.Size([128, 2])
torch.Size([128, 2])

AttributeError Traceback (most recent call last)
in ()
3 outputs = model(inputs)
4 print(targets.shape)
----> 5 loss = F.mse_loss(outputs, targets)
6 print(loss)
7 break

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in mse_loss(input, target, size_average, reduce, reduction)
2649 mse_loss, tens_ops, input, target, size_average=size_average, reduce=reduce,
2650 reduction=reduction)
-> 2651 if not (target.size() == input.size()):
2652 warnings.warn("Using a target size ({}) that is different to the input size ({}). "
2653 "This will likely lead to incorrect results due to broadcasting. "

AttributeError: ‘NoneType’ object has no attribute ‘size’

The sizes of output and target tensors are equal , still I am getting this error.
My input and target data is also correct they both have dtype = ‘float32’.

I am not understanding why am I getting this error.
Please help me out.

Can You share the link to Your Notebook?

HERE IS THE LINK TO MY NOTEBOOK : soptify-dataset

Your forward method doesn’t return anything.

As @Sebgolos said you have not returned anything in the forward method, I can also see you have not used any activation function in your forward method.

Thank you.
I didn’t notice it earlier.

1 Like