googlecolab / colabtools

Python libraries for Google Colaboratory
Apache License 2.0
2.17k stars 707 forks source link

Bug in Representation with a Feature Cross.ipynb #4752

Open ranjitkpaulraj opened 1 month ago

ranjitkpaulraj commented 1 month ago

This notebook is very outdated.

Running cell 36 gives ValueError: No loss to compute. Provide a 'loss' argument in 'compile()'

There are two other errors in this notebook. These errors are easily fixable as suggested by Gemini.

  1. RMSprop optimizer is no longer in the experimental
  2. tf.keras.layers.Concatenate doesn't take dict values

The remaining issue "No loss to compute" is still not solvable, Gemini couldn't help here.

What web browser you are using Chrome

Additional context https://colab.research.google.com/github/google/eng-edu/blob/main/ml/cc/exercises/representation_with_a_feature_cross.ipynb

c0rRupT9 commented 1 month ago

I updated the code myself as following it will work here take a look

        @title Define functions to create and train a model, and a plotting function
def create_model(my_inputs, my_outputs, my_learning_rate):

  model = tf.keras.Model(inputs=my_inputs, outputs=my_outputs)

  model.compile(optimizer=tf.keras.optimizers.RMSprop(
      learning_rate=my_learning_rate),
      ``loss="mean_squared_error"
      metrics=[tf.keras.metrics.RootMeanSquaredError()])

  return model

def train_model(model, dataset, epochs, batch_size, label_name):
  """Feed a dataset into the model in order to train it."""

  features = {name:np.array(value) for name, value in dataset.items()}
  label = np.array(features.pop(label_name))
  history = model.fit(x=features, y=label, batch_size=batch_size,
                      epochs=epochs, shuffle=True)

  epochs = history.epoch

  hist = pd.DataFrame(history.history)
  rmse = hist["loss"]

  return epochs, rmse

def plot_the_loss_curve(epochs, rmse):
  """Plot a curve of loss vs. epoch."""

  plt.figure()
  plt.xlabel("Epoch")
  plt.ylabel("Root Mean Squared Error")

  plt.plot(epochs, rmse, label="Loss")
  plt.legend()
  plt.ylim([rmse.min()*0.94, rmse.max()* 1.05])
  plt.show()

print("Defined the create_model, train_model, and plot_the_loss_curve functions.")

following lines were changed : ` rmse = hist["loss"] model.compile(optimizer=tf.keras.optimizers.RMSprop( learning_rate=my_learning_rate), loss="mean_squared_error", metrics=[tf.keras.metrics.RootMeanSquaredError()])

the second part is as following

learning_rate = 0.05
epochs = 30
batch_size = 100
label_name = 'median_house_value'

preprocessing_layer = tf.keras.layers.Concatenate()(list(inputs.values()))

dense_output = layers.Dense(units=1, name='dense_layer')(preprocessing_layer)

outputs = {
  'dense_output': dense_output
}

my_model = create_model(inputs, dense_output, learning_rate)

epochs, rmse = train_model(my_model, train_df, epochs, batch_size, label_name)

my_model.summary(expand_nested=True)

plot_the_loss_curve(epochs, rmse)

print("\n: Evaluate the new model against the test set:")
test_features = {name:np.array(value) for name, value in test_df.items()}
test_label = np.array(test_features.pop(label_name))
my_model.evaluate(x=test_features, y=test_label, batch_size=batch_size)

The following lines were changed:

                           ` preprocessing_layer = tf.keras.layers.Concatenate()(list(inputs.values()))`

       here dict are changed to lists

                               `my_model = create_model(inputs, dense_output, learning_rate)`

      insted of outputs use dense_output the loss problem will resolve do the same to errors in the following cells too
ranjitkpaulraj commented 1 month ago

Thanks. Yes, using dense_output solves the problem related to 'loss'.

I will leave this issue open, until the notebook is corrected.