Why is my loss function always returning nan?
I'm trying to learn about ML and I tried to make a simple linear model, but when I run it, loss comes out as null:

So I tried to find whats the problem about. If I print first 10 y_pred, only about 17 of them have numbers, the rest of them are null. Maybe im doing something wrong, please help.
import torch
from torch.utils.data import TensorDataset, DataLoader
import torch.nn as nn
import numpy as np

#Input (temp, rainfall, humidity)
inputs = np.array([[73, 67, 43], [91, 88, 64], [87, 134, 58], [102, 43, 37], [69, 96, 70], [73, 67, 43], [91, 88, 64], [87, 134, 58], [102, 43, 37], [69, 96, 70], [73, 67, 43], [91, 88, 64], [87, 134, 58], [102, 43, 37], [69, 96, 70]], dtype='float32')

#Target (apples, oranges)
targets = np.array([[56, 70], [81, 101], [119, 133], [22, 37], [103, 119], [56, 70], [81, 101], [119, 133], [22, 37], [103, 119], [56, 70], [81, 101], [119, 133], [22, 37], [103, 119]], dtype='float32')

inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)

#Define datasets
train_ds = TensorDataset(inputs, targets)
train_ds[0:3]

#Hyperparameters
batch_size = 5
num_epochs = 100
learning_rate = 0.01

train_dl = DataLoader(dataset=train_ds, batch_size=batch_size, shuffle=True)


model = nn.Linear(3,2) #inputs(temp, rainfall, humidity) , targets(apples, oranges)

loss_f = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

for epoch in range(num_epochs):
    for xb, yb in train_dl:
        y_pred = model(xb)
        loss = loss_f(y_pred, yb)
        loss.backward()
        optimizer.step()
        optimizer.zero_grad()
        
    if(epoch+1) % 10 == 0:
        print(f'epoch = {epoch+1}/{num_epochs}, loss = {loss.item():.4f}')
        
print(f'Final loss = {loss.item():.4f}')

edit:
y_pred.shape = torch.Size([5, 2]), yb.shape = torch.Size([5, 2])

It is not about the Loss function. Your model is predicting Nan and Infy numbers. Possible solution

Reduce the learning rate (Ex.:learning_rate = 0.001) or
Reduce batch size (Ex.: batch_size = 2) or
Add more layers into the model with activation functions or
Normalize inputs

