我正在自定义一个层来使用我的模型。核心部分是“调用”功能,
class Custom_Layer(Layer):
// some code
def call(self, inputs, **kwargs):
kernel = mul(self.base, self.diag_start - self.diag_end)
outputs = matmul(a=inputs, b=kernel)
if self.use_bias:
outputs = tf.nn.bias_add(outputs, self.bias)
if self.activation is not None:
outputs = self.activation(outputs)
return outputs
// some code
并在一个简单的模型中得到了应用。
inputs = tf.keras.layers.Input(shape=(784,),dtype='float32')
layer1 = Custom_layer(2000, **Custom_layer_config, activation='tanh')(inputs)
layer2 = Custom_layer(200, **Custom_layer_config, activation='tanh')(layer1)
output_lay = Custom_layer(10, **Custom_layer_config, activation='softmax')(layer2)
model = tf.keras.models.Model(inputs=inputs, outputs=output_lay)
opt = tf.keras.optimizers.Adamax(learning_rate=0.02)
model.compile(optimizer=opt,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.summary()
它应该打印如下:
Model: "functional_13"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_8 (InputLayer) [(None, 784)] 0
_________________________________________________________________
CustomLayer_18 (Custom_Layer) (None, 2000) 1570784
_________________________________________________________________
CustomLayer_19 (Custom_Layer) (None, 200) 402200
_________________________________________________________________
CustomLayer_20 (Custom_Layer) (None, 10) 2210
=================================================================
Total params: 1,975,194
Trainable params: 5,194
Non-trainable params: 1,970,000
_________________________________________________________________
但打印以下内容:
Model: "model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_2 (InputLayer) [(None, 784)] 0
tf.linalg.matmul_3 (TFOpLam (None, 2000) 0
bda)
tf.math.tanh_2 (TFOpLambda) (None, 2000) 0
tf.linalg.matmul_4 (TFOpLam (None, 200) 0
bda)
tf.math.tanh_3 (TFOpLambda) (None, 200) 0
tf.linalg.matmul_5 (TFOpLam (None, 10) 0
bda)
tf.compat.v1.nn.softmax_1 ( (None, 10) 0
TFOpLambda)
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
第一个摘要是我从作者的存储库中得到的,第二个摘要是我运行相同的代码时没有更改任何内容。
代码并不复杂,但奇怪的是为什么没有参数。我的问题是这里出了什么问题。
1条答案
按热度按时间dauxcl2d1#
尝试将其作为此示例中的继承类。
示例:自定义LSTM类
输出量: