保存微调的 bert 模型时列出超出范围的索引

List index out of range when saving a fine tuned bert model

模型是使用以下函数定义创建的

def create_model(max_length = 256):
  bert_model = TFBertModel.from_pretrained('bert-base-uncased')
  for layer in bert_model.layers:
    layer.trainable = False
  input_ids = tf.keras.Input(shape = (max_length, ), dtype = tf.int32, name = 'input_ids')
  attention_masks = tf.keras.Input(shape = (max_length, ), dtype = tf.int32, name = 'attention_masks')
  x = bert_model.bert([input_ids, attention_masks])
  x = x.pooler_output
  x = tf.keras.layers.Dropout(0.2)(x)
  x = tf.keras.layers.Dense(256, activation = 'relu')(x)
  x = tf.keras.layers.Dropout(0.2)(x)
  x = tf.keras.layers.Dense(33)(x)
  out = tf.keras.layers.Activation('sigmoid')(x)
  model = tf.keras.Model(inputs = [input_ids, attention_masks], outputs = out)
  model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5),
                loss = tf.keras.losses.BinaryCrossentropy(),
                metrics = tf.metrics.BinaryAccuracy())
  return model

在尝试使用 tf.keras.models.save_model 保存模型时,我 运行 出现以下错误:
IndexError: Exception encountered when calling layer 'bert' (type TFBertMainLayer). list index out of range

对不起,我本来想写答案的, 当我从

更改时,它为我解决了

sequence_output = bert_layer.bert([input_ids, input_masks_ids])["last_hidden_state"]

sequence_output = bert_layer.bert(input_ids, input_masks_ids)["last_hidden_state"]

请检查下面的内容,

https://discuss.tensorflow.org/t/list-index-out-of-range-while-saving-a-trained-model/6901/4