Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to train Keras model with multiple inputs in Tensorflow 2.2?

I'd like to train a Keras model with two inputs (one text input and some numerical features), but I struggle to get it working. I've setup a model as described in the Tensorflow documentation about models with multiple inputs:

import tensorflow as tf
from tensorflow.keras import Input, Model, models, layers


def build_model():
    input1 = Input(shape=(50,), dtype=tf.int32, name='x1')
    input2 = Input(shape=(1,), dtype=tf.float32, name='x2')
    y1 = layers.Embedding(1000, 10, input_length=50)(input1)
    y1 = layers.Flatten()(y1)
    y = layers.Concatenate(axis=1)([y1, input2])
    y = layers.Dense(1)(y)
    return Model(inputs=[input1, input2], outputs=y)

Building that model works fine too:

model = build_model()
model.compile(loss='mse')
model.summary()

You can find the output of summary() in this gist.

Then some (dummy) data is needed to get fit onto the model:

def make_dummy_data():
    X1 = tf.data.Dataset.from_tensor_slices(tf.random.uniform([100, 50], maxval=1000, dtype=tf.int32))
    X2 = tf.data.Dataset.from_tensor_slices(tf.random.uniform([100, 1], dtype=tf.float32))
    X = tf.data.Dataset.zip((X1, X2)).map(lambda x1, x2: {'x1': x1, 'x2': x2})
    y_true = tf.data.Dataset.from_tensor_slices(tf.random.uniform([100, 1], dtype=tf.float32))
    return X, y_true


X, y_true = make_dummy_data()
Xy = tf.data.Dataset.zip((X, y_true))
model.fit(Xy, batch_size=32)

...but now fit() fails with an incomprehensible error message (see full message here), which starts with a (probably relevant) warning:

WARNING:tensorflow:Model was constructed with shape (None, 50) for input Tensor("x1:0", shape=(None, 50), dtype=int32), but it was called on an input with incompatible shape (50, 1).

Huh, where did that extra dimension of size 1 come from? And, how do I get rid of it?

One more thing: further simplification of this dummy model by removing the Embedding-layer does suddenly make the model run.

If you want to play around with the above sample, I prepared a notebook on Google Colab for it. Any help appreciated.

like image 927
miho Avatar asked Sep 12 '25 17:09

miho


1 Answers

As the documentation of fit states:

batch_size
Integer or None. Number of samples per gradient update. If unspecified, batch_size will default to 32. Do not specify the batch_size if your data is in the form of datasets, generators, or keras.utils.Sequence instances (since they generate batches).

That is, if you are using a dataset to train your model, it will be expected to provide batches, not individual examples. The shape (50, 1) probably comes from Keras assuming that a single 50-element example was actually a batch of 50 1-element examples.

You can fix it simply like this:

Xy = tf.data.Dataset.zip((X, y_true)).batch(32)
model.fit(Xy)
like image 58
jdehesa Avatar answered Sep 14 '25 07:09

jdehesa