How to use multiple inputs in Tensorflow 2.x Keras Custom Layer?



I’m trying to use multiple inputs in custom layers in Tensorflow-Keras. Usage can be anything, right now it is defined as multiplying the mask with the image. I’ve search SO and the only answer I could find was for TF 1.x so it didn’t do any good.

class mul(layers.Layer):
    def __init__(self, **kwargs):
        # I've added pass because this is the simplest form I can come up with.
    def call(self, inputs):
        # magic happens here and multiplications occur


EDIT: Since TensorFlow v2.3/2.4, the contract is to use a list of inputs to the call method. For keras (not tf.keras) I think the answer below still applies.

Implementing multiple inputs is done in the call method of your class, there are two alternatives:

  • List input, here the inputs parameter is expected to be a list containing all the inputs, the advantage here is that it can be variable size. You can index the list, or unpack arguments using the = operator:

      def call(self, inputs):
          Z = inputs[0] * inputs[1]
          input1, input2 = inputs
          Z = input1 * input2
          return Z
  • Multiple input parameters in the call method, works but then the number of parameters is fixed when the layer is defined:

      def call(self, input1, input2):
          Z = input1 * input2
          return Z

Whatever method you choose to implement this depends if you need fixed size or variable sized number of arguments. Of course each method changes how the layer has to be called, either by passing a list of arguments, or by passing arguments one by one in the function call.

You can also use *args in the first method to allow for a call method with a variable number of arguments, but overall keras’ own layers that take multiple inputs (like Concatenate and Add) are implemented using lists.

Answered By – Dr. Snoopy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More