Home > Net >  Why does fitting my tensorflow model return a value error?
Why does fitting my tensorflow model return a value error?

Time:11-16

I am following along with a tutorial, and I am building a simple regression model with tensorflow. I would expect tf to fit the model without any hiccups. Instead, I am getting a value error.

The model and compile steps look identical to the tutorial.

The data is similar (two numpy arrays). I used different numbers in the arrays, but I do not think that is the issue. Any two arrays of equal length should be valid, right?

X = tf.constant(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
y = tf.constant(np.array([1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31]))

model = tf.keras.Sequential([
    tf.keras.layers.Dense(1)
])

model.compile(
    loss=tf.keras.losses.mae,
    optimizer=tf.keras.optimizers.SGD(),
    metrics=["mae"]
)

model.fit(X, y, epochs=10)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-41-2da4b2bd3c5c> in <module>
     12 )
     13 
---> 14 model.fit(X, y, epochs=10)

~/opt/anaconda3/lib/python3.8/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     59     def error_handler(*args, **kwargs):
     60         if not tf.debugging.is_traceback_filtering_enabled():
---> 61             return fn(*args, **kwargs)
     62 
     63         filtered_tb = None

~/opt/anaconda3/lib/python3.8/site-packages/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_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
   1562                         ):
   1563                             callbacks.on_train_batch_begin(step)
-> 1564                             tmp_logs = self.train_function(iterator)
   1565                             if data_handler.should_sync:
   1566                                 context.async_wait()

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py in error_handler(*args, **kwargs)
    139     try:
    140       if not is_traceback_filtering_enabled():
--> 141         return fn(*args, **kwargs)
    142     except NameError:
    143       # In some very rare cases,

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
    913 
    914       with OptionalXlaContext(self._jit_compile):
--> 915         result = self._call(*args, **kwds)
    916 
    917       new_tracing_count = self.experimental_get_tracing_count()

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
    961       # This is the first call of __call__, so we have to initialize.
    962       initializers = []
--> 963       self._initialize(args, kwds, add_initializers_to=initializers)
    964     finally:
    965       # At this point we know that the initialization is complete (or less

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
    783     self._graph_deleter = FunctionDeleter(self._lifted_initializer_graph)
    784     self._concrete_stateful_fn = (
--> 785         self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
    786             *args, **kwds))
    787 

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
   2521       args, kwargs = None, None
   2522     with self._lock:
-> 2523       graph_function, _ = self._maybe_define_function(args, kwargs)
   2524     return graph_function
   2525 

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
   2758             # Only get placeholders for arguments, not captures
   2759             args, kwargs = placeholder_dict["args"]
-> 2760           graph_function = self._create_graph_function(args, kwargs)
   2761 
   2762           graph_capture_container = graph_function.graph._capture_func_lib  # pylint: disable=protected-access

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs)
   2668     arg_names = base_arg_names   missing_arg_names
   2669     graph_function = ConcreteFunction(
-> 2670         func_graph_module.func_graph_from_py_func(
   2671             self._name,
   2672             self._python_function,

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, acd_record_initial_resource_uses)
   1245         _, original_func = tf_decorator.unwrap(python_func)
   1246 
-> 1247       func_outputs = python_func(*func_args, **func_kwargs)
   1248 
   1249       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
    675         # the function a weak reference to itself to avoid a reference cycle.
    676         with OptionalXlaContext(compile_with_xla):
--> 677           out = weak_wrapped_fn().__wrapped__(*args, **kwds)
    678         return out
    679 

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1231           except Exception as e:  # pylint:disable=broad-except
   1232             if hasattr(e, "ag_error_metadata"):
-> 1233               raise e.ag_error_metadata.to_exception(e)
   1234             else:
   1235               raise

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1220           # TODO(mdan): Push this block higher in tf.function's call stack.
   1221           try:
-> 1222             return autograph.converted_call(
   1223                 original_func,
   1224                 args,

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py in converted_call(f, args, kwargs, caller_fn_scope, options)
    437     try:
    438       if kwargs is not None:
--> 439         result = converted_f(*effective_args, **kwargs)
    440       else:
    441         result = converted_f(*effective_args)

~/opt/anaconda3/lib/python3.8/site-packages/keras/engine/training.py in tf__train_function(iterator)
     13                 try:
     14                     do_return = True
---> 15                     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16                 except:
     17                     do_return = False

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py in converted_call(f, args, kwargs, caller_fn_scope, options)
    375 
    376   if not options.user_requested and conversion.is_allowlisted(f):
--> 377     return _call_unconverted(f, args, kwargs, options)
    378 
    379   # internal_convert_user_code is for example turned off when issuing a dynamic

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py in _call_unconverted(f, args, kwargs, options, update_cache)
    457   if kwargs is not None:
    458     return f(*args, **kwargs)
--> 459   return f(*args)
    460 
    461 

~/opt/anaconda3/lib/python3.8/site-packages/keras/engine/training.py in step_function(model, iterator)
   1144                 )
   1145             data = next(iterator)
-> 1146             outputs = model.distribute_strategy.run(run_step, args=(data,))
   1147             outputs = reduce_per_replica(
   1148                 outputs, self.distribute_strategy, reduction="first"

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py in run(***failed resolving arguments***)
   1313       fn = autograph.tf_convert(
   1314           fn, autograph_ctx.control_status_ctx(), convert_by_default=False)
-> 1315       return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
   1316 
   1317   def reduce(self, reduce_op, value, axis):

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py in call_for_each_replica(self, fn, args, kwargs)
   2889       kwargs = {}
   2890     with self._container_strategy().scope():
-> 2891       return self._call_for_each_replica(fn, args, kwargs)
   2892 
   2893   def _call_for_each_replica(self, fn, args, kwargs):

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py in _call_for_each_replica(self, fn, args, kwargs)
   3690   def _call_for_each_replica(self, fn, args, kwargs):
   3691     with ReplicaContext(self._container_strategy(), replica_id_in_sync_group=0):
-> 3692       return fn(*args, **kwargs)
   3693 
   3694   def _reduce_to(self, reduce_op, value, destinations, options):

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
    687       try:
    688         with conversion_ctx:
--> 689           return converted_call(f, args, kwargs, options=options)
    690       except Exception as e:  # pylint:disable=broad-except
    691         if hasattr(e, 'ag_error_metadata'):

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py in converted_call(f, args, kwargs, caller_fn_scope, options)
    375 
    376   if not options.user_requested and conversion.is_allowlisted(f):
--> 377     return _call_unconverted(f, args, kwargs, options)
    378 
    379   # internal_convert_user_code is for example turned off when issuing a dynamic

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py in _call_unconverted(f, args, kwargs, options, update_cache)
    456 
    457   if kwargs is not None:
--> 458     return f(*args, **kwargs)
    459   return f(*args)
    460 

~/opt/anaconda3/lib/python3.8/site-packages/keras/engine/training.py in run_step(data)
   1133 
   1134             def run_step(data):
-> 1135                 outputs = model.train_step(data)
   1136                 # Ensure counter is updated only if `train_step` succeeds.
   1137                 with tf.control_dependencies(_minimum_control_deps(outputs)):

~/opt/anaconda3/lib/python3.8/site-packages/keras/engine/training.py in train_step(self, data)
    991         # Run forward pass.
    992         with tf.GradientTape() as tape:
--> 993             y_pred = self(x, training=True)
    994             loss = self.compute_loss(x, y, y_pred, sample_weight)
    995         self._validate_target_and_loss(y, loss)

~/opt/anaconda3/lib/python3.8/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     59     def error_handler(*args, **kwargs):
     60         if not tf.debugging.is_traceback_filtering_enabled():
---> 61             return fn(*args, **kwargs)
     62 
     63         filtered_tb = None

~/opt/anaconda3/lib/python3.8/site-packages/keras/engine/training.py in __call__(self, *args, **kwargs)
    555             layout_map_lib._map_subclass_model_variable(self, self._layout_map)
    556 
--> 557         return super().__call__(*args, **kwargs)
    558 
    559     @doc_controls.doc_in_current_and_subclasses

~/opt/anaconda3/lib/python3.8/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     59     def error_handler(*args, **kwargs):
     60         if not tf.debugging.is_traceback_filtering_enabled():
---> 61             return fn(*args, **kwargs)
     62 
     63         filtered_tb = None

~/opt/anaconda3/lib/python3.8/site-packages/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
   1095                     self._compute_dtype_object
   1096                 ):
-> 1097                     outputs = call_fn(inputs, *args, **kwargs)
   1098 
   1099                 if self._activity_regularizer:

~/opt/anaconda3/lib/python3.8/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
    153             else:
    154                 new_e = e
--> 155             raise new_e.with_traceback(e.__traceback__) from None
    156         finally:
    157             del signature

~/opt/anaconda3/lib/python3.8/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     94         bound_signature = None
     95         try:
---> 96             return fn(*args, **kwargs)
     97         except Exception as e:
     98             if hasattr(e, "_keras_call_info_injected"):

~/opt/anaconda3/lib/python3.8/site-packages/keras/engine/sequential.py in call(self, inputs, training, mask)
    423                 kwargs["training"] = training
    424 
--> 425             outputs = layer(inputs, **kwargs)
    426 
    427             if len(tf.nest.flatten(outputs)) != 1:

~/opt/anaconda3/lib/python3.8/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     59     def error_handler(*args, **kwargs):
     60         if not tf.debugging.is_traceback_filtering_enabled():
---> 61             return fn(*args, **kwargs)
     62 
     63         filtered_tb = None

~/opt/anaconda3/lib/python3.8/site-packages/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
   1063         ):
   1064 
-> 1065             input_spec.assert_input_compatibility(
   1066                 self.input_spec, inputs, self.name
   1067             )

~/opt/anaconda3/lib/python3.8/site-packages/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    248             ndim = x.shape.rank
    249             if ndim is not None and ndim < spec.min_ndim:
--> 250                 raise ValueError(
    251                     f'Input {input_index} of layer "{layer_name}" '
    252                     "is incompatible with the layer: "

ValueError: in user code:

    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/engine/training.py", line 1160, in train_function  *
        return step_function(self, iterator)
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/engine/training.py", line 1146, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py", line 1315, in run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py", line 2891, in call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py", line 3692, in _call_for_each_replica
        return fn(*args, **kwargs)
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/engine/training.py", line 1135, in run_step  **
        outputs = model.train_step(data)
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/engine/training.py", line 993, in train_step
        y_pred = self(x, training=True)
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 61, in error_handler
        return fn(*args, **kwargs)
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/engine/training.py", line 557, in __call__
        return super().__call__(*args, **kwargs)
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 61, in error_handler
        return fn(*args, **kwargs)
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/engine/base_layer.py", line 1097, in __call__
        outputs = call_fn(inputs, *args, **kwargs)
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 155, in error_handler
        raise new_e.with_traceback(e.__traceback__) from None
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 96, in error_handler
        return fn(*args, **kwargs)
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/engine/sequential.py", line 425, in call
        outputs = layer(inputs, **kwargs)
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 61, in error_handler
        return fn(*args, **kwargs)
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/engine/base_layer.py", line 1065, in __call__
        input_spec.assert_input_compatibility(
    File "/Users/mcm66103/opt/anaconda3/lib/python3.8/site-packages/keras/engine/input_spec.py", line 250, in assert_input_compatibility
        raise ValueError(

    ValueError: Exception encountered when calling layer "sequential_24" "                 f"(type Sequential).
    
    Input 0 of layer "dense_25" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (None,)
    
    Call arguments received by layer "sequential_24" "                 f"(type Sequential):
      • inputs=tf.Tensor(shape=(None,), dtype=int64)
      • training=True
      • mask=None

CodePudding user response:

You are missing the feature dimension necessary for the Dense layer, since your model is inferring the input shape based on the data that you feed, so try:

import tensorflow as tf
import numpy as np

X = tf.constant(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))[:, None]
y = tf.constant(np.array([1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31]))

model = tf.keras.Sequential([
    tf.keras.layers.Dense(1)
])

model.compile(
    loss=tf.keras.losses.mae,
    optimizer=tf.keras.optimizers.SGD(),
    metrics=["mae"]
)

model.fit(X, y, epochs=10)

You could also use X = tf.expand_dims(X, axis=-1).

CodePudding user response:

Try adding an input layer in the Sequential model:

X = tf.constant(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
y = tf.constant(np.array([1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31]))

model = tf.keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=(1,)), # This will take care of dimensions
    tf.keras.layers.Dense(1)
])

model.compile(
    loss=tf.keras.losses.mae,
    optimizer=tf.keras.optimizers.SGD(),
    metrics=["mae"]
)

model.fit(X, y, epochs=10)

For reference see: tf.keras.InputLayer

CodePudding user response:

You should try this code...

X = tf.constant(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
y = tf.constant(np.array([1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31]))

model = tf.keras.Sequential([
    tf.keras.layers.Dense(1)
])

model.compile(
    loss=tf.keras.losses.mae,
    optimizer=tf.keras.optimizers.SGD(),
    metrics=["mae"]
)
X = tf.expand_dims(X , axis=0)
y = tf.expand_dims(y , axis=0)
model.fit(X, y, epochs=10)

Output

Epoch 1/10
1/1 [==============================] - 1s 777ms/step - loss: 14.0288 - mae: 14.0288
Epoch 2/10
1/1 [==============================] - 0s 8ms/step - loss: 11.6894 - mae: 11.6894
Epoch 3/10
1/1 [==============================] - 0s 9ms/step - loss: 10.2720 - mae: 10.2720
Epoch 4/10
1/1 [==============================] - 0s 9ms/step - loss: 9.4745 - mae: 9.4745
Epoch 5/10
1/1 [==============================] - 0s 9ms/step - loss: 8.9153 - mae: 8.9153
Epoch 6/10
1/1 [==============================] - 0s 10ms/step - loss: 8.6282 - mae: 8.6282
Epoch 7/10
1/1 [==============================] - 0s 9ms/step - loss: 8.4167 - mae: 8.4167
Epoch 8/10
1/1 [==============================] - 0s 5ms/step - loss: 8.3848 - mae: 8.3848
Epoch 9/10
1/1 [==============================] - 0s 9ms/step - loss: 8.3529 - mae: 8.3529
Epoch 10/10
1/1 [==============================] - 0s 10ms/step - loss: 8.3210 - mae: 8.3210
  • Related