【问题标题】:List index out of range when saving a fine tuned bert model保存微调的 bert 模型时列表索引超出范围
【发布时间】:2022-03-08 07:05:52
【问题描述】:

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

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

【问题讨论】:

  • title 应该是'list index out of range...'????

标签: tensorflow huggingface-transformers


【解决方案1】:

对不起,我的意思是写答案, 当我从

更改时,它为我解决了

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

【讨论】:

  • 请根据您的编程代码突出显示语法:)
猜你喜欢
  • 2021-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多