无法获得正确形状的 TensorFlow 自定义层

can't get the right shape of TensorFlow custom layer

我正在尝试使用自定义层在 TensorFlow 中训练模型。 我在最后一层遇到问题,我正在尝试构建一个层来获取一批图像 [None,100,100,1] 和 returns 10 个不同方形区域的总和,所以输出应该是 [None,10].

的形状

我尝试了一些不同的方法但没有成功。 我试过:

        output = tf.concat([tf.reshape(tf.math.reduce_sum(inputs[34:42, 28:40,0]), [1,]),
                            tf.reshape(tf.math.reduce_sum(inputs[34:42, 44:56,0]), [1,]),
                            tf.reshape(tf.math.reduce_sum(inputs[34:42, 60:72,0]), [1,]),
                            tf.reshape(tf.math.reduce_sum(inputs[46:54, 20:32,0]), [1,]),
                            tf.reshape(tf.math.reduce_sum(inputs[46:54, 36:48,0]), [1,]),
                            tf.reshape(tf.math.reduce_sum(inputs[46:54, 52:64,0]), [1,]),
                            tf.reshape(tf.math.reduce_sum(inputs[46:54, 68:80,0]), [1,]),
                            tf.reshape(tf.math.reduce_sum(inputs[58:66, 28:40,0]), [1,]),
                            tf.reshape(tf.math.reduce_sum(inputs[58:66, 44:56,0]), [1,]),
                            tf.reshape(tf.math.reduce_sum(inputs[58:66, 60:72,0]), [1,])], axis= 0)

和类似的求和函数,但无法将形状的第一个维度设为 'None'。

我已经尝试 'cheating' 通过将输入重塑为正确的形状然后乘以 0 并添加大小为 [10] 的张量。这得到了正确的形状,但模型没有训练。

有正确的方法吗?我在这个问题上被困了好几个星期都没有运气。

如果我放置一个不符合我要求的不同层,模型训练良好,但它具有正确的输出形状:

class output_layer(tf.keras.Model):
    def __init__(self, shape_input):
        self.shape_input = shape_input
        super(output_layer, self).__init__()

    def call(self, inputs):
        inputs = tf.math.multiply(inputs,tf.math.conj(inputs))
        temp = tf.math.reduce_sum(inputs, axis=2)
        temp = tf.reshape(temp, [-1,10,10])
        temp = tf.math.reduce_sum(temp, axis=2)        
        output = tf.cast(temp, tf.float32)
        output = tf.keras.activations.softmax(output, axis=-1)
        return output

如果有人能帮我解决这个问题,我将不胜感激!

谢谢!

我修改了您的代码并得出以下结论:

output = tf.concat(
                  [tf.math.reduce_sum(inputs[:, 34:42, 28:40,:], axis=[1,2]),
                   tf.math.reduce_sum(inputs[:, 34:42, 44:56,:], axis=[1,2]),
                   tf.math.reduce_sum(inputs[:, 34:42, 60:72,:], axis=[1,2]),
                   tf.math.reduce_sum(inputs[:, 46:54, 20:32,:], axis=[1,2]),
                   tf.math.reduce_sum(inputs[:, 46:54, 36:48,:], axis=[1,2]),
                   tf.math.reduce_sum(inputs[:, 46:54, 52:64,:], axis=[1,2]),
                   tf.math.reduce_sum(inputs[:, 46:54, 68:80,:], axis=[1,2]),
                   tf.math.reduce_sum(inputs[:, 58:66, 28:40,:], axis=[1,2]),
                   tf.math.reduce_sum(inputs[:, 58:66, 44:56,:], axis=[1,2]),
                   tf.math.reduce_sum(inputs[:, 58:66, 60:72,:], axis=[1,2])], axis=-1)

注意我将 inputs[34:42, 28:40, 0] 更改为 inputs[:, 34:42, 28:40,:]。您可以使用 : 来表示要保持不变的维度。我还指定了应该减少哪个轴,因此,只有没有指定要减少的尺寸才会保留——在这种情况下,它是第一个和最后一个尺寸。在您的例子中,tf.math.reduce_sum 将生成形状 [None, 1]。 与此同时,我将 tf.concat 的轴更改为 -1,这是最后一层,因此它产生形状 [None, 10].

为了完整起见,您可以创建自己的图层。为此,您必须继承 tf.keras.layers.Layer.

然后,您可以将其用作任何其他图层。

class ReduceZones(tf.keras.layers.Layer):
    def __init__(self):
        super(ReduceZones, self).__init__()
      
    def build(self, input_shapes):
        return
      
    def call(self, inputs):
        output = tf.concat(
                [tf.math.reduce_sum(inputs[:, 34:42, 28:40,:], axis=[1,2]),
                 tf.math.reduce_sum(inputs[:, 34:42, 44:56,:], axis=[1,2]),
                 tf.math.reduce_sum(inputs[:, 34:42, 60:72,:], axis=[1,2]),
                 tf.math.reduce_sum(inputs[:, 46:54, 20:32,:], axis=[1,2]),
                 tf.math.reduce_sum(inputs[:, 46:54, 36:48,:], axis=[1,2]),
                 tf.math.reduce_sum(inputs[:, 46:54, 52:64,:], axis=[1,2]),
                 tf.math.reduce_sum(inputs[:, 46:54, 68:80,:], axis=[1,2]),
                 tf.math.reduce_sum(inputs[:, 58:66, 28:40,:], axis=[1,2]),
                 tf.math.reduce_sum(inputs[:, 58:66, 44:56,:], axis=[1,2]),
                 tf.math.reduce_sum(inputs[:, 58:66, 60:72,:], axis=[1,2])], axis=-1)
        return output