我自定义了一个层,将batch_size和第一个维度合并,其他维度保持不变,但是compute_output_shape好像没有效果,导致后续层无法获取准确的形状信息,导致报错。如何使 compute_output_shape 工作?
import keras
from keras import backend as K
class BatchMergeReshape(keras.layers.Layer):
def __init__(self, **kwargs):
super(BatchMergeReshape, self).__init__(**kwargs)
def build(self, input_shape):
super(BatchMergeReshape, self).build(input_shape)
def call(self, x):
input_shape = K.shape(x)
batch_size, seq_len = input_shape[0], input_shape[1]
r = K.reshape(x, (batch_size*seq_len,)+input_shape[2:])
print("call_shape:",r.shape)
return r
def compute_output_shape(self, input_shape):
if input_shape[0] is None:
r = (None,)+input_shape[2:]
print("compute_output_shape:",r)
return r
else:
r = (input_shape[0]*input_shape[1],)+input_shape[2:]
return r
a = keras.layers.Input(shape=(3,4,5))
b = BatchMergeReshape()(a)
print(b.shape)
# call_shape: (?, ?)
# compute_output_shape: (None, 4, 5)
# (?, ?)
我需要得到 (None,4,5) 但得到 (None,None),为什么 compute_output_shape 不起作用。我的 keras 版本是 2.2.4