Raquib-ul Alam
Raquib-ul Alam

Reputation: 574

Multiple inputs of keras model with tf.data.Dataset.from_generator in Tensorflow 2

I am trying to implement a model in keras that will have multiple inputs:

To feed that model, I want to write a generator to use with tf.data.Dataset.from_generator. From the docs of from_generator, its not clear to me how I should provide its parameters output_types, output_shapes. Can anyone help me with this?

Upvotes: 8

Views: 6286

Answers (3)

MarkV
MarkV

Reputation: 1062

I had a similar issue, and it took me many tries to get the structure right for those inputs. Here's an example of a network with 3 inputs and 2 outputs, complete to the .fit call.

The following works in tensorflow 2.1.0

import tensorflow as tf
import numpy as np

def generator(N=10):
    """
    Returns tuple of (inputs,outputs) where
    inputs  = (inp1,inp2,inp2)
    outputs = (out1,out2)
    """
    dt=np.float32
    for i in range(N):
        inputs  = (np.random.rand(N,3,3,1).astype(dt), 
                   np.random.rand(N,3,3,1).astype(dt), 
                   np.random.rand(N,3,3,1).astype(dt))
        outputs = (np.random.rand(N,3,3,1).astype(dt),
                   np.random.rand(N,3,3,1).astype(dt))
        yield inputs,outputs

# Create dataset from generator
types = ( (tf.float32,tf.float32,tf.float32),
          (tf.float32,tf.float32) )
shapes = (([None,3,3,1],[None,3,3,1],[None,3,3,1]),
          ([None,3,3,1],[None,3,3,1]))
data = tf.data.Dataset.from_generator(generator,
                                      output_types=types,
                                      output_shapes=shapes
                                     )
# Define a model
inp1 = tf.keras.Input(shape=(3,3,1),name='inp1')
inp2 = tf.keras.Input(shape=(3,3,1),name='inp2')
inp3 = tf.keras.Input(shape=(3,3,1),name='inp3')
out1 = tf.keras.layers.Conv2D(1,kernel_size=3,padding='same')(inp1)
out2 = tf.keras.layers.Conv2D(1,kernel_size=3,padding='same')(inp2)
model = tf.keras.Model(inputs=[inp1,inp2,inp3],outputs=[out1,out2])
model.compile(loss=['mse','mse'])

# Train
model.fit(data)


Upvotes: 9

Lhemerly
Lhemerly

Reputation: 21

model.fit([input_1, input_2, input_3], y, epochs=EPOCHS)

You got to have n(3 in the case above) input layers in your model.

Upvotes: 0

Stewart_R
Stewart_R

Reputation: 14525

So assuming you have a generator that is similar to this mock:

def dummy_generator():
  number_of_records = 100

  for i in range(100):
    an_image = tf.random.uniform((200,200,3))
    some_numbers = tf.random.uniform((50,))
    signal1 = tf.random.uniform((50000,))
    signal2 = tf.random.uniform((100000,))
    signal3 = tf.random.uniform((100000,))
    yield an_image, some_numbers, signal1, signal2, signal3

each record is of datatype float32 so the output types are easy:

out_types = (tf.float32, tf.float32, tf.float32, tf.float32, tf.float32)

for the output shapes we just list the shapes in the same order:

out_shapes = ((200,200,3), (50,), (50000,), (100000,), (100000,))

so now we can just call from_generator:

ds = tf.data.Dataset.from_generator(dummy_generator, 
                                    output_types=out_types,
                                    output_shapes=out_shapes)

Upvotes: 5

Related Questions