Exercise 1 Use a Keras sequential model but instead uses the Keras functional api

I copy the Click for solution for exercise 1 and is uncorrect:

NameError Traceback (most recent call last)
in
6 tf.keras.layers.Dense(256, activation=‘relu’),
7 tf.keras.layers.Dense(128, activation=‘relu’),
----> 8 tf.keras.layers.Dense(num_outputs),
9 ### END CODE HERE ###
10 ])

NameError: name ‘num_outputs’ is not defined

Tip:
Every time you open a notebook, you must run all of the cells starting from the top.

Maybe this will help. If not please post back with additional information.

That error message has nothing to do with the Sequential or Functional API. It appears you have an undefined variable.

I doing this

but i copy the solution:
my code: user_NN = tf.keras.models.Sequential([
### START CODE HERE ###
tf.keras.layers.Dense(256, activation=‘relu’),
tf.keras.layers.Dense(128, activation=‘relu’),
tf.keras.layers.Dense(num_outputs),
### END CODE HERE ###
])

item_NN = tf.keras.models.Sequential([
### START CODE HERE ###
tf.keras.layers.Dense(256, activation=‘relu’),
tf.keras.layers.Dense(128, activation=‘relu’),
tf.keras.layers.Dense(num_outputs),
### END CODE HERE ###
])

and then
the next cell ; # Public tests
from public_tests import *
test_tower(user_NN)
test_tower(item_NN)
the error:
AttributeError Traceback (most recent call last)
in
1 # Public tests
2 from public_tests import *
----> 3 test_tower(user_NN)
4 test_tower(item_NN)

~/work/public_tests.py in test_tower(target)
15 assert type(layer) == expected[i][0],
16 f"Wrong type in layer {i}. Expected {expected[i][0]} but got {type(layer)}"
—> 17 assert layer.output.shape.as_list() == expected[i][1],
18 f"Wrong number of units in layer {i}. Expected {expected[i][1]} but got {layer.output.shape.as_list()}"
19 assert layer.activation == expected[i][2], \

/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in output(self)
1551 “”"
1552 if not self._inbound_nodes:
→ 1553 raise AttributeError(‘Layer ’ + self.name + ’ has no inbound nodes.’)
1554 return self._get_node_attribute_at_index(0, ‘output_tensors’, ‘output’)
1555

AttributeError: Layer dense_257 has no inbound nodes.

Are you working on the “C3_W2_RecSysNN_Assignment” notebook?

The num_outputs variable is defined as the first line in the cell you seem to be working on.

C3_W2_RecSysNN_Assignment” notebook yes.
I alright put this number, but the next cell: # Public tests
from public_tests import *
test_tower(user_NN)
test_tower(item_NN)
the error is:
AttributeError Traceback (most recent call last)
in
1 # Public tests
2 from public_tests import *
----> 3 test_tower(user_NN)
4 test_tower(item_NN)

~/work/public_tests.py in test_tower(target)
15 assert type(layer) == expected[i][0],
16 f"Wrong type in layer {i}. Expected {expected[i][0]} but got {type(layer)}"
—> 17 assert layer.output.shape.as_list() == expected[i][1],
18 f"Wrong number of units in layer {i}. Expected {expected[i][1]} but got {layer.output.shape.as_list()}"
19 assert layer.activation == expected[i][2], \

/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in output(self)
1551 “”"
1552 if not self._inbound_nodes:
→ 1553 raise AttributeError(‘Layer ’ + self.name + ’ has no inbound nodes.’)
1554 return self._get_node_attribute_at_index(0, ‘output_tensors’, ‘output’)
1555

AttributeError: Layer dense has no inbound nodes.

Note: I enabled the thread listing after removing the fragment of solution code.

Your notebook is missing a lot of lines of code that seem to have been deleted, starting right after the item_NN() model is created.

That is why you are getting an error about the input layer - that code is missing from your notebook.

error:
File “”, line 24
vu= tf.linalg.12_normalize(vm,axis=1)
^
SyntaxError: invalid token

The “invalid token” with the marker at the first character of the line, means there is a syntax error with the previous lines of code.

Also, where you have “vu” and “vm” in the same line of code, that’s an error.

1 Like

I have same issue and unable to solve the following exercise , C3_W2_RecSysNN_Assignment

# GRADED_CELL
# UNQ_C1

num_outputs = 32
tf.random.set_seed(1)
user_NN = tf.keras.models.Sequential([
    ### START CODE HERE ###   
    tf.keras.layers.Dense(256, activation='relu'),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(num_outputs),    
    ### END CODE HERE ###  
])

item_NN = tf.keras.models.Sequential([
    ### START CODE HERE ###     
tf.keras.layers.Dense(256, activation='relu'),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(num_outputs),
    ### END CODE HERE ###  
])

# create the user input and point to the base network
input_user = tf.keras.layers.Input(shape=(num_user_features))
vu = user_NN(input_user)
vu = tf.linalg.l2_normalize(vu, axis=1)

# create the item input and point to the base network
input_item = tf.keras.layers.Input(shape=(num_item_features))
vm = item_NN(input_item)
vm = tf.linalg.l2_normalize(vm, axis=1)

# compute the dot product of the two vectors vu and vm
output = tf.keras.layers.Dot(axes=1)([vu, vm])

# specify the inputs and output of the model
model = Model([input_user, input_item], output)

model.summary()

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

Only post screen capture images of any error messages or asserts.

I have edited your message.

I’ll have another reply shortly with some tips.

I got it, that was my first post here , I will be avoid next time.
Please guide how i can post my question separately instead of commenting on others post. I’m new user

1 Like

Go to the forum topic for your course. See the “Course Q&A” topic in the left-side menu. You select the course, then the specific course area, then you will see a “New Topic” button.

For example:

Thanks a lot sir, Can you assist me in my problem please?