C2_W3_Assigement an ValueError problem

When creating a post, please add:

  • Week # must be added in the tags option of the post.
  • Link to the classroom item you are referring to:
  • Description (include relevant info but please do not post solution code or your entire notebook):
tf.random.set_seed(1234)
model = Sequential(
    [
        Dense(120, activation = 'relu', name = "L1"),      
        Dense(40, activation = 'relu', name = "L2"),         
        Dense(classes, activation = 'linear', name = "L3")  
    ], name="Complex"
)
model.compile(
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),          
    optimizer=tf.keras.optimizers.Adam(0.01),   
)

model.fit(
    X_train,y_train,
    epochs=1000
)              
model.summary()
model_test(model, classes, X_train.shape[1])

when I run this code,it turns out that :
ValueError: The layer Complex has never been called and thus has no defined input.

how to deal with it?

You don’t have an input layer, if no input then you have to manually build the model as suggested in this link:

model = Sequential(
    [
        Dense(units=120,activation="relu",name = "L1",input_shape=(X_train.shape[1],)),
        Dense(units=40,activation="relu",name = "L2"),
        Dense(classes,activation="linear",name = "L3")
    ], name="Complex"
)

like this?

the model can fit, but when I run

model_test(model, classes, X_train.shape[1]) 

it tells me that:

ValueError: The layer Complex has never been called and thus has no defined input.

Whereas if you specify an `Input`, the model gets built continuously as you are adding layers:
model = keras.Sequential()
model.add(keras.Input(shape=(16,)))
model.add(keras.layers.Dense(8))len(model.weights)  
# Returns "2"

Like this!

I change my code to this:

from tensorflow import keras
tf.random.set_seed(1234)
model = keras.Sequential()
model.add(keras.Input(shape=(X_train.shape[1],)))
model.add(keras.layers.Dense(120, activation='relu', name="L1"))
model.add(keras.layers.Dense(40, activation='relu', name="L2"))
model.add(keras.layers.Dense(classes, activation='linear', name="L3")) 

model.compile(
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    optimizer=tf.keras.optimizers.Adam(0.01),
)
model.fit(
    X_train, y_train,
    epochs=100
)

it is ok when I run it. However when I run the code

model.summary()
model_test(model, classes, X_train.shape[1]) 

it turns out:


:frowning_face:

I am not familiar with this course but it says you have more layers than supposed too, maybe you have put too many or maybe you can omit the input layer and build the model. Try to to read the Tensorflow link I attached above!

ok,thanks for your advice, I appreciate it !

Going back to your original post.

I don’t see anything wrong with your code for the model or for model.compile().

However, It does appear that you have combine the code into one cell, which should each be in separate cells. The code you show should be in three separate cells.

Modifying the notebook like this is not recommended. The reason is that every cell has metadata which you cannot see, and that is used by Jupyter to control how your code is used.

I recommend you delete your notebook and start over with a fresh copy. Then only modify the parts of the notebook that are identified by the “START CODE HERE” banner.

Please don’t delete any lines of comments - those are included for a reason.

Hi TMosh:
According to advice, I copy the code in a new notebook.
The codes are in three cell, I run them one by one.
It is ok when I run it until the code "model_test(model, classes, X_train.shape[1]) " which showed that
"ValueError: The layer Complex has never been called and thus has no defined input.
"

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.activations import relu,linear
from tensorflow.keras.losses import SparseCategoricalCrossentropy
from tensorflow.keras.optimizers import Adam

from public_tests_a1 import * 
from assigment_utils import *
# Generate and split data set
X, y, centers, classes, std = gen_blobs()

# split the data. Large CV population for demonstration
X_train, X_, y_train, y_ = train_test_split(X,y,test_size=0.50, random_state=1)
X_cv, X_test, y_cv, y_test = train_test_split(X_,y_,test_size=0.20, random_state=1)
print("X_train.shape:", X_train.shape, "X_cv.shape:", X_cv.shape, "X_test.shape:", X_test.shape)
# UNQ_C4
# GRADED CELL: model_s
# mentor edit: code removed

I do not recall recommending that.

I recommended that you get a fresh copy of the notebook and start over, paying particular attention to following the instructions and only modifying the portions of the notebook that are marked.

Please do not post your code on the forum. That’s not allowed by the Code of Conduct.

If a mentor needs to see your code, we’ll contact you by personal message with instructions.