I keep getting this model is not defined error


There was a problem compiling the code from your notebook. Details: name ‘model’ is not defined


import tensorflow as tf
import numpy as np

GRADED FUNCTION: house_model

def house_model():

# Define input and output tensors with the values for houses with 1 up to 6 bedrooms
# Hint: Remember to explictly set the dtype as float
xs = np.array([1, 2, 3, 4, 5, 6], dtype = float)
ys = np.array([1.0, 1.5, 2.0, 2.5, 3.0, 3.5], dtype = float)

# Define your model (should be a model with 1 dense layer and 1 unit)
from tensorflow import keras
model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])

# Compile your model
# Set the optimizer to Stochastic Gradient Descent
# and use Mean Squared Error as the loss function
model.compile(optimizer='sgd', loss='mean_squared_error')

# Train your model for 1000 epochs by feeding the i/o tensors
model.fit(xs, ys, epochs=500)

return model

Get your trained model

model = house_model()

new_y = 7.0
prediction = model.predict([new_y])[0]

Hello @21bcs2049 ,

Welcome to the Community!!

I can see 2 mistakes in your code:

1.You have taken xs of dtype float, you used int types 1,2,3…
Use 1.0, 2.0,3.0…

2.You are asked in the to go for 1000 epochs as per the hint.
You have taken 500.
Use 1000.

Btw send your notebook via dm. I need to check where and why it’s giving error.Then we will discuss the issues here.

With regards,
Nilosree Sengupta

Hello @21bcs2049 ,

I have seen your updated notebook in dm.
Now there are more 2 things:

  1. You have written :
    model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
    But it should be :
model = tf.keras.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])])
  1. When you are using tf.keras. , you dont need to import keras from tensorflow.
from tensorflow import keras ## Remove this. Not needed.

With regards,
Nilosree Sengupta

Btw What is the Current error status of your code?
Make the above stated minor modifications and attach a screenshot of the error message @21bcs2049 .

With regards,
Nilosree Sengupta

I had done this right but still getting errors

Even i have tried troubleshooting and different types of working to make code working but it doesn’t passes

@21bcs3497 , Give the screenshot of your error.

Thank you for your help, my submission is submitted successfully now, I did change the number of epochs to 1000 and modified the value of xs with 1.0, 2.0 and so on, but even after doing that the error kept coming.

I think there was an error in the server as after submitting the same code 11-12 times and repeatedly getting 0/100, it suddenly accepted the same code and marked my assignment as complete.

Hello @21bcs2049 ,

I see.

So is your problem solved now? Have you passed the assignment?

With regards,
Nilosree Sengupta


The staff are unable to replicate this as per the update today. Moving forward, if you face such an issue after refreshing the lab, please contact coursera support

The agents there might be able to help better. The learner should mention that both mentor and staff terminals are behaving differently so it’s hard for us to continue assisting. Hope these help! Thank you!

Hi ,

I am getting the same problem. I have tried to submit more than 10 times. Still not accepting.#

[code removed - moderator]

Much appreciated any help as I am fed-up with this.

The assignment asks you to predict price of a house with 7 bedrooms. Why are you predicting the price for a house with 8 bedrooms?

Moving forward, don’t post code in public but share either the stacktrace or grader feedback on a public post.

Hi all,
I am getting the same problem.

There was a problem compiling the code from your notebook. Details:
name ‘model’ is not defined

I have tried to submit more than 20 times.
But it still not accepting!

Everything is fine when I try on the notebook.

Does this help?

No, but thank you!
Finally, I sloved the problem by this