Skip to content

Dense layer in nb_ch04_03.ipynb raises ValueError due to batch_input_shape #18

Description

@AdamAdham

Issue

When running the current notebook, the following cell:

# custom loss function
def my_loss(y_true,y_pred): #A
  loss = -tf.reduce_sum(tf.math.log(f(y_true,y_pred)))
  return loss

# setup NN w/o hidden layer and with output node
model = Sequential() 
model.add(Dense(1, activation='linear', 
                batch_input_shape=(None, 1)))
model.compile(loss=my_loss,optimizer="adam") # use custom loss

Exception:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[/tmp/ipython-input-175710984.py](https://colab.research.google.com/github/tensorchiefs/dl_book/blob/master/chapter_04/nb_ch04_03.ipynb#) in <cell line: 0>()
      6 # setup NN w/o hidden layer and with output node
      7 model = Sequential()
----> 8 model.add(Dense(1, activation='linear', 
      9                 batch_input_shape=(None, 1)))
     10 model.compile(loss=my_loss,optimizer="adam") # use custom loss

1 frames[/usr/local/lib/python3.12/dist-packages/keras/src/layers/core/dense.py](https://colab.research.google.com/github/tensorchiefs/dl_book/blob/master/chapter_04/nb_ch04_03.ipynb#) in __init__(self, units, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, lora_rank, lora_alpha, **kwargs)
     91         **kwargs,
     92     ):
---> 93         super().__init__(activity_regularizer=activity_regularizer, **kwargs)
     94         self.units = units
     95         self.activation = activations.get(activation)

[/usr/local/lib/python3.12/dist-packages/keras/src/layers/layer.py](https://colab.research.google.com/github/tensorchiefs/dl_book/blob/master/chapter_04/nb_ch04_03.ipynb#) in __init__(self, activity_regularizer, trainable, dtype, autocast, name, **kwargs)
    289             self._input_shape_arg = input_shape_arg
    290         if kwargs:
--> 291             raise ValueError(
    292                 "Unrecognized keyword arguments "
    293                 f"passed to {self.__class__.__name__}: {kwargs}"

ValueError: Unrecognized keyword arguments passed to Dense: {'batch_input_shape': (None, 1)}

Context

  • This is due to recent changes in Keras / TensorFlow.

  • Dense layers no longer accept batch_input_shape directly; use input_shape=(1,) instead.

  • The rest of the notebook may need adjustments if other layers use similar deprecated arguments.

Suggested fix

Replace batch_input_shape=(None, 1) with input_shape=(1,).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions