C1_W3_Lab_2_custom-dense-layer problem with the shape of data xs

Hi,
As you know, the current supported version of Tensorflow in Colab is 2.8.0. So when I try to run the lab in Colab, it gives me the error below even though it works just fine with Tensorflow 2.1.0

Anyone knows how to fix this ?

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-f356b2f628c7> in <module>()
     10 # configure and train the model
     11 model.compile(optimizer='sgd', loss='mean_squared_error')
---> 12 model.fit(xs, ys, epochs=500,verbose=0)
     13 
     14 # perform inference

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
        y_pred = self(x, training=True)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "<ipython-input-2-41acae60e8f7>", line 17, in build
        dtype='float32'),

    ValueError: Exception encountered when calling layer "sequential" (type Sequential).
    
    Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.
    
    Call arguments received:
      • inputs=tf.Tensor(shape=(None,), dtype=float32)
      • training=True
      • mask=None

this happens when calling:

model.fit(xs, ys, epochs=500,verbose=0)

C1_W3_Lab_2_custom-dense-layer.ipynb (5.6 KB)

The shape of xs is (6,). In this case, when build() in SimpleDense() is called, input_shape =(None,). So, input_shape[-1] does not work.
One potential fix is to reshape xs to explicitly be (6,1), which will be (None,1) for build(). Then, input_shape[-1] should work.

I just made it run on TF2.3. Looks like build() is called with the input_shape=(None,1) even in the case of xs=(6,). That’s the reason why it worked on the older version of TF.

3 Likes

This notebook was sent to @husam ~23 hours ago which works on tensorflow 2.9.1

C1_W3_Lab_2_custom-dense-layer.ipynb (6.2 KB)

3 Likes

Yes, this is another way to set input_shape, and looks smart.

The reason that I did not select it is the timing to call “build()”.

I though the design point for this model is to set the input (input_shape) at the timing of model.fit().
If we set input_shape as part of a model, then, build() is called when a model is created. (before model.compile). If we do not set, then, build() is called at model.fit() with the actual data.

I might think too much…

Anyway, Sorry for interrupting. Good to know that you already provided the guidance.

No worries. Post away.