Value Error for Tensorflow pandas tutorial

model.fit(dict(numeric_features), target, epochs = 5, batch_size = BATCH_SIZE)

ValueError: Missing data for input “normalization_input”. You passed a data dictionary with keys [‘age’, ‘thalach’, ‘trestbps’, ‘chol’, ‘oldpeak’]. Expected the following keys: [‘normalization_input’]

@Ata_Tekeli,

I am successfully able to execute the pandas tutorial. Please refer to the gist.

Thank you!

I follow the tutorial and I get the same error

import pandas as pd
import tensorflow as tf

SHUFFLE_BUFFER = 500
BATCH_SIZE = 2

csv_file = tf.keras.utils.get_file('heart.csv', 'https://storage.googleapis.com/download.tensorflow.org/data/heart.csv')

df = pd.read_csv(csv_file)

df.head()

df.dtypes

target = df.pop('target')

numeric_feature_names = ['age', 'thalach', 'trestbps', 'chol', 'oldpeak']
numeric_features = df[numeric_feature_names]
numeric_features.head()

tf.convert_to_tensor(numeric_features)

normalizer = tf.keras.layers.Normalization(axis = -1)
normalizer.adapt(numeric_features)

normalizer(numeric_features.iloc[:3])

def get_basic_model():
  model = tf.keras.Sequential([
      normalizer,
      tf.keras.layers.Dense(10, activation = 'relu'),
      tf.keras.layers.Dense(10, activation = 'relu'),
      tf.keras.layers.Dense(1)
  ])

  model.compile(optimizer = 'adam',
                loss = tf.keras.losses.BinaryCrossentropy(from_logits = True),
                metrics = ['accuracy'])
  
  return model

model = get_basic_model()
model.fit(numeric_features, target, epochs = 15, batch_size = BATCH_SIZE)

numeric_dataset = tf.data.Dataset.from_tensor_slices((numeric_features, target))

for row in numeric_dataset.take(3):
  print(row)

numeric_batches = numeric_dataset.shuffle(1000).batch(BATCH_SIZE)

model = get_basic_model()
model.fit(numeric_batches, epochs = 15)

numeric_dict_ds = tf.data.Dataset.from_tensor_slices((dict(numeric_features), target))

for row in numeric_dict_ds.take(3):
  print(row)

def stack_dict(inputs, fun = tf.stack):
  values = []
  for key in sorted(inputs.keys()):
    values.append(tf.cast(inputs[key], tf.float32))

  return fun(values, axis = -1)

model.fit(dict(numeric_features), target, epochs = 5, batch_size = BATCH_SIZE)

Error code:ValueError: Missing data for input “normalization_1_input”. You passed a data dictionary with keys [‘age’, ‘thalach’, ‘trestbps’, ‘chol’, ‘oldpeak’]. Expected the following keys: [‘normalization_1_input’]

@Ata_Tekeli,

You should include the following code snippet before your model.fit

class MyModel(tf.keras.Model):
  def __init__(self):
    # Create all the internal layers in init.
    super().__init__(self)

    self.normalizer = tf.keras.layers.Normalization(axis=-1)

    self.seq = tf.keras.Sequential([
      self.normalizer,
      tf.keras.layers.Dense(10, activation='relu'),
      tf.keras.layers.Dense(10, activation='relu'),
      tf.keras.layers.Dense(1)
    ])

  def adapt(self, inputs):
    # Stack the inputs and `adapt` the normalization layer.
    inputs = stack_dict(inputs)
    self.normalizer.adapt(inputs)

  def call(self, inputs):
    # Stack the inputs
    inputs = stack_dict(inputs)
    # Run them through all the layers.
    result = self.seq(inputs)

    return result

model = MyModel()

model.adapt(dict(numeric_features))

model.compile(optimizer='adam',
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              metrics=['accuracy'],
              run_eagerly=True)

Please refer to the working gist.

Thank you!