Train a model on multiple input dataset

Hi.

import tensorflow as tf 
from tensorflow import keras 
import numpy as np 

a_train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train))
a_valid_ds = tf.data.Dataset.from_tensor_slices((x_valid, y_valid))

b_train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train))
b_valid_ds = tf.data.Dataset.from_tensor_slices((x_valid, y_valid))


a_input = keras.layers.Input(shape=(4,4), name='a_input')
x = keras.layers.Flatten()(a_input)
x = keras.layers.Dense(5)(x)

b_input = keras.layers.Input(shape=(4,4), name='b_input')
y = keras.layers.Flatten()(b_input)
y = keras.layers.Dense(5)(y)

z = keras.layers.concatenate([x,y])
z = keras.layers.Dense(2)(z)

model = keras.models.Model(inputs=[a_input, b_input], outputs=z)

model.compile(loss='mse', optimizer='adam', metrics=['mae'])

I tried to train the model:

model.fit([a_train_ds, b_train_ds], validation_data=[a_valid_ds, b_valid_ds], epochs=1)

I got the following error:
ValueError: Failed to find data adapter that can handle input: ( containing values of types {“”}),

Hi @youb ,

Please find the working gist, i have modified one change in your code and it is working as expected.

model.fit({'a_input': x_train, 'b_input': x_train}, y_train, validation_data=({'a_input': x_valid, 'b_input': x_valid}, y_valid), epochs=10)

To train the model with multiple inputs, we need to pass the input data as a dictionary, where the keys are the names of the input layers and the values are the corresponding input data arrays.

By specifying the input data in this way, Keras knows which input data corresponds to each input layer of the model during training and evaluation.

I hope this helps!

Thanks

HI.
Thank you for reply;
In this case we should have the same y_train for a_train_ds and b_train_ds. because already a_train_ds and b_train_ds contain y_train but are differents and that is due to shuffle process.
Thank you

1 Like

The solution that I found is:

a_xtrain, y_train = a_train_ds.map(lamba x,y: x,y)
b_xtrain          = b_train_ds.map(lambda x,y: x)

train_ds = tf.data.Dataset.zip(((a_xtrain, b_xtrain), y_train).batch(buffer_size=32)

Tested and it works fine.
Thank you