Skip to content

Instantly share code, notes, and snippets.

@twolodzko
Created September 20, 2018 08:57
Show Gist options
  • Select an option

  • Save twolodzko/e3eedb7aa973b9ff14c67ea7e89b3999 to your computer and use it in GitHub Desktop.

Select an option

Save twolodzko/e3eedb7aa973b9ff14c67ea7e89b3999 to your computer and use it in GitHub Desktop.
WeightedAverage merging layer for Keras
from keras import backend as K
from keras.layers import Average
from keras.activations import softmax
class WeightedAverage(Average):
def build(self, input_shape):
self.kernel = self.add_weight(name='kernel',
shape=(1, len(input_shape)),
initializer='ones',
trainable=True)
super(WeightedAverage, self).build(input_shape)
def _merge_function(self, inputs):
w = softmax(self.kernel)
output = K.zeros_like(inputs[0])
for i in range(len(inputs)):
output += w[0,i] * inputs[i]
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment