Search code examples
machine-learningtensorflowkeraskeras-layerkeras-2

Use Merge layer (lambda/function) on Keras 2.0?


I trying to port this model into Keras v2 but I have a problem with following function:

  def __call__(self, sent1, sent2):
        def _outer(AB):
            att_ji = K.batch_dot(AB[1], K.permute_dimensions(AB[0], (0, 2, 1)))
            return K.permute_dimensions(att_ji, (0, 2, 1))

        return merge([self.model(sent1), self.model(sent2)], mode=_outer,
                     output_shape=(self.max_length, self.max_length))

According to documentation, mode is:

String or lambda/function. If string, must be one of: 'sum', 'mul', 'concat', 'ave', 'cos', 'dot', 'max'. If lambda/function, it should take as input a list of tensors and return a single tensor.

What is equivalent function (when mode is function/lambda) in new Keras version to avoid following warning:

UserWarning: The `merge` function is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.
  return merge([attention, sentence], mode=_normalize_attention, output_shape=(self.max_length, self.nr_hidden))

Solution

  • Kind of a weird way to implement a model.... (at least in keras 2...)

    It seems you should just use a lambda layer with a custom function.

    def __call__(self, sent1, sent2):
        def _outer(AB) #custom function
            att_ji = K.batch_dot(AB[1], K.permute_dimensions(AB[0], (0, 2, 1)))    
            return K.permute_dimensions(att_ji, (0, 2, 1))
    
        return Lambda(_outer, 
                      output_shape=(self.max_length,self.max_length))([
                                               self.model(sent1), 
                                               self.model(sent2)])
    

    This should work if self.model(sent) returns a tensor made by keras layers.


    Now, for actual merge layers, in keras 2 you have the layers:

    • Concatenate(axis=...)(listOfTensors)
    • Add()(listOfTensors)
    • Multiply()(listOfTensors)
    • And others, including a Dot layer, which "might" do the same as that function.

    If using the dot layer:

    return Dot()([self.model(sent1),self.model(sent2)])
    

    This needs testing. Dot and batch dot in keras are confusing things.