Issue
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
Solution
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")
Answered By - Saswata Chakravarty
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.