Keras ValueError: No data provided for "add". Need data for each key in: ['add'] using Model API

Viewed 83

I would like to use Keras for binary classification. Below is my code:

input1 = tf.keras.Input(batch_size=batch_size, shape=(len1,))
output1 = tf.keras.layers.Dense(units=1, kernel_initializer='glorot_uniform')(input1)
input2 = tf.keras.Input(batch_size=batch_size, shape=(len2,))
output2 = tf.keras.layers.Dense(units=1, kernel_initializer='glorot_uniform')(input2)
inputs = [input1, input2]
outputs = [output1, output2]
logits = tf.keras.layers.Add()(outputs)
model = tf.keras.Model(inputs=inputs, outputs=logits)
model.compile(optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.001),
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True))

This would give me error ValueError: No data provided for "add". Need data for each key in: ['add']

I tried logits = tf.keras.layers.add(outputs), this would give the same error.

But if after logits = tf.keras.layers.Add()(outputs) I use:

probs = tf.keras.layers.Softmax()(logits)
model = tf.keras.Model(inputs=inputs, outputs=probs)
model.compile(optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.001),
              loss=tf.keras.losses.BinaryCrossentropy())

It will work. I'm using tensorflow 2.0

What's happening here?

1 Answers

I'm using tf.dataset here. It turns out I need to specify the correct input and output names for the tensors as tf.dataset is looking for these names while feeding the data to keras.

so input1 and input2 should have the corresponding name of the input features, and since the label is named label, it should be logits = tf.keras.layers.Add(name="label")(outputs)

Related