tf.data.Dastaset.from_generator has error when having multiple input

The following code has the following error. Basically there are two inputs, each with 5 time step. I am not sure why, the error is like this:

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Dropout, LSTM, Input, concatenate, Embedding
from tensorflow.keras.optimizers import Adam
import tensorflow as tf
import numpy as np

X0 = np.array([
    [226, 226, 226, 0, 226],
    [1200, 240, 230, 230, 230],
    [1200, 226, 1200, 230, 1200],
    [221, 226, 221, 0, 226],
    [100, 210, 230, 230, 230],
    [120, 226, 1200, 230, 1200],
])

X1 = np.array([
    [226, 22, 22, 0, 226],
    [100, 40, 23, 230, 230],
    [12, 26, 10, 230, 1200],
    [21, 26, 221, 0, 226],
    [10, 10, 23, 23, 230],
    [12, 26, 12, 23, 1200],
])

y = np.array([[1], [1], [1], [1]])

input1 = Input(shape=(5,), name='X0')
X1 = Embedding(10, 32)(input1)
input2 = Input(shape=(5,), name='X1')
X2 = Embedding(10, 32)(input2)
X = concatenate([X1, X2])
X = LSTM(32)(X)
X = Dense(64, activation='relu')(X)
output = Dense(1, activation='sigmoid')(X)

model = Model(inputs=[input1, input2], outputs=output)

batch_size = 2

def generator_aa(X0, X1, y, batch_size):
    while True:
        indices = np.random.permutation(len(X0))
        for i in range(0, len(indices), batch_size):
            batch_indices = indices[i:i + batch_size]
            yield {'X0': X0[batch_indices], 'X1': X1[batch_indices]}, y[batch_indices]

dataset = tf.data.Dataset.from_generator(
    generator_aa,
    args=(X0, X1, y, batch_size),
    output_signature=(
        {
            'X0': tf.TensorSpec(shape=(None, 5), dtype=tf.float32),
            'X1': tf.TensorSpec(shape=(None, 5), dtype=tf.float32)
        },
        tf.TensorSpec(shape=(None, 1), dtype=tf.float32)
    )
)

my_opt = Adam(learning_rate=0.01)
model.compile(loss='binary_crossentropy', optimizer=my_opt, metrics=['accuracy'])

model.fit(dataset, epochs=3, steps_per_epoch=2)

error msg said TypeError Traceback (most recent call last)
in <cell line: 47>()
45 yield {‘X0’: X0[batch_indices], ‘X1’: X1[batch_indices]}, y[batch_indices]
46
—> 47 dataset = tf.data.Dataset.from_generator(
48 generator_aa,
49 args=(X0, X1, y, batch_size),

12 frames
/usr/local/lib/python3.10/dist-packages/keras/engine/keras_tensor.py in array(self, dtype)
281
282 def array(self, dtype=None):
→ 283 raise TypeError(
284 f"You are passing {self}, an intermediate Keras symbolic "
285 "input/output, to a TF API that does not allow registering custom "

TypeError: You are passing KerasTensor(type_spec=TensorSpec(shape=(None, 5, 32), dtype=tf.float32, name=None), name=‘embedding/embedding_lookup/Identity_1:0’, description=“created by layer ‘embedding’”), an intermediate Keras symbolic input/output, to a TF API that does not allow registering custom dispatchers, such as tf.cond, tf.function, gradient tapes, or tf.map_fn. Keras Functional model construction only supports TF API calls that do support dispatching, such as tf.math.add or tf.reshape. Other APIs cannot be called directly on symbolic Kerasinputs/outputs. You can work around this limitation by putting the operation in a custom Keras layer call and calling that layer on this symbolic input/output… Thank you

Hi @daisydaisy, In your code you have defined one array with X1 variable

later you have used the same variable X1 to define embedding

If you print the X1 it will be

<KerasTensor: shape=(None, 5, 32) dtype=float32 (created by layer 'embedding_8')>

not the array. Due to this you are getting the error. Could please try by changing the variables. Thank You.

thank you very much Kiran!!!