title: How to make a weighted sum layer with trainable weight and a definable number of inputs?
body: <p>I am trying to make a weighted sum layer that performs a weighted sum of multiple parallel layers similar to the one posted <a href="https://datascience.stackexchange.com/questions/80750/how-can-i-perform-weighted-addition-of-three-layers-in-keras">here</a> except that I want to be able to define the number of inputs layers (i.e. n number of weights where n is defined as instantiation), and I want to use normalized weights. However, I am getting this error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;multi_fidelity_training_full_link.py&quot;, line 341, in &lt;module&gt;
      main()
    File &quot;multi_fidelity_training_full_link.py&quot;, line 283, in main
      linked_model.fit(x,y,batch_size = batch_size, epochs = mult_of*(1+i), verbose = 2, initial_epoch = mult_of*i)
    File &quot;/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py&quot;, line 1183, in fit
      tmp_logs = self.train_function(iterator)
    File &quot;/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py&quot;, line 889, in __call__
      result = self._call(*args, **kwds)
    File &quot;/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py&quot;, line 950, in _call
      return self._stateless_fn(*args, **kwds)
    File &quot;/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/eager/function.py&quot;, line 3024, in __call__
      filtered_flat_args, captured_inputs=graph_function.captured_inputs)  # pylint: disable=protected-access
    File &quot;/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/eager/function.py&quot;, line 1961, in _call_flat
      ctx, args, cancellation_manager=cancellation_manager))
    File &quot;/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/eager/function.py&quot;, line 596, in call
      ctx=ctx)
    File &quot;/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/eager/execute.py&quot;, line 75, in quick_execute
      raise e
    File &quot;/usr/WS2/mvander/py3venv/py3venv/lib/python3.7/site-packages/tensorflow/python/eager/execute.py&quot;, line 60, in quick_execute
      inputs, attrs, num_outputs)
  TypeError: An op outside of the function building code is being passed
  a &quot;Graph&quot; tensor. It is possible to have Graph tensors
  leak out of the function building context by including a
  tf.init_scope in your function building code.
  For example, the following function will fail:
    @tf.function
    def has_init_scope():
      my_constant = tf.constant(1.)
      with tf.init_scope():
        added = my_constant * 2
  The graph tensor has name: linked_model/high_fidelity_model/weighted_sum/truediv:0
  </code></pre>
  <p>My code is this:</p>
  <pre><code>class WeightedSum(krs.layers.Layer):
      def __init__( self, n_models = 2, **kwargs):
          super( WeightedSum, self ).__init__( **kwargs)
          self.n_models = n_models
          w_init = tf.random_uniform_initializer()
          self.sum_weights = tf.Variable(initial_value = w_init(shape=(1,self.n_models)),trainable=True)
          self.sum_weights = tf.divide(self.sum_weights,tf.math.reduce_sum(self.sum_weights))
  
      def call(self,inputs):
          self.sum_weights = tf.divide(self.sum_weights,tf.math.reduce_sum(self.sum_weights))
          output = tf.multiply(tf.cast(self.sum_weights[0,0],inputs[0].dtype),inputs[0])
          for i in range(1,len(inputs)):
              output += tf.multiply(inputs[i],tf.cast(self.sum_weights[0,i],inputs[0].dtype))
  
          return output
  </code></pre>
  <p>How do I make this work?</p>
  <p>Note: I have a different version of this code that looks quite different that builds and trains but produces a unicode error when trying to save. The error is almost certainly coming from my WeightedSum layer.</p>