Assignment 2: Train Your First Model

Starter notebook:


In this assignment, we’re going to use information like a person’s age, sex, BMI, no. of children, and smoking habit to predict the price of yearly medical bills. We will train a model with the following steps:

  1. Download and explore the dataset
  2. Prepare the dataset for training
  3. Create a linear regression model
  4. Train the model to fit the data
  5. Make predictions using the trained model

Steps to complete the assignment

  1. Fork & run this notebook:
  2. Fill out all the ??? in the notebook to complete the assignment, and commit the final version to Jovian
  3. (Optional) Replicate the model training for another dataset & write a blog post
  4. (Optional) Share your work with the community on the thread

Make sure to review the material from Lecture 2 before starting the assignment. Please reply here if you have any questions or face issues. The recommended platform for writing your blog post is .

1 Like


I’m getting this error while trying to fit my model: RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn. I have been trying to find what produces the error, but I could not find it. This is the link to the project: link
Thanks in advance

1 Like

@lsalvador-ht As I can see in your notebook , there is some wrong codes in your Model cells.

In the training_step and validation_step function the loss = F.mse_loss line should have out variable instead of inputs variable in its arguements.
Hope it helps, Thank You.



My val_loss and all the weights and bias parameters in the model became nan when I call fit method.
And I do not know why.

It would be really nice if someone can have a look or give me some advices.

Thank you in advance

Here is my notebook

I would try and restart the notebook. Perhaps you’ve picked too large learning rate first, and you experienced a thing called “exploding gradients”.

I would also suggest to incorporate a method called “gradient clipping” in your training (clip_grad_value_ or clip_grad_norm_).

1 Like

Hi, In the second assignment (02-insurance-linear) I am getting an error for the command.

Has anyone faced this error yet.


@hargurjeet you have to put it in quotation mark ‘something’


Please explain why it is giving me NAN values in val_loss and even when I am running evaluate(model,val_loader) second time it is resulting as NAN .

1 Like

Thank you very much!

1 Like

Your learning rate is too big

Ok, then what learning rate should I keep instead?

Depends on the model and on you, if its giving nan with 1e-2 make it 1e-4 then train 5 to 10 epochs then decrease the LR to 1e-5 then again train 10 epochs it depends completely on you and the model.


ok I will try it Thank You

1 Like

your_name is a string variable, insert “hargurjeet”

I am getting this error someone please help:
RuntimeError: The size of tensor a (7) must match the size of tensor b (127) at non-singleton dimension 1

Hello guys, Here is my first Model in this course, check this link

i am getting a very large val_loss using mse_loss, is it with everyone or just me. should i change my loss function?

@siddhantramteke369 could you share the notebook please.

This problem of your is solved just do .type(torch.float32) while making tensors and it shall work fine, but it seems there are other problems in your noteobok.


I am getting the below error, Can anyone suggest on the resolution

link to my notebook -