Suppose I have a base model take two inputs and output single value:
# define two input
input1 = keras.Input(shape=(100,), dtype=tf.int8)
input2 = keras.Input(shape=(20,), dtype=tf.int8)
# DNN for onehot feature
dense1 = Dense(32, activation='relu')(input1)
dense2 = Dense(4, activation='relu')(input2 )
# output
output = Dense(1, activation='sigmoid')(Concatenate(axis=1)([dense1 , dense2 ]))
# define base model
item_base_model = keras.Model(inputs=[input1, input2], outputs=output, name="base_model")
Then I have a model take (None,100) and (None, 20) arrays as input :
# define input list
input1_list = keras.Input(shape=(None, 100],), dtype=tf.int8)
input2_list = keras.Input(shape=(None, 20,), dtype=tf.int16)
I want to ask that how I can call the based model for each input in the input1_list and input2_list and get their output as a tensor with shape (None,). Finally, I will train the entire model
CodePudding user response:
Something like this should work -
out = tf.keras.layers.Lambda(
lambda x: tf.map_fn(item_base_model, x, fn_output_signature=tf.TensorSpec(shape=[None, 1], dtype=tf.float32))
)([input1_list, input2_list])
new_model = tf.keras.Model(inputs=[input1_list, input2_list], outputs=out, name="new_model")
CodePudding user response:
import tensorflow as tf
class myLayer(tf.keras.layers.Layer):
def __init__(self):
super(myLayer, self).__init__()
self._supports_ragged_inputs = True
def call(self, inputs):
print(type(inputs))
print(len(inputs))
return tf.map_fn(fn=item_base_model, elems=inputs, fn_output_signature=tf.TensorSpec(shape=[None, 1],dtype=tf.float32))
output = myLayer()([input1_list, input2_list])
I do not know is there anything wrong in these code. But thanks for Saswata's help. Really helpful.