@@ -0,0 +1,2161 @@
+# adapted from: https://github.com/kuza55/keras-extras/blob/master/utils/multi_gpu.py%0A#%0A## Example usage:%0A#%0A# # parameter server device%0A# ps_device = '/gpu:0'%0A#%0A# def basic_model():%0A#     #...%0A#%0A# with tf.device(ps_device):%0A#     model = make_parallel(basic_model(), gpu_count, ps_device)%0A#     model.compile(loss='categorical_crossentropy', optimizer='sgd')%0A%0Afrom keras.layers.merge concatenate%0Afrom keras.layers import Lambda%0Afrom keras.models import Model%0Aimport tensorflow as tf%0A%0Adef make_parallel(model, gpu_count, ps_device=None):%0A    if gpu_count %3C= 1:%0A        return model%0A%0A    if ps_device is None:%0A        ps_device = '/gpu:0'%0A%0A    def get_slice(data, idx, parts):%0A        shape = tf.shape(data)%0A        size = tf.concat(%5Bshape%5B:1%5D // parts, shape%5B1:%5D%5D, axis=0)%0A        stride = tf.concat(%5Bshape%5B:1%5D // parts, shape%5B1:%5D * 0%5D, axis=0)%0A        start = stride * idx%0A        return tf.slice(data, start, size)%0A%0A    outputs_all = %5B%5D%0A    for i in range(len(model.outputs)):%0A        outputs_all.append(%5B%5D)%0A%0A    # Place a copy of the model on each GPU, each getting a slice of the batch%0A    for i in range(gpu_count):%0A        with tf.device('/gpu:%25d' %25 i):%0A            with tf.name_scope('tower_%25d' %25 i) as scope:%0A%0A                inputs = %5B%5D%0A                # Slice each input into a piece for processing on this GPU%0A                for x in model.inputs:%0A                    input_shape = tuple(x.get_shape().as_list())%5B1:%5D%0A                    slice_n = Lambda(get_slice, output_shape=input_shape,%0A                        arguments=%7B'idx': i, 'parts': gpu_count%7D)(x)%0A                    inputs.append(slice_n)%0A%0A                outputs = model(inputs)%0A%0A                if not isinstance(outputs, list):%0A                    outputs = %5Boutputs%5D%0A%0A                # Save all the outputs for merging back together later%0A                for l in range(len(outputs)):%0A                    outputs_all%5Bl%5D.append(outputs%5Bl%5D)%0A%0A    # merge outputs on parameter server%0A    with tf.device(ps_device):%0A        merged = %5B%5D%0A        for outputs in outputs_all:%0A            merged.append(concatenate(outputs, axis=0))%0A%0A        return Model(inputs=model.inputs, outputs=merged)%0A

Add adapted make_parallel.py.