Search code examples
pythonkerasneural-networkconcatenationlstm

Keras concatenate Sequential and Dense models


I have the following models:

Dense Model:

def dense_model(num_features):
  inputs = Input(shape=(num_features,), dtype=tf.float32)
  layers = Dense(32, activation='relu')(inputs)
  model = Model(inputs=inputs, outputs=layers)
  
  return model

enter image description here

LSTM Model:

def lstm_model(num_features):

    inputs = Input(shape=[None, num_features], dtype=tf.float32, ragged=True)
    layers = LSTM(16, activation='tanh')(
        inputs.to_tensor(), mask=tf.sequence_mask(inputs.row_lengths()))

    layers = BatchNormalization()(layers)
    layers = Dense(16,activation='relu')(layers)
    layers = Dense(1, activation='sigmoid')(layers)
    
    model = Model(inputs=inputs, outputs=layers)
    model.compile(loss='mse', optimizer='adam', metrics=['mse'])
        
    return model

enter image description here


I'm trying to concatenate the two networks like so:

def concatenate_model(num_features):
    model_1 = dense_model(10)
    inputs = Input(shape=[None, num_features], dtype=tf.float32, ragged=True)
    layers = LSTM(16, activation='tanh')(
        inputs.to_tensor(), mask=tf.sequence_mask(inputs.row_lengths()))

    layers = BatchNormalization()(layers)
    layers = Dense(16,activation='relu')(concatenate([layers,model_1]))
    layers = Dense(1, activation='sigmoid')(layers)
    
    model = Model(inputs=inputs, outputs=layers)
    model.compile(loss='mse', optimizer='adam', metrics=['mse'])
        
    return model

to get the following result: enter image description here

But I get an error:

ValueError: as_list() is not defined on an unknown TensorShape.

Here is a Colab that demonstrate my issue. What am I missing?


Solution

  • Try this:

    import tensorflow as tf
    
    def dense_model(num_features):
      inputs = tf.keras.layers.Input(shape=(num_features,), dtype=tf.float32)
      layers = tf.keras.layers.Dense(32, activation='relu')(inputs)
      model = tf.keras.Model(inputs=inputs, outputs=layers)
      
      return model
    
    def lstm_model(num_features):
    
        model_1 = dense_model(10)
        inputs = tf.keras.layers.Input(shape=[None, num_features], dtype=tf.float32, ragged=True)
        layers = tf.keras.layers.LSTM(16, activation='tanh')(
            inputs.to_tensor(), mask=tf.sequence_mask(inputs.row_lengths()))
    
        layers = tf.keras.layers.BatchNormalization()(layers)
        concatenated = tf.keras.layers.Concatenate(axis=-1)([layers, model_1.output])
        layers = tf.keras.layers.Dense(16,activation='relu')(concatenated)
        layers = tf.keras.layers.Dense(1, activation='sigmoid')(layers)
        
        model = tf.keras.Model(inputs=[inputs, model_1.input], outputs=layers)
        model.compile(loss='mse', optimizer='adam', metrics=['mse'])
            
        return model
    
    model = lstm_model(20)
    dot_img_file = '/model.png'
    tf.keras.utils.plot_model(model, to_file=dot_img_file, show_shapes=True)
    

    enter image description here