0

I'm using for the first time tf.data.dataset to feed a model. I look on some exemple but don't find how to use multi-inputs on a 2 heads model.

My first input as shape[nb_samples, nb_timesteps, nb_features] to feed LSTM first head. My second input as shape[nb_samples, nb_features] to feed dense second head. The output is a sequence of 8 values ex:

input_1 = [14000, 10, 5]
input_2 = [14000, 6]
output = [14000, 8]

now how I turn my numpy inputs to dataset and pass it to the model

input_1 = tf.data.Dataset.from_tensor_slices((X))
input_2= tf.data.Dataset.from_tensor_slices((Xphysio))
output = tf.data.Dataset.from_tensor_slices((y))
combined_dataset = tf.data.Dataset.zip(((inputs_hydro, inputs_static), output))

history = model.fit(combined_dataset)

but at this stage, how I must "split" my input to direct it to the good head model?? Here an simple exemple of the model and how I direct my input inside it...

tensor_input1 = Input(shape=(10, 5))
tensor_input2 = Input(shape=(6, ))

x = LSTM(100, return_sequences=False)(tensor_input1)
x = Dropout(rate = params['dropout1'])(x)
x = Dense(50)(x)
merge = concatenate([x, tensor_input2])
x = Dense(50)(merge)
x = Dropout(rate = params['dropout1'])(x)
output = Dense(8)(x)

model = Model(inputs=[tensor_input1, tensor_input2], outputs=output)

If I understand, while using tf.data.dataset it is not require to specify the shape of the inputs like Input(shape[.....]).

thank for your help, and sorry if my english is not top, I'm working on it too

Jonathan Roy
  • 405
  • 1
  • 6
  • 18

1 Answers1

1

The complete solution is probably as follows

import tensorflow as tf
import numpy as np

input_1 = tf.data.Dataset.from_tensor_slices(np.random.normal(size=[14, 10, 5]).astype(np.float32))
input_2= tf.data.Dataset.from_tensor_slices(np.random.normal(size=[14, 6]).astype(np.float32))
output = tf.data.Dataset.from_tensor_slices(np.random.normal(size=[14, 8]).astype(np.float32))
combined_dataset = tf.data.Dataset.zip(((input_1, input_2), output))
input_dataset = combined_dataset.batch(2)

tensor_input1 = tf.keras.Input(shape=(10, 5))
tensor_input2 = tf.keras.Input(shape=(6,))
x = tf.keras.layers.LSTM(100, return_sequences=False)(tensor_input1)
x = tf.keras.layers.Dropout(rate=0.1)(x)
x = tf.keras.layers.Dense(50)(x)
merge = tf.keras.layers.Concatenate(axis=1)([x, tensor_input2])
x = tf.keras.layers.Dense(50)(merge)
x = tf.keras.layers.Dropout(rate=0.1)(x)
output = tf.keras.layers.Dense(8)(x)
model = tf.keras.Model(inputs=[tensor_input1, tensor_input2], outputs=output)
model.compile(loss="mse")

history = model.fit(input_dataset)
# 7/7 [==============================] - 2s 6ms/step - loss: 1.6438
Alexey Tochin
  • 653
  • 5
  • 8