繁体   English   中英

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

[英]can't get the right shape of TensorFlow custom layer

我正在尝试使用自定义层在 TensorFlow 中训练模型。 我的最后一层有问题,我正在尝试构建一个获取一批图像 [None,100,100,1] 并返回 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)

和类似的求和函数,但无法将形状的第一维设为“无”。

我尝试通过将输入重塑为正确的形状然后乘以 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的轴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

暂无
暂无

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

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