TensorFlow 层子类输入形状

TensorFlow layer subclass input shape

我正在尝试从 TensorFlow 的 layers.Layer 实例创建自定义层。

我正在尝试制作一个 IIR 滤波器,因此它使用输入层的值并计算输出序列,如下所示:

y[i] = a0 * x[i] + a1 * x[i - 1] + b1 * y[i - 1]

其中 x 是输入,y 是输出。我这样定义 class:

class IIR(keras.layers.Layer):
    def __init__(self, input_dim):
        super(IIR, self).__init__()
        self.input_dim = 60
        self.b0 = tf.Variable(tf.constant([uniform(-1, 1)]))
        self.b1 = tf.Variable(tf.constant([uniform(-1, 1)]))
        self.b2 = tf.Variable(tf.constant([uniform(-1, 1)]))
        self.a1 = tf.Variable(tf.constant([uniform(-1, 1)]))
        self.a2 = tf.Variable(tf.constant([uniform(-1, 1)]))
        
    def call(self, inputs):
      order = 3
      init_dim = [0,1,2]
      output_sequence = tf.constant(np.zeros((self.input_dim)),dtype=tf.float32)
      outt = np.zeros(self.input_dim)
      outt[0] = inputs[0]
      outt[1] = inputs[1]
      outt[2] = inputs[2]
      for i in range(2,self.input_dim):
        outt[i] = self.b0*inputs[i] + self.b1*inputs[i-1] + self.b2*inputs[i-2] - self.a1*outt[i-1] - self.a2*outt[i-2]
      output_sequence = tf.constant(outt)
      return output_sequence

但我一直收到错误

ValueError: Exception encountered when calling layer "iir_13" (type IIR).

in user code:

    File "<ipython-input-37-0717fc982e73>", line 17, in call  *
        outt[0] = inputs[:][0]

    ValueError: setting an array element with a sequence.


Call arguments received:
  • inputs=tf.Tensor(shape=(None, 60), dtype=float32)

等等。输入的形状是 (None, 60)(我设置 60 只是为了测试目的)并且我假设 None 会在训练时被批量大小取代?我怎样才能访问输入的值?输入的实际形状是什么?这是正确的方法吗?

编辑:我正在尝试在模型中实现它,如下所示:

model = keras.Sequential()
model.add(keras.layers.Input(shape=60))
model.add(IIR(input_dim=60))
model.add(keras.layers.Dense(8, activation='relu'))
model.add(keras.layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy')

不确定你到底想做什么,但我建议只使用 Tensorflow 操作。这是一个例子:

import tensorflow as tf

class IIR(tf.keras.layers.Layer):

    def __init__(self, input_dim):
        super(IIR, self).__init__()
        self.input_dim = input_dim
        self.b0 = tf.Variable(tf.random.uniform((1,), minval=-1, maxval=1))
        self.b1 = tf.Variable(tf.random.uniform((1,), minval=-1, maxval=1))
        self.b2 = tf.Variable(tf.random.uniform((1,), minval=-1, maxval=1))
        self.a1 = tf.Variable(tf.random.uniform((1,), minval=-1, maxval=1))
        self.a2 = tf.Variable(tf.random.uniform((1,), minval=-1, maxval=1))
        

    def call(self, inputs):
      batch_size = tf.shape(inputs)[0]
      output_sequence = tf.TensorArray(dtype=tf.float32, size=0, dynamic_size=True, clear_after_read=False) 
      output_sequence = output_sequence.write(0, inputs[:, 0])
      output_sequence = output_sequence.write(1, inputs[:, 1])
      output_sequence = output_sequence.write(2, inputs[:, 2])

      for i in range(2, self.input_dim):
        output_sequence = output_sequence.write(i, self.b0*inputs[:, i] + self.b1*inputs[:, i-1] 
                                                 + self.b2*inputs[:, i-2] - self.a1*output_sequence.read(i-1)
                                                 - self.a2*output_sequence.read(i-2))
      result = output_sequence.stack()
      return tf.reshape(result, tf.shape(inputs))

iir = IIR(input_dim=60)
tf.print(iir(tf.random.normal((2, 60))).shape)

iir = IIR(input_dim=60)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Input(shape=60))
model.add(IIR(input_dim=60))
model.add(tf.keras.layers.Dense(8, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
print(model.summary())
TensorShape([2, 60])
Model: "sequential_21"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 iir_80 (IIR)                (None, 60)                5         
                                                                 
 dense_20 (Dense)            (None, 8)                 488       
                                                                 
 dense_21 (Dense)            (None, 1)                 9         
                                                                 
=================================================================
Total params: 502
Trainable params: 502
Non-trainable params: 0
_________________________________________________________________
None