Error due to a negative dimension in C1W4_Assignment

In the (optional) training part of C1W4_Assignment, I get an error due to some…

Negative dimension size caused by subtracting 2 from 1.

What is this due to? All my test passed just fine. I even tried to account for the case where the number of classes was two and thus encoded as booleans, in which case I used a sigmoid activation and a binary cross-entropy.

The full error is as follows:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-61-a97d1c255550> in <module>
     17 
     18 # Train the custom VGG model
---> 19 vgg.fit(dataset, epochs=10)

/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    817         max_queue_size=max_queue_size,
    818         workers=workers,
--> 819         use_multiprocessing=use_multiprocessing)
    820 
    821   def evaluate(self,

/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    233           max_queue_size=max_queue_size,
    234           workers=workers,
--> 235           use_multiprocessing=use_multiprocessing)
    236 
    237       total_samples = _get_total_number_of_samples(training_data_adapter)

/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py in _process_training_inputs(model, x, y, batch_size, epochs, sample_weights, class_weights, steps_per_epoch, validation_split, validation_data, validation_steps, shuffle, distribution_strategy, max_queue_size, workers, use_multiprocessing)
    591         max_queue_size=max_queue_size,
    592         workers=workers,
--> 593         use_multiprocessing=use_multiprocessing)
    594     val_adapter = None
    595     if validation_data:

/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py in _process_inputs(model, mode, x, y, batch_size, epochs, sample_weights, class_weights, shuffle, steps, distribution_strategy, max_queue_size, workers, use_multiprocessing)
    704       max_queue_size=max_queue_size,
    705       workers=workers,
--> 706       use_multiprocessing=use_multiprocessing)
    707 
    708   return adapter

/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, standardize_function, **kwargs)
    700 
    701     if standardize_function is not None:
--> 702       x = standardize_function(x)
    703 
    704     # Note that the dataset instance is immutable, its fine to reusing the user

/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py in standardize_function(dataset)
    658         model.sample_weight_mode = getattr(model, 'sample_weight_mode', None)
    659 
--> 660       standardize(dataset, extract_tensors_from_dataset=False)
    661 
    662       # Then we map using only the tensor standardization portion.

/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset)
   2344     # First, we build the model on the fly if necessary.
   2345     if not self.inputs:
-> 2346       all_inputs, y_input, dict_inputs = self._build_model_with_inputs(x, y)
   2347       is_build_called = True
   2348     else:

/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in _build_model_with_inputs(self, inputs, targets)
   2570     else:
   2571       cast_inputs = inputs
-> 2572     self._set_inputs(cast_inputs)
   2573     return processed_inputs, targets, is_dict_inputs
   2574 

/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in _set_inputs(self, inputs, outputs, training)
   2657           kwargs['training'] = training
   2658       try:
-> 2659         outputs = self(inputs, **kwargs)
   2660       except NotImplementedError:
   2661         # This Model or a submodel is dynamic and hasn't overridden

/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    771                     not base_layer_utils.is_in_eager_or_tf_function()):
    772                   with auto_control_deps.AutomaticControlDependencies() as acd:
--> 773                     outputs = call_fn(cast_inputs, *args, **kwargs)
    774                     # Wrap Tensors in `outputs` in `tf.identity` to avoid
    775                     # circular dependencies.

/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/api.py in wrapper(*args, **kwargs)
    235       except Exception as e:  # pylint:disable=broad-except
    236         if hasattr(e, 'ag_error_metadata'):
--> 237           raise e.ag_error_metadata.to_exception(e)
    238         else:
    239           raise

ValueError: in converted code:

    <ipython-input-57-f62868bf82fd>:34 call  *
        x = self.block_c(x)
    /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py:773 __call__
        outputs = call_fn(cast_inputs, *args, **kwargs)
    <ipython-input-27-648bf33d2825>:39 call  *
        max_pool = self.max_pool(x)
    /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py:773 __call__
        outputs = call_fn(cast_inputs, *args, **kwargs)
    /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/pooling.py:248 call
        data_format=conv_utils.convert_data_format(self.data_format, 4))
    /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/ops/nn_ops.py:3875 max_pool
        name=name)
    /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_nn_ops.py:5200 max_pool
        data_format=data_format, name=name)
    /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/framework/op_def_library.py:742 _apply_op_helper
        attrs=attr_protos, op_def=op_def)
    /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py:595 _create_op_internal
        compute_device)
    /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:3322 _create_op_internal
        op_def=op_def)
    /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:1786 __init__
        control_input_ops)
    /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:1622 _create_c_op
        raise ValueError(str(e))

    ValueError: Negative dimension size caused by subtracting 2 from 1 for 'my_vgg_9/block_55/max_pooling2d_55/MaxPool' (op: 'MaxPool') with input shapes: [?,1,1,256].


​

Hi Amine,

You have to learn to trace the error from the error output eventually. Ok so from your error output

Blockquote
ValueError: Negative dimension size caused by subtracting 2 from 1 for ‘my_vgg_9/block_55/max_pooling2d_55/MaxPool’ (op: ‘MaxPool’) with input shapes: [?,1,1,256]. x = self.block_c(x)

So it tells you there is an issue in the x = self.block_c(x) and specifically the (op: ‘MaxPool’) with input shapes: [?,1,1,256]., there should be something wrong with that shape.

Assuming that class Block is built correctly then you need to check self.block_c parameters that they are correctly implemented.

hi gent,

I am also getting attribute error to the collaborative vgg network. although submission of the assignment I cleared. it says vgg network has error.

Can you please guide me through where I am going wrong. n I am sharing the screenshot of the error.
Thank you
DP

Hi I think the problem here is in using multiple processing units. I dont know where it specifically could be but I would guess some changes need to be made either in the code or in the settings for the code to use many processing units.

ok if the coding has error how can the vgg network pass all the test in the previous block. I have followed as per the instructions given. Processing unit part I am unable to understand as I am not a IT person but healthcare related professional. can you elaborate about the processing part mistake if you know.

Thank you again
DP

Hi, I can not give you exact or definitive instructions but what I can tell you is maybe try and use some of the knowledge on distributive training from the courses. Maybe some one else can help in more detail.