Week 2 Assignment 2 alpaca_model TypeError

I don’t understand where the following error is coming from.


TypeError Traceback (most recent call last)
in
----> 1 model2 = alpaca_model(IMG_SIZE, data_augmentation)

in alpaca_model(image_shape, data_augmentation)
42
43 # create a prediction layer with one neuron (as a classifier only needs one)
—> 44 prediction_layer = base_model.predict(x)
45
46 ### END CODE HERE

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
128 raise ValueError(’{} is not supported in multi-worker mode.’.format(
129 method.name))
→ 130 return method(self, *args, **kwargs)
131
132 return tf_decorator.make_decorator(

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
1577 use_multiprocessing=use_multiprocessing,
1578 model=self,
→ 1579 steps_per_execution=self._steps_per_execution)
1580
1581 # Container that configures and calls tf.keras.Callbacks.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/data_adapter.py in init(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution)
1115 use_multiprocessing=use_multiprocessing,
1116 distribution_strategy=ds_context.get_strategy(),
→ 1117 model=model)
1118
1119 strategy = ds_context.get_strategy()

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/data_adapter.py in init(self, x, y, sample_weights, sample_weight_modes, batch_size, epochs, steps, shuffle, **kwargs)
273 inputs = pack_x_y_sample_weight(x, y, sample_weights)
274
→ 275 num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs))
276 if len(num_samples) > 1:
277 msg = “Data cardinality is ambiguous:\n”

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/data_adapter.py in (.0)
273 inputs = pack_x_y_sample_weight(x, y, sample_weights)
274
→ 275 num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs))
276 if len(num_samples) > 1:
277 msg = “Data cardinality is ambiguous:\n”

TypeError: int() argument must be a string, a bytes-like object or a number, not ‘NoneType’

The error is in the alpaca_model(), at the line where you create the “prediction_layer”.
Your code is incorrect, you’re supposed to use “Dense()” with one unit.

1 Like

The key to interpreting these error tracebacks is to start at the bottom, and work upward until you find a line of code marked with —> in a function that you have written. That’s the line that throws the error.

2 Likes

Does anyone find the process of trouble-shooting tedious ? and sometimes feel lost on how to proceed? how are we supposed to ask questions without including the code ?

Yes, it is tedious.

Debugging your software is a learned art. If you just post your code and ask people to fix it, you don’t learn how to debug.

You can post your error messages, or post a description of the issue, and in general reading the FAQ threads will give you some useful tips.

1 Like

I understand now. It helps to look at this: Transfer learning and fine-tuning  |  TensorFlow Core

3 Likes

I had the same problem… I think the point of this assignment was to make us go to the API guide and figure things out for ourselves. There isn’t an exact explanation, but if you look at the example of Dense here: tf.keras.layers.Dense  |  TensorFlow Core v2.5.0
you will eventually find one that uses Dense with only one output. You’re on the right track… I think the mentors are just not allowed to give you the answer directly. (which makes sense unless you’re on a wild goose chase, and nobody is answering your questions on this support site).

1 Like

If instead of Dense, we use Activation(‘sigmoid’) then is it different from Dense() with one unit?

Activation from :

Well, how would one go about answering that question? The first thing to do is understand what a Dense layer is. Then the second step is to understand what the sigmoid activation function does. Just at a general level, note that all activation functions operate “elementwise”. That means that whatever the input to your proposed sigmoid layer is, the output will be a tensor of the same shape, right? Is that what a Dense layer does? Have a look at the documentation link that @caltech_kerava has provided above to find out more. Note that a “Dense” layer is what Prof Ng refers to in the lectures as a “Fully Connected” layer. The other thing you’ll learn from reading the documentation is that you can specify the activation function to be used as one of the arguments to the Dense() function. Not to spoil your fun or anything: you should read the documentation for yourself.

1 Like

I get this. Has anyone seen this error?
Test failed
Expected value

[‘Functional’, (None, 5, 5, 1280), 2257984]

does not match the input value:

[‘GlobalAveragePooling2D’, (None, 3), 0]

AssertionError Traceback (most recent call last)
in
10 [‘Dense’, (None, 1), 1281, ‘linear’]] #linear is the default activation
11
—> 12 comparator(summary(model2), alpaca_summary)
13
14 for layer in summary(model2):

~/work/W2A2/test_utils.py in comparator(learner, instructor)
19 “\n\n does not match the input value: \n\n”,
20 colored(f"{a}", “red”))
—> 21 raise AssertionError(“Error in test”)
22 print(colored(“All tests passed!”, “green”))
23

AssertionError: Error in test

@ocharlesg: It looks like perhaps you skipped the step of adding the base_model as a layer in the alpaca_model logic. They give you the line in the template, but you have to fill in the RHS of the assignment statement correctly.