Week 3 Practice Lab: Advice for Applying Machine Learning

assignemnt page:

link to my practice lab:

Hi dear community, I have a question while finishing my week 3 assignment, so the feedback after submission says Cell #UNQ_C5. Can’t compile the student’s code. Error: NameError(“name ‘l2’ is not defined”)

and can you help me with adjust my code, do not really know how to fix this from my side:

UNQ_C5

GRADED CELL: model_r

tf.random.set_seed(1234)

model_r = Sequential([
Dense(120, activation=‘relu’, kernel_regularizer=l2(0.1)), # First dense layer with L2 regularization
Dense(40, activation=‘relu’, kernel_regularizer=l2(0.1)), # Second dense layer with L2 regularization
Dense(6, activation=‘linear’) # Output dense layer without activation modification
], name=“Complex_Regularized”)

model_r.compile(
loss=SparseCategoricalCrossentropy(from_logits=True),
optimizer=Adam(learning_rate=0.01)
)

Assuming X_train and y_train are defined elsewhere in your code

model_r.fit(X_train, y_train, epochs=1000)

model_r.summary()

Additionally, see attached screen-shots

Thanks so much for the help!!

1 Like

Hi @shunyao

Firstly, make sure that you have imported the necessary libraries at the beginning of your notebook. You need to import the l2 function from the tensorflow.keras.regularizers module. You can do this by running the cell that contains:

from tensorflow.keras.regularizers import l2

Make sure you have modified the code only in the allowed places and have not deleted any essential code. If the issue persists, consider resetting your notebook to the last checkpoint and rewriting your code from the beginning.

Let me know if you need further assistance!

3 Likes

I may be wrong, but I don’t see any cell in the notebook that specifically imports the l2 regularizer.

tf and keras are included by the imports in Cell 1 in the notebook.

In your code for the Sequential model, the hint code tells you to specify “tf.keras.regularizers.l2(…)”.

1 Like

Hi @Alireza_Saei @TMosh,
Thanks for the guidance!! YEESSS I modified the code, and now I passed!!! :tada:
Also this is my final assignment to pass, and now I got my ML specialization certification :raised_hands:

2 Likes

Congratulations on completing your ML specialization certification! I’m glad I could help you. Keep up the great work! :tada:

1 Like

Thank you!! Really happy about this!!!

2 Likes