#tensorflow #keras #huggingface-transformers
Вопрос:
Пожалуйста, посоветуйте, как я могу получить выходную форму или детали слоя (например, выход, output_shape) из предварительно обученной модели.
Просто найдите layer.output_shape
следующие причины AttributeError: The layer has never been called and thus has no defined output shape.
. Пожалуйста, помогите понять, в чем причина ошибки.
model_name = 'distilbert-base-uncased'
max_sequence_length = MAX_SEQUENTH_LENGTH
num_labels = NUM_LABELS
from transformers import DistilBertTokenizerFast
tokenizer = DistilBertTokenizerFast.from_pretrained('distilbert-base-uncased')
def tokenize(sentences):
return tokenizer(
sentences,
truncation=True,
padding=True,
max_length=max_sequence_length,
return_tensors="tf"
)
tokens = tokenize("I say hello")
from transformers import TFDistilBertModel
# Use TFDistilBertModel as TFDistilBertForSequenceClassification has classification heads added.
# base = TFDistilBertForSequenceClassification.from_pretrained(model_name, num_labels=num_labels)
base = TFDistilBertModel.from_pretrained(model_name)
# Freeze the base model weights.
for layer in base.layers:
layer.trainable = False
base.summary()
---
Model: "tf_distil_bert_model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
distilbert (TFDistilBertMain multiple 66362880
=================================================================
Total params: 66,362,880
Trainable params: 0
Non-trainable params: 66,362,880
base.compile(
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=tf.keras.optimizers.Adam(learning_rate=LEARNING_RATE),
metrics=['accuracy']
)
for layer in base.layers:
print(layer.output_shape)for layer in base.layers:
print(layer.output_shape)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-89-c8756f2a58fc> in <module>()
1 for layer in base.layers:
----> 2 print(layer.output_shape)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py in output_shape(self)
2136 """
2137 if not self._inbound_nodes:
-> 2138 raise AttributeError('The layer has never been called '
2139 'and thus has no defined output shape.')
2140 all_output_shapes = set(
AttributeError: The layer has never been called and thus has no defined output shape.
Обходной путь
base_model_output = base(tokens)
base_model_output.last_hidden_state.shape
---
TensorShape([2, 6, 768])