在将KERAS模型与Bert类方法一起使用时,如何显示模型摘要?
我从本教程中的本教程)。
如其中所述,我用:
class Classifier(tf.keras.Model):
def __init__(self, num_classes):
super(Classifier, self).__init__(name="prediction")
self.encoder = hub.KerasLayer(tfhub_handle_encoder, trainable=True)
self.dropout = tf.keras.layers.Dropout(0.1)
self.dense = tf.keras.layers.Dense(num_classes)
def call(self, preprocessed_text):
encoder_outputs = self.encoder(preprocessed_text)
pooled_output = encoder_outputs["pooled_output"]
x = self.dropout(pooled_output)
x = self.dense(x)
return x
我想在使用mode.summary()
构建模型详细信息之后显示模型。不幸的是,这只会返回
模型:“预测”
图层(类型)输出形状param#
================================================ =================== keras_layer_1(keraslayer)多个109482241
dropout_10(辍学)多0
致密_10(密集)多个4614
================================================ =================== 总参数:109,486,855可训练的参数:109,486,854不可训练 参数:1
,但我想详细获取完整的图层。因此,我尝试添加一个函数以在模型中创建它:
def summary_model(self, sequence_length):
in_id = tf.keras.layers.Input(shape=(sequence_length,), name="input_ids")
in_mask = tf.keras.layers.Input(shape=(sequence_length,), name="input_masks")
in_segment = tf.keras.layers.Input(shape=(sequence_length,), name="segment_ids")
bert_inputs = [in_id, in_mask, in_segment]
bert_output = self.encoder(bert_inputs)
pooled_output = bert_output["pooled_output"]
x = self.dropout(pooled_output)
output = self.dense(x)
model = tf.keras.models.Model(inputs=bert_inputs, outputs=output)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
return model
但是,这导致了与
valueerror:找不到匹配的具体功能来调用加载 从SavedModel。哥特:位置参数(总计3):
- [< tf.tensor'输入:0'shape =(none,124)dtype = float32>,< tf.tensor'inputs_1:0'shape =(none,124)dtype = float = float32> tf.tensor'inputs_2:0' shape =(无,124)dtype = float32>]
- false
- 无关键字参数:{}
期望这些论点匹配以下4个选项之一:
选项1:位置参数(总计3):
- {'input_mask':tensorSpec(shape =(none,none),dtype = tf.int32,name ='input_mask'),'input_type_ids':tensorspec(tensorspec(shape =(none,none,none),无),无),无),无) dtype = tf.int32,name ='input_type_ids'),'input_word_ids': tensorspec(shape =(无,无),dtype = tf.int32,name ='input_word_ids')}
- false
- 无关键字参数:{}
选项2:位置参数(总计3):
- {'input_mask':tensorSpec(shape =(none,none),dtype = ttype = tf.int32,name ='inputs/input_mask'),'input_type_ids':tensorspec(tensorspec(shape =(无,,无,, 无),dtype = tf.int32,name ='inputs/input_type_ids'),), 'input_word_ids':tensorspec(shape =(无,无),dtype = tf.int32, name ='输入/input_word_ids')}
- false
- 无关键字参数:{}
选项3:位置参数(总计3):
- {'input_mask':tensorSpec(shape =(none,none),dtype = ttype = tf.int32,name ='inputs/input_mask'),'input_type_ids':tensorspec(tensorspec(shape =(无,,无,, 无),dtype = tf.int32,name ='inputs/input_type_ids'),), 'input_word_ids':tensorspec(shape =(无,无),dtype = tf.int32, name ='输入/input_word_ids')}
- true
- 无关键字参数:{}
选项4:位置参数(总计3):
- {'input_mask':tensorSpec(shape =(none,none),dtype = tf.int32,name ='input_mask'),'input_type_ids':tensorspec(tensorspec(shape =(none,none,none),无),无),无),无) dtype = tf.int32,name ='input_type_ids'),'input_word_ids': tensorspec(shape =(无,无),dtype = tf.int32,name ='input_word_ids')}
- true
- 无关键字参数:{}
我需要在输入形状或其他地方更改什么才能获得详细信息概括?
I went through this tutorial from this tutorial from tensorflow.
As described in there I defined my model with:
class Classifier(tf.keras.Model):
def __init__(self, num_classes):
super(Classifier, self).__init__(name="prediction")
self.encoder = hub.KerasLayer(tfhub_handle_encoder, trainable=True)
self.dropout = tf.keras.layers.Dropout(0.1)
self.dense = tf.keras.layers.Dense(num_classes)
def call(self, preprocessed_text):
encoder_outputs = self.encoder(preprocessed_text)
pooled_output = encoder_outputs["pooled_output"]
x = self.dropout(pooled_output)
x = self.dense(x)
return x
I would like to display the model details after building it with mode.summary()
. Unfortunately this only returns
Model: "prediction"
Layer (type) Output Shape Param #
=================================================================
keras_layer_1 (KerasLayer) multiple 109482241dropout_10 (Dropout) multiple 0
dense_10 (Dense) multiple 4614
=================================================================
Total params: 109,486,855 Trainable params: 109,486,854 Non-trainable
params: 1
But I would like to get the full layers in detail. Therefore I tried to add a function to create it in the model:
def summary_model(self, sequence_length):
in_id = tf.keras.layers.Input(shape=(sequence_length,), name="input_ids")
in_mask = tf.keras.layers.Input(shape=(sequence_length,), name="input_masks")
in_segment = tf.keras.layers.Input(shape=(sequence_length,), name="segment_ids")
bert_inputs = [in_id, in_mask, in_segment]
bert_output = self.encoder(bert_inputs)
pooled_output = bert_output["pooled_output"]
x = self.dropout(pooled_output)
output = self.dense(x)
model = tf.keras.models.Model(inputs=bert_inputs, outputs=output)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
return model
But this leads to an error with
ValueError: Could not find matching concrete function to call loaded
from the SavedModel. Got: Positional arguments (3 total):
- [<tf.Tensor 'inputs:0' shape=(None, 124) dtype=float32>, <tf.Tensor 'inputs_1:0' shape=(None, 124) dtype=float32>, <tf.Tensor 'inputs_2:0'
shape=(None, 124) dtype=float32>]- False
- None Keyword arguments: {}
Expected these arguments to match one of the following 4 option(s):
Option 1: Positional arguments (3 total):
- {'input_mask': TensorSpec(shape=(None, None), dtype=tf.int32, name='input_mask'), 'input_type_ids': TensorSpec(shape=(None, None),
dtype=tf.int32, name='input_type_ids'), 'input_word_ids':
TensorSpec(shape=(None, None), dtype=tf.int32, name='input_word_ids')}- False
- None Keyword arguments: {}
Option 2: Positional arguments (3 total):
- {'input_mask': TensorSpec(shape=(None, None), dtype=tf.int32, name='inputs/input_mask'), 'input_type_ids': TensorSpec(shape=(None,
None), dtype=tf.int32, name='inputs/input_type_ids'),
'input_word_ids': TensorSpec(shape=(None, None), dtype=tf.int32,
name='inputs/input_word_ids')}- False
- None Keyword arguments: {}
Option 3: Positional arguments (3 total):
- {'input_mask': TensorSpec(shape=(None, None), dtype=tf.int32, name='inputs/input_mask'), 'input_type_ids': TensorSpec(shape=(None,
None), dtype=tf.int32, name='inputs/input_type_ids'),
'input_word_ids': TensorSpec(shape=(None, None), dtype=tf.int32,
name='inputs/input_word_ids')}- True
- None Keyword arguments: {}
Option 4: Positional arguments (3 total):
- {'input_mask': TensorSpec(shape=(None, None), dtype=tf.int32, name='input_mask'), 'input_type_ids': TensorSpec(shape=(None, None),
dtype=tf.int32, name='input_type_ids'), 'input_word_ids':
TensorSpec(shape=(None, None), dtype=tf.int32, name='input_word_ids')}- True
- None Keyword arguments: {}
What do I need to change in the input shape or somewhere else to be able to get the detailed summary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论