Skip to content

Instantly share code, notes, and snippets.

@BAXTER001
Last active March 25, 2019 19:09
Show Gist options
  • Save BAXTER001/5b2bb9e65b587f893298f09102436ca1 to your computer and use it in GitHub Desktop.
Save BAXTER001/5b2bb9e65b587f893298f09102436ca1 to your computer and use it in GitHub Desktop.
Keras multi gpu duplicate model names
from keras.models import Model
from keras.layers import Input, Add
from keras.utils import multi_gpu_model
m_in = Input( shape=(1,) )
m_out1 = Add()([m_in,m_in])
m_out2 = m_in
m = Model( m_in, [m_out1,m_out2] )
x = Input( shape=(1,) )
compoundModel = Model( x, m( x ) )
compoundModel = multi_gpu_model( compoundModel ,2)
#RuntimeError: ('The name "model_1" is used 2 times in the model. All layer names should be unique. Layer names: ',
#['input_2', 'lambda_1', 'lambda_2', 'model_2', 'model_1', 'model_1'])
# Merge outputs on CPU.
with tf.device('/cpu:0'):
merged = []
for n,(name, outputs) in enumerate(zip(model.output_names, all_outputs)):
merged.append(concatenate(outputs,
axis=0, name=name+'_gpu_'+str(n)))
@fengwang
Copy link

Would you please make a pr? This is useful to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment