Я предлагаю вам использовать входные данные, определенные в методах вызова, иначе слой не имеет смысла
Я привожу фиктивный пример и отлично работает
class SimpleLayer(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super(SimpleLayer, self).__init__(**kwargs)
self.baseline = tf.Variable(initial_value=0.1, trainable=True)
def call(self, inputs):
ret = inputs + self.baseline
return (ret)
def compute_output_shape(self, input_shape):
return (input_shape[0], input_shape[1], input_shape[2])
создать модель с SimpleLayer
inp = Input(shape=(25,1))
x = SimpleLayer()(inp)
out = Dense(3)(x)
model = Model(inp, out)
model.summary()
сводка:
Layer (type) Output Shape Param #
=================================================================
input_10 (InputLayer) [(None, 25, 1)] 0
_________________________________________________________________
simple_layer_16 (SimpleLayer (None, 25, 1) 1
_________________________________________________________________
dense_22 (Dense) (None, 25, 3) 6
=================================================================
Total params: 7
Trainable params: 7
Non-trainable params: 0
РЕДАКТИРОВАТЬ
Я пытаюсь преодолеть проблему отсутствия измерения таким образом
class SimpleLayer(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super(SimpleLayer, self).__init__(**kwargs)
self.baseline = tf.Variable(initial_value=0.1, trainable=True, dtype=tf.float64)
def call(self, inputs):
ret = tf.zeros((1, 25, 1), dtype=tf.float64) + self.baseline
ret = tf.compat.v1.placeholder_with_default(ret, (None, 25, 1))
return (ret)
inp = Input((150,1))
x = Dense(256)(inp)
x = SimpleLayer()(x)
x = Dense(10)(x)
model = Model(inp, x)
model.summary()
резюме:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_34 (InputLayer) [(None, 150, 1)] 0
_________________________________________________________________
dense_68 (Dense) (None, 150, 256) 512
_________________________________________________________________
simple_layer_9 (SimpleLayer) (None, 25, 1) 1
_________________________________________________________________
dense_69 (Dense) (None, 25, 10) 20
=================================================================
Total params: 533
Trainable params: 533
Non-trainable params: 0