Could not execute python script with instructions in the docs

Hello all!
I am new to this forum and just wanted to comment on something I observed.

I worked my way through the tutorial but got stuck at the part int the overfit_and_underfit tutorial where the function “compile_and_fit” is defined. My cli always threw the error “ValueError: Arguments target and output must have the same rank (ndim). Received: target.shape=(None,), output.shape=(None, 1)”. So I ran the code in the above listed jupyter notebook and it worked. I looked through the code and saw that there was a difference between the code listed in the notebook and in the docs. I adjusted the code in my script as it was written in the notebook and it worked.
I just wanted to report that since others might be confused by this like I was.

Hope it helps and have a great day,
Jürgen Thomas

1 Like

Hi @Jurgen_Thomas, I have executed the above mentioned overfit_and_underfit tutorial in colab and did not face any error while executing the code mentioned in the tutorial without making any changes in the code. please refer to this gist. Could you please let us know in which environment you are facing the issue. Thank You.

Hi @Kiran_Sai_Ramineni, the gist you mentioned uses the exact code as the .overfit_and_underfit.ipynb that is listed above the tutorial on Google Colab.
But if you look at the code on the code that is shown on the website than you see the following function (with it’s contents):

def compile_and_fit(model, name, optimizer=None, max_epochs=10000):
  if optimizer is None:
    optimizer = get_optimizer()
  model.compile(optimizer=optimizer,
                loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
                metrics=[
                  tf.keras.metrics.BinaryCrossentropy(
                      from_logits=True, name='binary_crossentropy'),
                  'accuracy'])

  model.summary()

  history = model.fit(
    train_ds,
    steps_per_epoch = STEPS_PER_EPOCH,
    epochs=max_epochs,
    validation_data=validate_ds,
    callbacks=get_callbacks(name),
    verbose=0)
  return history

But the .ipynb uses this code:

  history = model.fit(
    train_ds.map(lambda x, y: (x, tf.expand_dims(y, axis=-1))),
    steps_per_epoch = STEPS_PER_EPOCH,
    epochs=max_epochs,
    validation_data=validate_ds.map(lambda x, y: (x, tf.expand_dims(y, axis=-1))),
    callbacks=get_callbacks(name),
    verbose=0)
  return history

You can find it under the function “def compile_and_fit”.

But I tried it again now and the error does not occur again. I swear it was there when I tried it the first time. It occurred on my machine, then I tested in in the provided .ipynb and it was there too. But now it’s gone… Really strange sometimes.

But thank you for your time, since the error is gone now