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