tensorflow: AttributeError: 'NoneType' object has no attribute 'shape'

Hello,

I’m trying to train a model using the model.fit() function, but it keeps throwing me an error which I cannot resolve. The error is:

Traceback (most recent call last):
  File "train.py", line 355, in <module>
    main()
  File "/Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py", line 620, in wrapper
    return func(*args, **kwargs)
  File "train.py", line 338, in main
    history = model.fit(
  File "/Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1100, in fit
    tmp_logs = self.train_function(iterator)
  File "/Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 828, in __call__
    result = self._call(*args, **kwds)
  File "/Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 871, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "/Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 725, in _initialize
    self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
  File "/Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2969, in _get_concrete_function_internal_garbage_collected
    graph_function, _ = self._maybe_define_function(args, kwargs)
  File "/Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3361, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "/Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3196, in _create_graph_function
    func_graph_module.func_graph_from_py_func(
  File "/Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 990, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "/Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 634, in wrapped_fn
    out = weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "/Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 977, in wrapper
    raise e.ag_error_metadata.to_exception(e)
AttributeError: in user code:

    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:805 train_function  *
        return step_function(self, iterator)
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:795 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
        return fn(*args, **kwargs)
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:788 run_step  **
        outputs = model.train_step(data)
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:758 train_step
        self.compiled_metrics.update_state(y, y_pred, sample_weight)
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/compile_utils.py:387 update_state
        self.build(y_pred, y_true)
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/compile_utils.py:317 build
        self._metrics = nest.map_structure_up_to(y_pred, self._get_metric_objects,
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/util/nest.py:1159 map_structure_up_to
        return map_structure_with_tuple_paths_up_to(
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/util/nest.py:1257 map_structure_with_tuple_paths_up_to
        results = [
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/util/nest.py:1258 <listcomp>
        func(*args, **kwargs) for args in zip(flat_path_gen, *flat_value_gen)
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/util/nest.py:1161 <lambda>
        lambda _, *values: func(*values),  # Discards the path arg.
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/compile_utils.py:418 _get_metric_objects
        return [self._get_metric_object(m, y_t, y_p) for m in metrics]
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/compile_utils.py:418 <listcomp>
        return [self._get_metric_object(m, y_t, y_p) for m in metrics]
    /Users/pietmuller/Dokumente/code/AI/E2EASRS/tensor_venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/compile_utils.py:439 _get_metric_object
        y_t_rank = len(y_t.shape.as_list())

    AttributeError: 'NoneType' object has no attribute 'shape'

This is my .fit() function:

history = model.fit(
        train_ds, 
        validation_data=val_ds, 
        epochs=EPOCHS, 
        batch_size=BATCH_SIZE,
        callbacks=[earlyStopping, modelCheckpoint]
    )

My train dataset is a tf.data.Dataset and is of type: <class 'tensorflow.python.data.ops.dataset_ops.BatchDataset'>

The model is building and compiling fine, but training…

When I try to look at the dataset using:

for batch in train_ds.take(1):
    input_shape = batch["spectrogram"].shape
    label_shape = batch["label"].shape
    print('Input shape:', input_shape)

It outputs the shape just fine.

I’m looking forward for help:)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (7 by maintainers)

Most upvoted comments

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you.