且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Keras 中构建这个自定义层?

更新时间:2023-12-01 22:19:34

您的代码有几个问题.

首先我应该解决你从解释器那里得到的错误:

First of all I should address the error you get from interpreter:

TypeError: build() takes 1 positional argument but 2 were given

build 方法应该采用 input_shape 参数.因此,您应该将构建方法声明为 build(self, input_shape)

The build method should take input_shape argument. Therefore you should declare build method as build(self, input_shape)

第二个问题是 build 方法中变量的未定义形状.您应该显式声明变量的形状.在您的情况下, np.random.rand 数组应该是 input_shape 形状.

The second issue is undefined shape of the variables in the build method. You should explicitly declare shape of the variables. In your case the np.random.rand array should be of input_shape shape.

另一个问题是您试图在 call 方法中返回 2 个结果 ([op_r, op_i]).我不是 Keras 的专家,但据我所知你做不到.每个 Keras 层都应该有一个且只有一个输出.详情请看这里:https://github.com/keras-team/keras/问题/3061

Another issue is that you are trying to return 2 results ([op_r, op_i]) in the call method. I'm not specialist in Keras but as far as I know you can't do it. Every Keras layer should have one and only one output. See here for the details: https://github.com/keras-team/keras/issues/3061

但是,如果您使用 tensorflow 后端,您可以使用复数 (tf.complex) 来返回实数 (op_r) 和图像数 (op_i)>) 复数的一部分.

However if you use tensorflow backend you may use complex numbers (tf.complex) to return both real (op_r) and imagenary (op_i) parts of the complex number.

这里是 modrelu 层的工作实现和简单的使用示例.它是为 TensorFlow 1.12.0 编写的,TensorFlow 1.12.0 随它自己的 Keras API 实现一起分发,但我认为您可以轻松地将它用于原始 Keras:

Here is the working implementation of modrelu layer with simple usage example. It is writtern for TensorFlow 1.12.0 which is distributed with it's own implementation of Keras API but I think you can easily adopt it for original Keras:

import tensorflow as tf
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.engine import Layer
import numpy as np


class modrelu(Layer):

    def __init__(self, **kwargs):
        super(modrelu, self).__init__(**kwargs)

    # provide input_shape argument in the build method
    def build(self, input_shape):
        # You should pass shape for your variable
        self.b= K.variable(value=np.random.rand(*input_shape)-0.5,
                           dtype='float32')
        super(modrelu, self).build(input_shape)  # Be sure to call this at the end

    def call(self, inputs, **kwargs):
        assert inputs.dtype == tf.complex64

        ip_r = tf.math.real(inputs)
        ip_i = tf.math.imag(inputs)

        comp = tf.complex(ip_r, ip_i )
        ABS = tf.math.abs(comp)
        ANG = tf.math.angle(comp)

        ABS = K.relu(self.b + ABS)

        op_r = ABS * K.sin(ANG) #K.dot ??
        op_i = ABS * K.cos(ANG)

        # return single tensor in the call method
        return tf.complex(op_r, op_i)


real = tf.constant([2.25, 3.25])
imag = tf.constant([4.75, 5.75])

x = tf.complex(real, imag)

y = modrelu()(x)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    print(sess.run(y))

P.S.:我没有检查数学,所以你应该自己检查.

P.S.: I didn't check the math so you should check it by yourself.