Python can';t获取TensorFlow自定义层的正确形状
我正在尝试使用自定义图层在TensorFlow中训练模型。 最后一层有问题,我正在尝试构建一个层,该层获取一批图像[None,100100,1],并返回10个不同方形区域的总和,因此输出应为[None,10]的形状 我尝试过一些不同的方法,但没有成功。 我试过:Python can';t获取TensorFlow自定义层的正确形状,python,tensorflow,neural-network,tensorflow2.0,Python,Tensorflow,Neural Network,Tensorflow2.0,我正在尝试使用自定义图层在TensorFlow中训练模型。 最后一层有问题,我正在尝试构建一个层,该层获取一批图像[None,100100,1],并返回10个不同方形区域的总和,因此输出应为[None,10]的形状 我尝试过一些不同的方法,但没有成功。 我试过: output = tf.concat([tf.reshape(tf.math.reduce_sum(inputs[34:42, 28:40,0]), [1,]),
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)
注意,我将输入[34:42,28:40,0]
更改为输入[:,34:42,28:40,:]
。对于要保持相同的尺寸,可以使用:
。我还指定了应减少的轴,因此,在这种情况下,只有未减少规格的尺寸才会保留,这是第一个也是最后一个尺寸。在您的例子中,tf.math.reduce\u sum
将生成shape[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