简体   繁体   English

如何在`tensorflow.keras`中替换`keras.layers.merge._Merge`

[英]How to substitute `keras.layers.merge._Merge` in `tensorflow.keras`

I want to create a custom Merge layer using the tf.keras API.我想使用tf.keras API 创建一个自定义合并层。 However, the new API hides the keras.layers.merge._Merge class that I want to inherit from.但是,新的 API 隐藏了我想继承的keras.layers.merge._Merge class。

The purpose of this is to create a Layer that can perform a weighted sum/merge of the outputs of two different layers.这样做的目的是创建一个可以对两个不同层的输出进行加权求和/合并的层。 Before, and in keras python API (not the one included in tensorflow.keras ) I could inherit from keras.layers.merge._Merge class, which is now not accessible from tensorflow.keras . Before, and in keras python API (not the one included in tensorflow.keras ) I could inherit from keras.layers.merge._Merge class, which is now not accessible from tensorflow.keras .

Where before I could do this在我能做到这一点之前在哪里

class RandomWeightedAverage(keras.layers.merge._Merge):
    def __init__(self, batch_size):
        super().__init__()
        self.batch_size = batch_size
    def _merge_function(self, inputs):
        alpha = K.random_uniform((self.batch_size, 1, 1, 1))
        return (alpha * inputs[0]) + ((1 - alpha) * inputs[1])

Now I cannot use the same logic if using tensorflow.keras现在,如果使用tensorflow.keras ,我不能使用相同的逻辑

class RandomWeightedAverage(tf.keras.layers.merge._Merge):
    def __init__(self, batch_size):
        super().__init__()
        self.batch_size = batch_size
    def _merge_function(self, inputs):
        alpha = K.random_uniform((self.batch_size, 1, 1, 1))
        return (alpha * inputs[0]) + ((1 - alpha) * inputs[1])

Produces生产

AttributeError: module 'tensorflow.python.keras.api._v1.keras.layers' has no attribute 'merge'

I have also tried inheriting from Layer class instead我也尝试过从Layer class 继承

class RandomWeightedAverage(tensorflow.keras.layers.Layer):
    def __init__(self, batch_size):
        super().__init__()
        self.batch_size = batch_size
    def call(self, inputs):
        alpha = K.random_uniform((self.batch_size, 1, 1, 1))
        return (alpha * inputs[0]) + ((1 - alpha) * inputs[1])

which gives me a layer with output shape equals to multiple , whereas I want the output shape to be well defined.这给了我一个 output 形状等于multiple的层,而我希望 output 形状定义明确。 I further attempted我进一步尝试

class RandomWeightedAverage(tensorflow.keras.layers.Layer):
    def __init__(self, batch_size):
        super().__init__()
        self.batch_size = batch_size
    def call(self, inputs):
        alpha = K.random_uniform((self.batch_size, 1, 1, 1))
        return (alpha * inputs[0]) + ((1 - alpha) * inputs[1])

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

But this did not solve the multiple ambiguity as output shape.但这并没有解决 output 形状的multiple歧义。

I have slightly modified your code to use tf.random_uniform instead of K.random_uniform and it's working fine on 1.13.1 and 1.14.0 (full snippet and resulting model.summary() below).我稍微修改了您的代码以使用tf.random_uniform而不是K.random_uniform并且它在 1.13.1 和 1.14.0 上运行良好(完整的代码片段和下面的model.summary() )。

import tensorflow as tf
print(tf.__version__)


class RandomWeightedAverage(tf.keras.layers.Layer):
    def __init__(self, batch_size):
        super().__init__()
        self.batch_size = batch_size

    def call(self, inputs, **kwargs):
        alpha = tf.random_uniform((self.batch_size, 1, 1, 1))
        return (alpha * inputs[0]) + ((1 - alpha) * inputs[1])

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


x1 = tf.keras.layers.Input((32, 32, 1))
x2 = tf.keras.layers.Input((32, 32, 1))

y = RandomWeightedAverage(4)(inputs=[x1, x2])

model = tf.keras.Model(inputs=[x1, x2], outputs=[y])

print(model.summary())

模型摘要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM