Inigo
Inigo

Reputation: 13

Merge serval models (LSTMs) in TensorFlow

I know how to merge different models into one in Keras.

first_model = Sequential()
first_model.add(LSTM(output_dim, input_shape=(m, input_dim)))

second_model = Sequential()
second_model.add(LSTM(output_dim, input_shape=(n-m, input_dim)))

model = Sequential()
model.add(Merge([first_model, second_model], mode='concat'))
model.fit([X1,X2])

I am not sure how to do this in TensorFlow though.

I have two LSTM models and want to merge those (in the same way as in above Keras example).

outputs_1, state_1 = tf.nn.dynamic_rnn(stacked_lstm_1, model_input_1)
outputs_2, state_2 = tf.nn.dynamic_rnn(stacked_lstm_2, model_input_2)

Any help would be much appreciated!

Upvotes: 0

Views: 739

Answers (1)

Nathan
Nathan

Reputation: 10306

As was said in the comment, I believe the simplest way to do this is just to concatenate the outputs. The only complication that I've found is that, at least how I made my LSTM layers, they ended up with the exact same names for their weight tensors. This led to an error because TensorFlow thought the weights were already made when I tried to make the second layer. If you have this problem, you can solve it using a variable scope, which will apply to the names of the tensors in that LSTM layer:

with tf.variable_scope("LSTM_1"):
    lstm_cells_1 = tf.contrib.rnn.MultiRNNCell(tf.contrib.rnn.LSTMCell(256))
    output_1, state_1 = tf.nn.dynamic_rnn(lstm_cells_1, inputs_1)
    last_output_1 = output_1[:, -1, :]
# I usually work with the last one; you can keep them all, if you want

with tf.variable_scope("LSTM_2"):
    lstm_cells_2 = tf.contrib.rnn.MultiRNNCell(tf.contrib.rnn.LSTMCell(256))
    output_2, state_2 = tf.nn.dynamic_rnn(lstm_cells_2, inputs_2)
    last_output_2 = output_2[:, -1, :]

merged = tf.concat((last_output_1, last_output_2), axis=1)

Upvotes: 3

Related Questions