Search code examples
tensorflowtensorflow2.0

Concate two submodel in tensorflow


I want to build a simple model same as the figure. I have a submodel for each one and in the submodel I have the lstm. However, I dont know how exactly I should define the input and the out put. Here is a simple code of my problem, and the submodels are exactly same as each other here. Could you help me how to run this model? Thank you. Here is the model that I want to create: enter image description here

import numpy as np
import tensorflow as tf
from keras.models import Sequential, Model,load_model
from keras.layers import Dense, Dropout, Activation, Flatten, LSTM,  Input, concatenate
import keras


X_train1 = np.random.randint(10, size = (10, 20, 28))
Y_train1= np.random.randint(10, size = (10, 28))

X_train2 = np.random.randint(10, size = (10, 10, 28))
Y_train2= np.random.randint(10, size = (10, 28))

def sub_model1(X_train, Y_train):  
    model = Sequential()
    model.add(LSTM(100, activation='linear', input_shape=(X_train.shape[1], X_train.shape[2]), return_sequences=True))
    model.add(LSTM(32, activation='linear', return_sequences=False))
    model.add(Dense(100, activation='linear'))
    return model.add(Dense(Y_train.shape[1], activation='linear'))


model1 = sub_model1(X_train1, Y_train1)
model2 = sub_model1(X_train2, Y_train2)

concat   = concatenate([model1, model2])

output   = Dense(28, activation="linear")(concat)

#model    =  Model(inputs = INPUT, outputs = output)
#how to define that?

model.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics = ['MAE'])

history  = model.fit(X_train, Y_train, epochs =2, batch_size = 100)

Solution

  • You need to use keras.Input layers and you can concat the following way:

    def sub_model1(x, n_dim=28):  
    
     x= LSTM(100, activation='linear', return_sequences=True)(x)
     x= LSTM(32, activation='linear', return_sequences=False)(x)
     x= Dense(100, activation='linear')(x)
     return Dense(n_dim, activation='linear')(x)
    
    input_1 = tf.keras.layers.Input([20, 28], dtype=tf.float32, name='input_1')
    input_2 = tf.keras.layers.Input([10, 28], dtype=tf.float32, name='input_2')
    x1 = sub_model1(input_1)
    x2 = sub_model1(input_2)
    
    concat   = concatenate([x1, x2])
    
    output   = Dense(28, activation="linear")(concat)
    
    model = tf.keras.models.Model(inputs=[input_1, input_2], outputs=output)
    
    #check output size
    model([X_train1, X_train2]).shape
    #TensorShape([10, 28])