Keras计算两个张量的凸组合

时间:2018-04-04 05:23:37

标签: python tensorflow keras

我有两个张量h1h2都具有(?, H, T)形状。哪个是通过计算凸组合lambda * h1 + (1 - lambda) * h2来合并它们的最佳方法,其中lambda是一个可学习的具有形状(H,)的一维向量?

我在keras后端使用tensorflow

1 个答案:

答案 0 :(得分:2)

定义custom keras layer

from keras.engine.topology import Layer
from keras.models import Model
from keras.layers import Input
import numpy as np

H = 2
T = 3


class ConvexCombination(Layer):
    def __init__(self, **kwargs):
        super(ConvexCombination, self).__init__(**kwargs)

    def build(self, input_shape):
        batch_size, H, T = input_shape[0]
        self.lambd = self.add_weight(name='lambda',
                                     shape=(H, 1),  # Adding one dimension for broadcasting
                                     initializer='zeros',  # Try also 'ones' and 'uniform'
                                     trainable=True)
        super(ConvexCombination, self).build(input_shape)

    def call(self, x):
        # x is a list of two tensors with shape=(batch_size, H, T)
        h1, h2 = x
        return self.lambd * h1 + (1 - self.lambd) * h2

    def compute_output_shape(self, input_shape):
        return input_shape[0]


h1 = Input(shape=(H, T))
h2 = Input(shape=(H, T))
cc = ConvexCombination()([h1, h2])
model = Model(inputs=[h1, h2],
              outputs=cc)

a = np.zeros(H * T).reshape(1, H, T)
b = np.arange(H * T).reshape(1, H, T)
pred = model.predict([a, b])
print(pred)