【问题标题】:Trying to train model for Intent Recognition but getting float error尝试训练模型进行意图识别但出现浮动错误
【发布时间】:2021-03-26 05:32:48
【问题描述】:

我正在尝试训练模型以进行意图识别。我尝试删除所有特殊字符和停用词,但无法解决此错误。我也尝试删除整数,但它抛出了一个错误。我的数据有两列,一个文本和一个意图列

我写的代码是

class IntentDetectionData:
  DATA_COLUMN = "text"
  LABEL_COLUMN = "intent"

  def __init__(self, train, test, tokenizer: FullTokenizer, classes, max_seq_len=192):
    self.tokenizer = tokenizer
    self.max_seq_len = 0
    self.classes = classes
    
    train, test = map(lambda df: df.reindex(df[IntentDetectionData.DATA_COLUMN].str.len().sort_values().index), [train, test])
    
    ((self.train_x, self.train_y), (self.test_x, self.test_y)) = map(self._prepare, [train, test])

    print("max seq_len", self.max_seq_len)
    self.max_seq_len = min(self.max_seq_len, max_seq_len)
    self.train_x, self.test_x = map(self._pad, [self.train_x, self.test_x])

  def _prepare(self, df):
    x, y = [], []
    
    for _, row in tqdm(df.iterrows()):
      text, label = row[IntentDetectionData.DATA_COLUMN], row[IntentDetectionData.LABEL_COLUMN]
      tokens = self.tokenizer.tokenize(text)
      tokens = ["[CLS]"] + tokens + ["[SEP]"]
      token_ids = self.tokenizer.convert_tokens_to_ids(tokens)
      self.max_seq_len = max(self.max_seq_len, len(token_ids))
      x.append(token_ids)
      y.append(self.classes.index(label))

    return np.array(x), np.array(y)

  def _pad(self, ids):
    x = []
    for input_ids in ids:
      input_ids = input_ids[:min(len(input_ids), self.max_seq_len - 2)]
      input_ids = input_ids + [0] * (self.max_seq_len - len(input_ids))
      x.append(np.array(input_ids))
    return np.array(x)

下一个函数是

def create_model(max_seq_len, bert_ckpt_file):

  with tf.io.gfile.GFile(bert_config_file, "r") as reader:
      bc = StockBertConfig.from_json_string(reader.read())
      bert_params = map_stock_config_to_params(bc)
      bert_params.adapter_size = None
      bert = BertModelLayer.from_params(bert_params, name="bert")
        
  input_ids = keras.layers.Input(shape=(max_seq_len, ), dtype='int32', name="input_ids")
  bert_output = bert(input_ids)

  print("bert shape", bert_output.shape)

  cls_out = keras.layers.Lambda(lambda seq: seq[:, 0, :])(bert_output)
  cls_out = keras.layers.Dropout(0.5)(cls_out)
  logits = keras.layers.Dense(units=768, activation="tanh")(cls_out)
  logits = keras.layers.Dropout(0.5)(logits)
  logits = keras.layers.Dense(units=len(classes), activation="softmax")(logits)

  model = keras.Model(inputs=input_ids, outputs=logits)
  model.build(input_shape=(None, max_seq_len))

  load_stock_weights(bert, bert_ckpt_file)
        
  return model

接下来的代码是:

classes = train.intent.unique().tolist()

data = IntentDetectionData(train, test, tokenizer, classes, max_seq_len=10000)

运行上面的代码后,我得到了类似的错误

ValueError: Unsupported string type: <class 'float'>

【问题讨论】:

  • 你可以添加堆栈跟踪,错误发生在哪里?
  • 对于最后一个代码 'data = IntentDetectionData(train, test, tokenizer, classes, max_seq_len=10000)' 我遇到了错误。
  • 调用 IntentDetectionData.__init__ 时出现错误,但具体在哪里?抛出错误时,您应该会看到 stack trace
  • 在`tokens = self.tokenizer.tokenize(text), check print(type(tokens))`之后。当您尝试将字符串与浮点数连接时,通常会发生这种错误,并且通常不会给出堆栈跟踪,

标签: python machine-learning deep-learning nlp bert-language-model


【解决方案1】:

我遇到了同样的问题,并在 GitHub 上遇到了很多想法:https://github.com/google-research/bert/issues/559

就我而言,我的数据框(训练、测试)中有一些 NaN 值。我不得不用类似的东西替换它们:

train.fillna('unknown',inplace=True)

test 类似。这意味着我的“浮点”值现在是字符串。

【讨论】:

    猜你喜欢
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 2021-05-18
    • 2019-08-14
    • 2019-10-30
    相关资源
    最近更新 更多